Skip to content
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

Remove defer for improved inlining in v1.20+ using PGO #218

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 18 additions & 17 deletions scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}()
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
7 changes: 4 additions & 3 deletions scope_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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.
Expand Down