Description
When entries exit the unstable
log structure
Line 156 in 3e6cb62
it shrinks itself based on the entries slice length and cap:
Lines 162 to 179 in 3e6cb62
Firstly, the len(u.entries)*lenMultiple < cap(u.entries)
does not necessarily do a right thing. After shifting the slice start in u.entries = u.entries[num:]
, the first num
entries "disappear" from len
and cap
of this slice: https://go.dev/play/p/ao1xaL0Edla.
So it's possible that len >= cap/2
all the time, even though only a small portion of the backing slice is used. We should take the cap
of the entire backing slice into account instead.
Secondly, this heuristic only takes len/cap
of the slice into consideration, but not the size of the entries referenced by it. It could be beneficial to, in addition, shrink the slice if the sum size of the "garbage" entries is more than a half. This would keep the overall memory consumption more controlled. Doing so would require maintaining a running sum of entry sizes in the unstable
struct (which we do anyway in other places for rate limiting purposes).
The same heuristic could be applied in MemoryStorage.Compact method to smooth out the allocation/copying cost of log truncations. Frequent truncations may incur a quadratic cost here, while the heuristic allows capping it at O(N).