Skip to content

Commit 2850f47

Browse files
committed
Added fast path to normal registry to save 119 allocs and 3KB per Gather.
Signed-off-by: Bartlomiej Plotka <[email protected]>
1 parent 3dcf61c commit 2850f47

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

prometheus/registry.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,14 @@ func (r *Registry) MustRegister(cs ...Collector) {
407407

408408
// Gather implements Gatherer.
409409
func (r *Registry) Gather() ([]*dto.MetricFamily, error) {
410+
r.mtx.RLock()
411+
412+
if len(r.collectorsByID) == 0 && len(r.uncheckedCollectors) == 0 {
413+
// Fast path.
414+
r.mtx.RUnlock()
415+
return nil, nil
416+
}
417+
410418
var (
411419
checkedMetricChan = make(chan Metric, capMetricChan)
412420
uncheckedMetricChan = make(chan Metric, capMetricChan)
@@ -416,7 +424,6 @@ func (r *Registry) Gather() ([]*dto.MetricFamily, error) {
416424
registeredDescIDs map[uint64]struct{} // Only used for pedantic checks
417425
)
418426

419-
r.mtx.RLock()
420427
goroutineBudget := len(r.collectorsByID) + len(r.uncheckedCollectors)
421428
metricFamiliesByName := make(map[string]*dto.MetricFamily, len(r.dimHashesByName))
422429
checkedCollectors := make(chan Collector, len(r.collectorsByID))

0 commit comments

Comments
 (0)