From 8563ed03e03b89c6e9a9c3a988381e65705a409d Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Tue, 2 Sep 2025 15:18:14 +0100 Subject: [PATCH] Scraping: use clear builtin function This was added in Go 1.21, and is neater than a loop deleting all elements. Also move the comment noting why we do this, because it could be read as saying this is the only reason we have two maps. Signed-off-by: Bryan Boreham --- scrape/scrape.go | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/scrape/scrape.go b/scrape/scrape.go index 5c611c1f74..5b658dc5aa 100644 --- a/scrape/scrape.go +++ b/scrape/scrape.go @@ -981,7 +981,6 @@ type scrapeCache struct { droppedSeries map[string]*uint64 // Series that were seen in the current and previous scrape, for staleness detection. - // We hold two maps and swap them out to save allocations. seriesCur map[storage.SeriesRef]*cacheEntry seriesPrev map[storage.SeriesRef]*cacheEntry @@ -1059,13 +1058,9 @@ func (c *scrapeCache) iterDone(flushCache bool) { c.metaMtx.Unlock() } - // Swap current and previous series. + // Swap current and previous series then clear the new current, to save allocations. c.seriesPrev, c.seriesCur = c.seriesCur, c.seriesPrev - - // We have to delete every single key in the map. - for k := range c.seriesCur { - delete(c.seriesCur, k) - } + clear(c.seriesCur) c.iter++ }