-
Notifications
You must be signed in to change notification settings - Fork 69
perf(metrics): reduce allocations for metrics #33699 #2037
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev-upgrade
Are you sure you want to change the base?
Conversation
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR aims to reduce allocations in the metrics package by avoiding the generic getOrRegister helper (which required an extra closure capture) and inlining Registry.GetOrRegister calls across metric constructors.
Changes:
- Inline
nil-registry fallback +Registry.GetOrRegisterin multipleGetOrRegister*helpers to reduce closure captures/allocations. - Remove the generic internal
getOrRegister[T]helper frommetrics/registry.go. - Add a new benchmark for
GetOrRegisterhelpers and adjust a prefixed-child-registry test to use a non-default parent registry.
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| metrics/counter.go | Inline GetOrRegisterCounter implementation using Registry.GetOrRegister. |
| metrics/counter_float64.go | Inline GetOrRegisterCounterFloat64 implementation using Registry.GetOrRegister. |
| metrics/gauge.go | Inline GetOrRegisterGauge implementation using Registry.GetOrRegister. |
| metrics/gauge_float64.go | Inline GetOrRegisterGaugeFloat64 implementation using Registry.GetOrRegister. |
| metrics/gauge_info.go | Inline GetOrRegisterGaugeInfo implementation using Registry.GetOrRegister. |
| metrics/histogram.go | Inline GetOrRegisterHistogram / GetOrRegisterHistogramLazy using Registry.GetOrRegister. |
| metrics/meter.go | Inline GetOrRegisterMeter implementation using Registry.GetOrRegister. |
| metrics/resetting_timer.go | Inline GetOrRegisterResettingTimer implementation using Registry.GetOrRegister. |
| metrics/runtimehistogram.go | Inline runtime histogram registration via Registry.GetOrRegister. |
| metrics/timer.go | Inline GetOrRegisterTimer implementation using Registry.GetOrRegister. |
| metrics/registry.go | Remove generic getOrRegister[T] helper. |
| metrics/registry_test.go | Add benchmark for GetOrRegister* helpers; change one test to use NewRegistry() as parent. |
Comments suppressed due to low confidence (1)
metrics/registry_test.go:299
- TestChildPrefixedRegistryRegister calls the package-level Register("bar", c) but ignores the returned error. This test file already registers "bar" in another test, so depending on execution order this can silently hit ErrDuplicateMetric and mask failures. Please assert on the error and/or use a unique metric name (or unregister it) to avoid global DefaultRegistry pollution.
r := NewPrefixedChildRegistry(NewRegistry(), "prefix.")
err := r.Register("foo", NewCounter())
c := NewCounter()
Register("bar", c)
if err != nil {
t.Fatal(err.Error())
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| sample := func() Sample { return nil } | ||
| tests := []struct { | ||
| name string | ||
| ctor func() any | ||
| }{ | ||
| {name: "counter", ctor: func() any { return GetOrRegisterCounter("counter", DefaultRegistry) }}, | ||
| {name: "gauge", ctor: func() any { return GetOrRegisterGauge("gauge", DefaultRegistry) }}, | ||
| {name: "gaugefloat64", ctor: func() any { return GetOrRegisterGaugeFloat64("gaugefloat64", DefaultRegistry) }}, | ||
| {name: "histogram", ctor: func() any { return GetOrRegisterHistogram("histogram", DefaultRegistry, sample()) }}, | ||
| {name: "meter", ctor: func() any { return GetOrRegisterMeter("meter", DefaultRegistry) }}, | ||
| {name: "timer", ctor: func() any { return GetOrRegisterTimer("timer", DefaultRegistry) }}, | ||
| {name: "gaugeinfo", ctor: func() any { return GetOrRegisterGaugeInfo("gaugeinfo", DefaultRegistry) }}, | ||
| {name: "resettingtimer", ctor: func() any { return GetOrRegisterResettingTimer("resettingtimer", DefaultRegistry) }}, | ||
| {name: "runtimehistogramlazy", ctor: func() any { return GetOrRegisterHistogramLazy("runtimehistogramlazy", DefaultRegistry, sample) }}, |
Copilot
AI
Feb 10, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In BenchmarkRegistryGetOrRegister, the histogram and histogram-lazy cases construct a Histogram with a nil Sample (sample() returns nil). That creates a StandardHistogram with a nil backing sample, which will panic if any histogram method (Update/Snapshot/Clear/etc.) is used and also leaves an invalid metric registered in the registry. Use a real Sample implementation (e.g., NewUniformSample/NewExpDecaySample) for the benchmark inputs.
| ctor func() any | ||
| }{ | ||
| {name: "counter", ctor: func() any { return GetOrRegisterCounter("counter", DefaultRegistry) }}, | ||
| {name: "gauge", ctor: func() any { return GetOrRegisterGauge("gauge", DefaultRegistry) }}, | ||
| {name: "gaugefloat64", ctor: func() any { return GetOrRegisterGaugeFloat64("gaugefloat64", DefaultRegistry) }}, | ||
| {name: "histogram", ctor: func() any { return GetOrRegisterHistogram("histogram", DefaultRegistry, sample()) }}, | ||
| {name: "meter", ctor: func() any { return GetOrRegisterMeter("meter", DefaultRegistry) }}, | ||
| {name: "timer", ctor: func() any { return GetOrRegisterTimer("timer", DefaultRegistry) }}, | ||
| {name: "gaugeinfo", ctor: func() any { return GetOrRegisterGaugeInfo("gaugeinfo", DefaultRegistry) }}, | ||
| {name: "resettingtimer", ctor: func() any { return GetOrRegisterResettingTimer("resettingtimer", DefaultRegistry) }}, | ||
| {name: "runtimehistogramlazy", ctor: func() any { return GetOrRegisterHistogramLazy("runtimehistogramlazy", DefaultRegistry, sample) }}, | ||
| } | ||
| for _, test := range tests { | ||
| b.Run(test.name, func(b *testing.B) { | ||
| for i := 0; i < b.N; i++ { | ||
| test.ctor() |
Copilot
AI
Feb 10, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BenchmarkRegistryGetOrRegister uses DefaultRegistry for all subbenchmarks, which introduces shared global state and can skew results (e.g., if other tests/benchmarks register the same names) and leaves registered metrics behind. Prefer using a fresh Registry per sub-benchmark (or explicitly Unregister/replace the registry) to keep the benchmark isolated and repeatable.
| ctor func() any | |
| }{ | |
| {name: "counter", ctor: func() any { return GetOrRegisterCounter("counter", DefaultRegistry) }}, | |
| {name: "gauge", ctor: func() any { return GetOrRegisterGauge("gauge", DefaultRegistry) }}, | |
| {name: "gaugefloat64", ctor: func() any { return GetOrRegisterGaugeFloat64("gaugefloat64", DefaultRegistry) }}, | |
| {name: "histogram", ctor: func() any { return GetOrRegisterHistogram("histogram", DefaultRegistry, sample()) }}, | |
| {name: "meter", ctor: func() any { return GetOrRegisterMeter("meter", DefaultRegistry) }}, | |
| {name: "timer", ctor: func() any { return GetOrRegisterTimer("timer", DefaultRegistry) }}, | |
| {name: "gaugeinfo", ctor: func() any { return GetOrRegisterGaugeInfo("gaugeinfo", DefaultRegistry) }}, | |
| {name: "resettingtimer", ctor: func() any { return GetOrRegisterResettingTimer("resettingtimer", DefaultRegistry) }}, | |
| {name: "runtimehistogramlazy", ctor: func() any { return GetOrRegisterHistogramLazy("runtimehistogramlazy", DefaultRegistry, sample) }}, | |
| } | |
| for _, test := range tests { | |
| b.Run(test.name, func(b *testing.B) { | |
| for i := 0; i < b.N; i++ { | |
| test.ctor() | |
| ctor func(r Registry) any | |
| }{ | |
| {name: "counter", ctor: func(r Registry) any { return GetOrRegisterCounter("counter", r) }}, | |
| {name: "gauge", ctor: func(r Registry) any { return GetOrRegisterGauge("gauge", r) }}, | |
| {name: "gaugefloat64", ctor: func(r Registry) any { return GetOrRegisterGaugeFloat64("gaugefloat64", r) }}, | |
| {name: "histogram", ctor: func(r Registry) any { return GetOrRegisterHistogram("histogram", r, sample()) }}, | |
| {name: "meter", ctor: func(r Registry) any { return GetOrRegisterMeter("meter", r) }}, | |
| {name: "timer", ctor: func(r Registry) any { return GetOrRegisterTimer("timer", r) }}, | |
| {name: "gaugeinfo", ctor: func(r Registry) any { return GetOrRegisterGaugeInfo("gaugeinfo", r) }}, | |
| {name: "resettingtimer", ctor: func(r Registry) any { return GetOrRegisterResettingTimer("resettingtimer", r) }}, | |
| {name: "runtimehistogramlazy", ctor: func(r Registry) any { return GetOrRegisterHistogramLazy("runtimehistogramlazy", r, sample) }}, | |
| } | |
| for _, test := range tests { | |
| b.Run(test.name, func(b *testing.B) { | |
| r := NewRegistry() | |
| b.ResetTimer() | |
| for i := 0; i < b.N; i++ { | |
| test.ctor(r) |
| // GetOrRegister gets an existing metric or creates and registers a new one. Threadsafe | ||
| // alternative to calling Get and Register on failure. | ||
| func GetOrRegister(name string, i func() interface{}) interface{} { | ||
| return DefaultRegistry.GetOrRegister(name, i) | ||
| } | ||
|
|
||
| func getOrRegister[T any](name string, ctor func() T, r Registry) T { | ||
| if r == nil { | ||
| r = DefaultRegistry | ||
| } | ||
| return r.GetOrRegister(name, func() any { return ctor() }).(T) | ||
| } | ||
|
|
||
| // Register the given metric under the given name. Returns a ErrDuplicateMetric | ||
| // if a metric by the given name is already registered. | ||
| func Register(name string, i interface{}) error { |
Copilot
AI
Feb 10, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing the shared getOrRegister helper has introduced repeated "if r == nil { r = DefaultRegistry }" blocks across many metric types. To keep the allocation improvement while avoiding duplication and future inconsistency, consider adding a small non-generic internal helper that takes ctor func() interface{} (so callers can pass non-capturing func literals) and centralizes the nil->DefaultRegistry behavior.
Proposed changes
Ref: ethereum#33699
Types of changes
What types of changes does your code introduce to XDC network?
Put an
✅in the boxes that applyImpacted Components
Which part of the codebase this PR will touch base on,
Put an
✅in the boxes that applyChecklist
Put an
✅in the boxes once you have confirmed below actions (or provide reasons on not doing so) that