Skip to content
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

EventLoop: store Timers in min Pairing Heap #15206

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

ysbaddaden
Copy link
Contributor

Replaces the Deque used in #14996 for a min Pairing Heap which is a kind of Mergeable Heap and is one of the best performing heap in practical tests when arbitrary deletions are required (think cancelling a timeout), otherwise a D-ary Heap (e.g. 4-heap) will usually perform better. See the A Nearly-Tight Analysis of Multipass Pairing Heaps paper or the Wikipedia page for more details.

The implementation itself is based on the Pairing Heaps: Experiments and Analysis paper, and merely implements a recursive twopass algorithm (the auxiliary twopass might perform even better). The Crystal::PointerPairingList(T) type is generic and relies on intrusive nodes (the links are into T) to avoid extra allocations for the nodes (same as Crystal::PointerLinkedList(T)). It also requires a T#heap_compare method, so we can use the same type for a min or max heap, or to build a more complex comparison.

Note: I also tried a 4-heap, and while it performs very well and only needs a flat array, the arbitrary deletion (e.g. cancelling timeout) needs a linear scan and its performance quickly plummets, even at low occupancy, and becomes painfully slow at higher occupancy (tens of microseconds on each delete, while the pairing heap does it in tens of nanoseconds).

Follow up to #14996

Builds on top of #15205, you should open the last commit for just the pairing heap.

ysbaddaden and others added 5 commits November 19, 2024 17:13
The main advantage is the ability to grow on demand and avoid a large
single contiguous mmap of memory that needs to preallocates most of the
memory.

We also don't need to remember the maximum reached index anymore, which
is replaced with a `#each_index` method (only needed to allow `fork`).
Co-authored-by: Johannes Müller <straightshoota@gmail.com>
@ysbaddaden ysbaddaden changed the title Store Timers in Min Pairing Heap (lifetime Event Loop) EventLoop: store Timers in min Pairing Heap Nov 19, 2024
@ysbaddaden ysbaddaden self-assigned this Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant