Skip to content

Conversation

@gzliudan
Copy link
Collaborator

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 apply

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation Update (if none of the other choices apply)
  • Regular KTLO or any of the maintaince work. e.g code style
  • CICD Improvement

Impacted Components

Which part of the codebase this PR will touch base on,

Put an in the boxes that apply

  • Consensus
  • Account
  • Network
  • Geth
  • Smart Contract
  • External components
  • Not sure (Please specify below)

Checklist

Put an in the boxes once you have confirmed below actions (or provide reasons on not doing so) that

  • This PR has sufficient test coverage (unit/integration test) OR I have provided reason in the PR description for not having test coverage
  • Provide an end-to-end test plan in the PR description on how to manually test it on the devnet/testnet.
  • Tested the backwards compatibility.
  • Tested with XDC nodes running this version co-exist with those running the previous version.
  • Relevant documentation has been updated as part of this PR
  • N/A

@coderabbitai
Copy link

coderabbitai bot commented Feb 10, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

Copilot AI left a 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.GetOrRegister in multiple GetOrRegister* helpers to reduce closure captures/allocations.
  • Remove the generic internal getOrRegister[T] helper from metrics/registry.go.
  • Add a new benchmark for GetOrRegister helpers 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.

Comment on lines +18 to +31
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) }},
Copy link

Copilot AI Feb 10, 2026

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.

Copilot uses AI. Check for mistakes.
Comment on lines +21 to +36
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()
Copy link

Copilot AI Feb 10, 2026

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.

Suggested change
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)

Copilot uses AI. Check for mistakes.
Comment on lines 328 to 336
// 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 {
Copy link

Copilot AI Feb 10, 2026

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.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants