-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
time: reduce timer drop contention with per-worker timers #7668
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
Open
yakryder
wants to merge
2
commits into
tokio-rs:master
Choose a base branch
from
yakryder:reduce-timer-contention
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+182
−1
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This benchmark demonstrates the mutex contention issue described in tokio-rs#6504, specifically focusing on the drop path for timers that are registered but never fire. The benchmark creates 10,000 sleep timers, polls each once to initialize and register it with the timer wheel, then drops them before they fire. This simulates the common case of timeouts that don't fire (e.g., operations that complete before their timeout). Baseline results show severe contention: the 8-worker case is only ~1.5x faster than single-threaded. Refs: tokio-rs#6504
Reduces lock contention in timer operations by registering timers in a per-worker HashMap for the multi-threaded runtime, while falling back to the global timer wheel for current_thread runtime and block_in_place. Benchmark results (benches/time_drop_sleep_contention.rs): - Single-threaded: 33.3ms → 32.7ms (no regression) - Multi-threaded (8 workers): 21.6ms → 16.0ms (25.9% faster) Refs tokio-rs#6504
Darksonn
reviewed
Oct 10, 2025
Comment on lines
+1075
to
+1081
fn fire_expired_timers(&mut self, now: Instant) { | ||
self.timers.retain(|&deadline, wakers| { | ||
(now < deadline) || { | ||
wakers.drain(..).for_each(Waker::wake); | ||
false | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a very expensive operation.
ADD-SP
reviewed
Oct 11, 2025
/// This is called from TimerEntry::poll_elapsed when a timer is registered. | ||
/// The waker will be fired when fire_expired_timers() is called with a time | ||
/// >= deadline. | ||
pub(crate) fn register_timer(&mut self, deadline: Instant, waker: Waker) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apart from issues of O(n)
time complexity, how to de-register a timer?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
A-tokio
Area: The main tokio crate
M-time
Module: tokio/time
R-loom-multi-thread
Run loom multi-thread tests on this PR
R-loom-time-driver
Run loom time driver tests on this PR
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.
Ref #6504
Reduces lock contention in timer operations by registering timers in a per-worker HashMap for the multi-threaded runtime, while falling back to the global timer wheel for current_thread runtime and block_in_place contexts.
Benchmark
Added
benches/time_drop_sleep_contention.rs
to measure timer drop performance: