MCPcopy Index your code
hub / github.com/prometheus/prometheus / shrinkChunkRefMap

Method shrinkChunkRefMap

tsdb/chunks/chunk_write_queue.go:151–178  ·  view source on GitHub ↗

shrinkChunkRefMap checks whether the conditions to shrink the chunkRefMap are met, if so chunkRefMap is reinitialized. The chunkRefMapMtx must be held when calling this method. We do this because Go runtime doesn't release internal memory used by map after map has been emptied. To achieve that we c

()

Source from the content-addressed store, hash-verified

149// We do this because Go runtime doesn't release internal memory used by map after map has been emptied.
150// To achieve that we create new map instead and throw the old one away.
151func (c *chunkWriteQueue) shrinkChunkRefMap() {
152 if len(c.chunkRefMap) > 0 {
153 // Can't shrink it while there is data in it.
154 return
155 }
156
157 if c.chunkRefMapPeakSize < chunkRefMapShrinkThreshold {
158 // Not shrinking it because it has not grown to the minimum threshold yet.
159 return
160 }
161
162 now := time.Now()
163
164 if now.Sub(c.chunkRefMapLastShrink) < chunkRefMapMinShrinkInterval {
165 // Not shrinking it because the minimum duration between shrink-events has not passed yet.
166 return
167 }
168
169 // Re-initialize the chunk ref map to half of the peak size that it has grown to since the last re-init event.
170 // We are trying to hit the sweet spot in the trade-off between initializing it to a very small size
171 // potentially resulting in many allocations to re-grow it, and initializing it to a large size potentially
172 // resulting in unused allocated memory.
173 c.chunkRefMap = make(map[ChunkDiskMapperRef]chunkenc.Chunk, c.chunkRefMapPeakSize/2)
174
175 c.chunkRefMapPeakSize = 0
176 c.chunkRefMapLastShrink = now
177 c.shrink.Inc()
178}
179
180func (c *chunkWriteQueue) addJob(job chunkWriteJob) (err error) {
181 defer func() {

Callers 1

processJobMethod · 0.95

Calls 2

IncMethod · 0.80
SubMethod · 0.45

Tested by

no test coverage detected