Skip to content

Commit

Permalink
Added fast path to normal (empty) registry to save 8 allocs and 3K5B …
Browse files Browse the repository at this point in the history
…per Gather.

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>
  • Loading branch information
bwplotka committed Oct 24, 2021
1 parent 3dcf61c commit 8afb168
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion prometheus/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,14 @@ func (r *Registry) MustRegister(cs ...Collector) {

// Gather implements Gatherer.
func (r *Registry) Gather() ([]*dto.MetricFamily, error) {
r.mtx.RLock()

if len(r.collectorsByID) == 0 && len(r.uncheckedCollectors) == 0 {
// Fast path.
r.mtx.RUnlock()
return nil, nil
}

var (
checkedMetricChan = make(chan Metric, capMetricChan)
uncheckedMetricChan = make(chan Metric, capMetricChan)
Expand All @@ -416,7 +424,6 @@ func (r *Registry) Gather() ([]*dto.MetricFamily, error) {
registeredDescIDs map[uint64]struct{} // Only used for pedantic checks
)

r.mtx.RLock()
goroutineBudget := len(r.collectorsByID) + len(r.uncheckedCollectors)
metricFamiliesByName := make(map[string]*dto.MetricFamily, len(r.dimHashesByName))
checkedCollectors := make(chan Collector, len(r.collectorsByID))
Expand Down

0 comments on commit 8afb168

Please sign in to comment.