From 4c0e26bfa1bbf18e801e2b8c0b8b79359d6f9dca Mon Sep 17 00:00:00 2001 From: subham sarkar Date: Sun, 6 Sep 2020 16:40:38 +0530 Subject: [PATCH] Avoid unnecessary initialization of variables This commit fixes the case when the metric is not present in rm (registered metric) then LastRegisteredAt and TTL are initialized twice. Firstly, LastRegisteredAt is initialized with the time.Time's zero value and then with clock.Now(). Plus, TTL is initialized with the same value twice. Signed-off-by: subham sarkar --- pkg/registry/registry.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pkg/registry/registry.go b/pkg/registry/registry.go index 0022848..42adcff 100644 --- a/pkg/registry/registry.go +++ b/pkg/registry/registry.go @@ -107,18 +107,20 @@ func (r *Registry) Store(metricName string, hash metrics.LabelHash, labels prome metric.Vectors[hash.Names] = v } + now := clock.Now() rm, ok := metric.Metrics[hash.Values] if !ok { rm = &metrics.RegisteredMetric{ - Labels: labels, - TTL: ttl, - Metric: mh, - VecKey: hash.Names, + LastRegisteredAt: now, + Labels: labels, + TTL: ttl, + Metric: mh, + VecKey: hash.Names, } metric.Metrics[hash.Values] = rm v.RefCount++ + return } - now := clock.Now() rm.LastRegisteredAt = now // Update ttl from mapping rm.TTL = ttl