Open valyala opened 2 days ago
Related Issues
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
This is another case where I think map shrinking (#20135) is probably the right solution. Or at least, it would help a lot, and maybe enough. And it helps lots of other cases.
The issue
The following pattern is frequently used in order to avoid excess memory allocations by re-using the map:
It has been appeared that
clear(m)
performance is proportional to the number of buckets inm
. The number of buckets can grow significantly ataddSomeItemsToMap()
. After that the performance ofclear(m)
can slow down significantly (and forever), even if only a few items are added into the map on subsequent iterations.See https://philpearl.github.io/post/map_clearing_and_size/ for more details.
The solution
Go runtime must be able to switch between the algorithm, which unconditionally clears all the buckets in
m
, and the algorithm, which clears only the buckets, which contain at least a single item, depending on the ratio between the number of items in the map and the number of buckets in it. This should improve performance ofclear(m)
in the pattern above when every iteration can store widely different number of items in m.