-
-
Notifications
You must be signed in to change notification settings - Fork 706
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
Faster heap operations #3933
Faster heap operations #3933
Conversation
|
I'm afraid this is my handy work. This technique is used in Bottom-Up Heap Sort because it performs nearly half the number of comparisons on average. The upside is that it should be faster when comparisons are expensive but the downside is that it's less cache friendly because it always sifts down all the way to a leaf node before sifting up. |
|
The algorithm could be selected at compile time - use the sift down method for basic types known to have fast comparison operators (integers, floating point, pointers, etc.), but keep the current two-stage method for I don't know that it's worth maintaining the extra lines of code, though. |
|
@Xinok OK, I figured there must be some depth to it. So the approach trades comparisons for swaps. What data types did you measure improvements on? I figure percolate() is sensible when there's not a lot of percolation back up, i.e. when you're actually percolating the extremum. During heap building however, you're percolating an arbitrary element so it makes sense to just go with siftDown. So I kept percolate in heapSort and siftDown in buildHeap. Makes sense? With this last commit I eliminated one test on the common path in both siftDown and percolate. |
|
@andralex I don't remember exactly but most likely an array of integers. However, the benchmarks were for completely sorting the array. Here are some new benchmarks for just building the heap: ~4 million integers. ~1 million strings: For building the heap, the bottom-up method is slower and doesn't result in a significant reduction of comparisons, so I would say the sift-down method is better. But for sorting, what I said before still applies. |
|
@Xinok Great, thanks! So this pull should be in good order. |
|
this blocks #3934 |
|
LGTM |
|
Auto-merge toggled on |
I was surprised that building and sorting the heap call percolate which in turn has two stages - going up and going down. To the best of my knowledge, all you need to do to build a heap is to sift elements down (not up). @Xinok could you please comment?