New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimized autoMemoryFreed loop #3244
Conversation
Good work Dvir! It's much better to optimize for releasing the last element, without to mention how bad it is to traverse it all after we found the element :-) Will review and merge soon. |
+1 |
@antirez Following our IRC talk I'll optimize it even a bit further: check head and tail first, then loop backwards |
An alternative and more complex approach - #3245 |
Thanks @dvirsky, I merged this simpler implementation because the other looked too complex (by attempting to track the bottom element). However zig-zag scanning, as you suggested, or just checking the last element without caring about tracking the bottom element, looks like a good idea and can implement in a few lines of code, I'll try to add it and report here in a minute. |
Possible implementation here: f2dbc02 |
Oh, I see only know that you switch the elements inside the queue, probably no longer needed. Checking better in a moment. |
@antirez looks good to me |
why is not not needed? you'd still grow the queue unnecessarily if the user allocates and frees constantly. |
Yes makes sense but it can leave holes (not just after your patch, it used to be like that) so I was thinking about adding this:
|
My fear is in general that the switch thing reshuffles the array in bad ways compared to the access pattern. Example: we usually free the last element allocated, but at some time we free an element in the middle. |
However it looks like a tradeoff, because instead in random deallocations in the middle, swapping reduces the queue length and allows for faster scanning. |
my approach would never leave holes. If we removed the top element - we just decrease the queue size, else we fill the hole with the top element. the switching is indeed a tradeoff but I think in real world use cases you'll either remove from the top or bottom, most likely from the top, so the zigzag scanning takes care of this. |
Yes you are right, sorry, so basically swapping never give holes but shuffles the array. However as you suggest, it only shuffles the array when the access pattern is random to start with, so we would anyway be in O(N) territory. Ok I think we are good, thank you for clarifying. |
Optimized autoMemoryFreed loop
Optimized autoMemoryFreed loop
This fixes two issues:
break
after marking the element as freed, making it slow as the element list grew.In a huge SCAN loop adding this made it run X100 faster