A d-ary heap

I have created a d-ary heap in Zig, mostly as a learning exercise.

dheap.zig

To my surprise, this heap is almost twice as fast as the std.PriorityQueue binary heap for a benchmark where n elements are inserted into the heap and are all removed.

I think the major improvement in performance is coming from separating the (more common usually) case where you do not need to check for slice bounds when finding children in the “sift-down” function.

4 Likes

Here is the benchmark (using zBench)

1 Like

The performance benefit is unclear… I did some more tests and I currently have no idea why is it faster in this benchmark.