From 90e98e0235522451bf35d054173702c8837f97b1 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Sat, 21 Oct 2023 13:45:47 +0000 Subject: [PATCH] tsdb: create isolation transaction slice on demand When Prometheus restarts it creates every series read in from the WAL, but many of those series will be finished, and never receive any more samples. By defering allocation of the txRing slice to when it is first needed, we save 32 bytes per stale series. Signed-off-by: Bryan Boreham --- tsdb/head.go | 2 +- tsdb/isolation.go | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/tsdb/head.go b/tsdb/head.go index 0ea2b8385..4a6e6ac73 100644 --- a/tsdb/head.go +++ b/tsdb/head.go @@ -2011,7 +2011,7 @@ func newMemSeries(lset labels.Labels, id chunks.HeadSeriesRef, isolationDisabled nextAt: math.MinInt64, } if !isolationDisabled { - s.txs = newTxRing(4) + s.txs = newTxRing(0) } return s } diff --git a/tsdb/isolation.go b/tsdb/isolation.go index 19eba9340..86330f36e 100644 --- a/tsdb/isolation.go +++ b/tsdb/isolation.go @@ -253,7 +253,11 @@ func newTxRing(capacity int) *txRing { func (txr *txRing) add(appendID uint64) { if int(txr.txIDCount) == len(txr.txIDs) { // Ring buffer is full, expand by doubling. - newRing := make([]uint64, txr.txIDCount*2) + newLen := txr.txIDCount * 2 + if newLen == 0 { + newLen = 4 + } + newRing := make([]uint64, newLen) idx := copy(newRing, txr.txIDs[txr.txIDFirst:]) copy(newRing[idx:], txr.txIDs[:txr.txIDFirst]) txr.txIDs = newRing @@ -265,6 +269,9 @@ func (txr *txRing) add(appendID uint64) { } func (txr *txRing) cleanupAppendIDsBelow(bound uint64) { + if len(txr.txIDs) == 0 { + return + } pos := int(txr.txIDFirst) for txr.txIDCount > 0 {