Prevent timer wheel from growing unexpectedly #238
+34
−81
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
A client cancelling and inserting timers at high rates could result in the used heap to grow unexpectedly.
This can for example happen if a compositor tracks inactivity using a timer and receives a lot of pointer events in a single dispatch.
Currently
TimerWheel::cancelcan result in just marking a timer cancelled without the timer being actually removedfrom the heap. So there might be a time-frame where the heap grows quite big holding mostly cancelled timers.
TimerWheel::next_expiredis expected to clean-up the cancelled timers, but at this point the heap might have alreadygrown quite big and
BinaryHeap::popwill not implicitly shrink its backing store.Also I am not completely sure this will catch all usage patterns as
TimerWheel::insertcould result in re-orderingof the heap.
(Note: Calling shrink might also bring the memory consumption down, but this might result in constant re-allocation)
Calling
TimerWheel::cancelmight already result in linear search in the heap to mark a timer cancelled,so imo using
retaindoes not make this worse but should keep the heap at a reasonable size.This might also have the side-effect of making some operations like next_expired a bit faster for some use-cases.
fixes #233