From 05b6a3d83ee1cc7c52f4793b90d044fc4085c73a Mon Sep 17 00:00:00 2001 From: Raj Barik Date: Mon, 15 May 2023 12:59:55 -0700 Subject: [PATCH] Remove defer for improved inlining in v1.20+ using Profile-Guided Optimization (PGO) --- scope.go | 35 ++++++++++++++++++----------------- scope_registry.go | 7 ++++--- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/scope.go b/scope.go index 1418348d..c4a70941 100644 --- a/scope.go +++ b/scope.go @@ -188,8 +188,8 @@ func newRootScope(opts ScopeOptions, interval time.Duration) *scope { if interval > 0 { s.wg.Add(1) go func() { - defer s.wg.Done() s.reportLoop(interval) + s.wg.Done() }() } @@ -281,9 +281,9 @@ func (s *scope) Counter(name string) Counter { } s.cm.Lock() - defer s.cm.Unlock() if c, ok := s.counters[name]; ok { + s.cm.Unlock() return c } @@ -299,14 +299,14 @@ func (s *scope) Counter(name string) Counter { s.counters[name] = c s.countersSlice = append(s.countersSlice, c) + s.cm.Unlock() return c } func (s *scope) counter(sanitizedName string) (Counter, bool) { s.cm.RLock() - defer s.cm.RUnlock() - c, ok := s.counters[sanitizedName] + s.cm.RUnlock() return c, ok } @@ -317,9 +317,9 @@ func (s *scope) Gauge(name string) Gauge { } s.gm.Lock() - defer s.gm.Unlock() if g, ok := s.gauges[name]; ok { + s.gm.Unlock() return g } @@ -334,14 +334,14 @@ func (s *scope) Gauge(name string) Gauge { s.gauges[name] = g s.gaugesSlice = append(s.gaugesSlice, g) + s.gm.Unlock() return g } func (s *scope) gauge(name string) (Gauge, bool) { s.gm.RLock() - defer s.gm.RUnlock() - g, ok := s.gauges[name] + s.gm.RUnlock() return g, ok } @@ -352,9 +352,9 @@ func (s *scope) Timer(name string) Timer { } s.tm.Lock() - defer s.tm.Unlock() if t, ok := s.timers[name]; ok { + s.tm.Unlock() return t } @@ -370,14 +370,14 @@ func (s *scope) Timer(name string) Timer { ) s.timers[name] = t + s.tm.Unlock() return t } func (s *scope) timer(sanitizedName string) (Timer, bool) { s.tm.RLock() - defer s.tm.RUnlock() - t, ok := s.timers[sanitizedName] + s.tm.RUnlock() return t, ok } @@ -397,9 +397,9 @@ func (s *scope) Histogram(name string, b Buckets) Histogram { } s.hm.Lock() - defer s.hm.Unlock() if h, ok := s.histograms[name]; ok { + s.hm.Unlock() return h } @@ -421,14 +421,14 @@ func (s *scope) Histogram(name string, b Buckets) Histogram { s.histograms[name] = h s.histogramsSlice = append(s.histogramsSlice, h) + s.hm.Unlock() return h } func (s *scope) histogram(sanitizedName string) (Histogram, bool) { s.hm.RLock() - defer s.hm.RUnlock() - h, ok := s.histograms[sanitizedName] + s.hm.RUnlock() return h, ok } @@ -536,10 +536,6 @@ func (s *scope) clearMetrics() { s.gm.Lock() s.tm.Lock() s.hm.Lock() - defer s.cm.Unlock() - defer s.gm.Unlock() - defer s.tm.Unlock() - defer s.hm.Unlock() for k := range s.counters { delete(s.counters, k) @@ -559,6 +555,11 @@ func (s *scope) clearMetrics() { delete(s.histograms, k) } s.histogramsSlice = nil + + s.hm.Unlock() + s.tm.Unlock() + s.gm.Unlock() + s.cm.Unlock() } // NB(prateek): We assume concatenation of sanitized inputs is diff --git a/scope_registry.go b/scope_registry.go index c52514db..17c682bb 100644 --- a/scope_registry.go +++ b/scope_registry.go @@ -164,12 +164,12 @@ func (r *scopeRegistry) Subscope(parent *scope, prefix string, tags map[string]s key := scopeRegistryKey(prefix, parent.tags, tags) subscopeBucket.mu.Lock() - defer subscopeBucket.mu.Unlock() if s, ok := r.lockedLookup(subscopeBucket, key); ok { if _, ok = r.lockedLookup(subscopeBucket, preSanitizeKey); !ok { subscopeBucket.s[preSanitizeKey] = s } + subscopeBucket.mu.Unlock() return s } @@ -201,6 +201,7 @@ func (r *scopeRegistry) Subscope(parent *scope, prefix string, tags map[string]s if _, ok := r.lockedLookup(subscopeBucket, preSanitizeKey); !ok { subscopeBucket.s[preSanitizeKey] = subscope } + subscopeBucket.mu.Unlock() return subscope } @@ -229,10 +230,10 @@ func (r *scopeRegistry) removeWithRLock(subscopeBucket *scopeBucket, key string) // n.b. This function must lock the registry for writing and return it to an // RLocked state prior to exiting. Defer order is important (LIFO). subscopeBucket.mu.RUnlock() - defer subscopeBucket.mu.RLock() subscopeBucket.mu.Lock() - defer subscopeBucket.mu.Unlock() delete(subscopeBucket.s, key) + subscopeBucket.mu.Unlock() + subscopeBucket.mu.RLock() } // Records internal Metrics' cardinalities.