4 releases (2 breaking)

Uses old Rust 2015

0.3.0 Feb 27, 2018
0.2.0 Dec 10, 2017
0.1.1 Dec 2, 2017
0.1.0 Nov 25, 2017

#16 in #binary-heap

Download history 40/week @ 2023-12-23 24/week @ 2023-12-30 37/week @ 2024-01-06 70/week @ 2024-01-13 140/week @ 2024-01-20 137/week @ 2024-01-27 24/week @ 2024-02-03 25/week @ 2024-02-17 52/week @ 2024-02-24 97/week @ 2024-03-02 70/week @ 2024-03-09 32/week @ 2024-03-16 74/week @ 2024-03-23 110/week @ 2024-03-30 61/week @ 2024-04-06

302 downloads per month
Used in tquic

Apache-2.0

16KB
301 lines

Build Status

API Documentation

A timer system that uses a binary heap to order timers by expiration time.


lib.rs:

A binary heap based timer system with fast deletes.

Timers are maintained in a binary heap ordered by expiration time. In order to prevent O(n) deletes, keys are also stored in an active map. A timer is only active when when it exists in the active map. When a timer is deleted, it is only removed from the active map . When expire is called the key for a timer is only returned if it exists in the active map.

In order to differentiate multiple adds of the same key, so that when a key is deleted and re-added all previously non-expired keys don't become active (since they remain in the heap), each heap entry and active entry is attached to a montonically increasing counter. This ensures only truly active versions of the same key can be expired.

In order to provide this fast delete functionality, there is extra cpu overhead from hashmap lookups during expiry and time_remaining functionality. There is also extra memory usage for the duplicate keys and monotonic counters maintained in each heap entry and active map value. Note that when a lot of keys are added and deleted, expiry can become long if those keys are in the front of the heap and also expired, as they need to be scanned over to get to a valid expired value. However, this may be fine depending upon use case, as it's possible the keys cancelled will be evenly distriuted throughout the heap, and we only scan through expiry until we reach a non-expired time, whether the key is active or not. The tradeoff made for the user is whether they want to do any scans during deletes in order to save memory and cpu during expiry and time_remaining functionality.

No runtime deps