summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-05-22 11:37:07 +0300
committerPaul Buetow <paul@buetow.org>2026-05-22 11:37:07 +0300
commit16905a183606efbcb908a12af1a821cc525ff268 (patch)
treeb1d509ddda053a9825a6ee63a366e5f7ecffb5d7
parent8356d25b5411b52333549dea66c19a1ea025c49c (diff)
perf(rpn): cache Cool metric pointer to avoid repeated RWMutex lookups
GetCoolMetric() is called for every NewFloat/NewRat/NewNumber. Previously acquired RWMutex read lock via GetMetricRegistry().Find() each time. Now caches the *Metric pointer during registry initialization, returning a bare pointer after first call with no lock needed.
-rw-r--r--internal/rpn/metric_registry.go2
-rw-r--r--internal/rpn/number.go4
2 files changed, 6 insertions, 0 deletions
diff --git a/internal/rpn/metric_registry.go b/internal/rpn/metric_registry.go
index a214737..de6b1a4 100644
--- a/internal/rpn/metric_registry.go
+++ b/internal/rpn/metric_registry.go
@@ -20,12 +20,14 @@ type MetricRegistry struct {
// Global registry instance.
var defaultRegistry *MetricRegistry
var registryOnce sync.Once
+var cachedCoolMetric *Metric // cached Cool metric pointer (set after init)
// GetMetricRegistry returns the global metric registry, initialized with built-in metrics.
func GetMetricRegistry() *MetricRegistry {
registryOnce.Do(func() {
defaultRegistry = NewMetricRegistry()
registerBuiltInMetrics(defaultRegistry)
+ cachedCoolMetric, _ = defaultRegistry.Find("Cool")
})
return defaultRegistry
}
diff --git a/internal/rpn/number.go b/internal/rpn/number.go
index 9a7ac99..b82c4f6 100644
--- a/internal/rpn/number.go
+++ b/internal/rpn/number.go
@@ -56,7 +56,11 @@ func NewNumber(value float64, mode CalculationMode, metric ...*Metric) Number {
}
// GetCoolMetric returns the universal default metric.
+// Returns a cached pointer after first call — no lock needed.
func GetCoolMetric() *Metric {
+ if cachedCoolMetric != nil {
+ return cachedCoolMetric
+ }
m, _ := GetMetricRegistry().Find("Cool")
return m
}