You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In many instances, especially when computing intersections, we generate empty containers that are immediately garbage collected.
There are clever tricks we could use to start scanning the input containers, advance up to the point where there might be values to be intersected, and bail out without generating an empty container if the intersection is empty.
hmmm When I read this issue I was thinking about reusing containers, which would may help here too.
I mean, when roaring decides to automatically optimize a container (like on iAddReturnMinimized method, for example), it discards the old container without any chance to reuse it in the future, so on a dynamic environment, if someone calls Remove on that same bitmap the recently created container is, again, downgraded to the old container, which is allocated again without reusing the old container and increasing GC pressure.
Personally, I think reusing a container could help here too. Yeah, the algorithm you suggested could probably be better for the intersection case, but in cases where adding/removing integers to the bitmap causes a bunch of containers to be allocated and deallocated it could be interesting to reuse containers directly in the library, internally.
So what I suggest is: arrayContainerPool for arrayContainer, bitmapContainerPool for bitmapContainer and, of course, runContainerPool for runContainers. And then, instead of just returning the new container and discarding the old container, we just clear() the old container, put in the pool and, when allocating the container, just get the correspondent container of the pool. So it's a more generic option to improve performance while avoiding container allocations when adding and removing elements from the bitmap.
Oh, for sure. Only benchmarks to test these sort of things. And for sure no-allocation (like in the algorithm for intersections) is always better than one allocation here and there using pools. :)
I have observed high CPU usage caused by the memory allocations when using Bitmap.ReadFrom and bitmap.Or/Clone frequently. By implementing memory reuse, we should be able to effectively resolve the majority of this problem.
There are clever tricks we could use to start scanning the input containers, advance up to the point where there might be values to be intersected, and bail out without generating an empty container if the intersection is empty
@lemire I have applied this technique in 2 PRs and the results are promising!
#566 counts the result before allocating it in runContainer16.andBitmapContainer and bitmapContainer.iandRun16, and runContainer16.intersects now returns at the first shared value instead of computing the whole intersection just to find out whether it is empty.
empty And: 8 KB → 104 bytes, 5x faster
in-place And: zero allocations
Intersects on disjoint containers: zero allocations, 25x faster
#572 carries the same idea into FastAnd with three or more inputs: the input with the fewest containers drives, and a key is probed for a shared value before any containers are intersected, so a key with nothing in common allocates nothing.
Activity
lemire commentedon Mar 30, 2018
Example:
fjorgemota commentedon Jan 11, 2019
hmmm When I read this issue I was thinking about reusing containers, which would may help here too.
I mean, when roaring decides to automatically optimize a container (like on
iAddReturnMinimizedmethod, for example), it discards the old container without any chance to reuse it in the future, so on a dynamic environment, if someone callsRemoveon that same bitmap the recently created container is, again, downgraded to the old container, which is allocated again without reusing the old container and increasing GC pressure.Personally, I think reusing a container could help here too. Yeah, the algorithm you suggested could probably be better for the intersection case, but in cases where adding/removing integers to the bitmap causes a bunch of containers to be allocated and deallocated it could be interesting to reuse containers directly in the library, internally.
So what I suggest is:
arrayContainerPoolfor arrayContainer,bitmapContainerPoolfor bitmapContainer and, of course,runContainerPoolfor runContainers. And then, instead of just returning the new container and discarding the old container, we justclear()the old container, put in the pool and, when allocating the container, just get the correspondent container of the pool. So it's a more generic option to improve performance while avoiding container allocations when adding and removing elements from the bitmap.What do you think?
lemire commentedon Jan 11, 2019
@fjorgemota Yes, this is sensible, but one would like to see actual performance gains in realistic scenarios.
fjorgemota commentedon Jan 11, 2019
Oh, for sure. Only benchmarks to test these sort of things. And for sure no-allocation (like in the algorithm for intersections) is always better than one allocation here and there using pools. :)
damnever commentedon Aug 15, 2023
I have observed high CPU usage caused by the memory allocations when using Bitmap.ReadFrom and bitmap.Or/Clone frequently. By implementing memory reuse, we should be able to effectively resolve the majority of this problem.
damnever commentedon Aug 16, 2023
I have submitted a pull request to resolve the issue with ReadFrom #395
tamirms commentedon Sep 17, 2026
@lemire I have applied this technique in 2 PRs and the results are promising!
#566 counts the result before allocating it in
runContainer16.andBitmapContainerandbitmapContainer.iandRun16, andrunContainer16.intersectsnow returns at the first shared value instead of computing the whole intersection just to find out whether it is empty.And: 8 KB → 104 bytes, 5x fasterAnd: zero allocationsIntersectson disjoint containers: zero allocations, 25x faster#572 carries the same idea into
FastAndwith three or more inputs: the input with the fewest containers drives, and a key is probed for a shared value before any containers are intersected, so a key with nothing in common allocates nothing.Both are ready for review.