Skip to content

Commit

Permalink
Added fast path to normal registry to save 119 allocs and 3KB per Gat…
Browse files Browse the repository at this point in the history
…her.

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>
  • Loading branch information
bwplotka committed Oct 24, 2021
1 parent 3dcf61c commit 2850f47
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion prometheus/registry.go
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 2850f47

Please sign in to comment.