Skip to content
This repository was archived by the owner on Jan 16, 2026. It is now read-only.
This repository was archived by the owner on Jan 16, 2026. It is now read-only.

histogram.go: use of memory can be made more efficient by using a circular buffer #70

@keep94

Description

@keep94

This line here

h.priorTimedBinsList = h.priorTimedBinsList[1:]
makes inefficient use of memory. The bin that used to be at h.priorTimesBinList[0] doesn't go away, but it stays in the memory chunk allocated for the h.priorTimesBiList slice. If h.maxBins = 100, the capacity of the memory chunk holding the slice will eventually be 200. As bins are added and removed from the slice, the usable portion of the bins slice of 100 will walk up the memory chunk. When the bins slice reaches the end of the memory chunk of 200, go's append will allocate a new memory chunk of 200 and copy the bin slice of 100 to the start of that new memory chunk, and the bins slice will begin walking up that new memory chunk. What this means is that go programs using this histogram library will be doing more memory allocations and holding onto more memory than necessary. Also when the bins slice is near the end of its memory chunk, all of those TDigest objects in bins that fell off the beginning of the slice may not get GCed until the memory chunk gets GCed. A circular buffer would be good to use here. The circular buffer would only need to be allocated one time. Moreover, when the circular buffer starts overwriting old bins, it may be possible to reuse those old TDigest objects rather than allocate new ones.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions