time: delay the Arc::clone of the scheduler::Handle until registering timer#7461
Closed
ADD-SP wants to merge 14 commits into
Closed
time: delay the Arc::clone of the scheduler::Handle until registering timer#7461ADD-SP wants to merge 14 commits into
Arc::clone of the scheduler::Handle until registering timer#7461ADD-SP wants to merge 14 commits into
Conversation
There are two usage of this handle for timer: 1. Ensure the time driver is enabled. 2. Registering or clear the entry from the global wheel. For (1), we just need the `&Handle`, no need to make a clone. For (2), we can delay the `.clone()` until we are about to register the entry. Delaying the `Arc::clone` improves the performance on multi-core machine. Signed-off-by: ADD-SP <qiqi.zhang@konghq.com>
Signed-off-by: ADD-SP <qiqi.zhang@konghq.com>
Signed-off-by: ADD-SP <qiqi.zhang@konghq.com>
ADD-SP
commented
Jul 16, 2025
Member
Author
There was a problem hiding this comment.
For reviewers
Hiding whitespace reduces diffs.
ADD-SP
commented
Jul 16, 2025
ADD-SP
commented
Jul 16, 2025
ADD-SP
commented
Jul 16, 2025
ADD-SP
commented
Jul 16, 2025
Signed-off-by: ADD-SP <qiqi.zhang@konghq.com>
ADD-SP
commented
Jul 16, 2025
Arc::clone of the scheduler::Handle until registering timer
Signed-off-by: ADD-SP <qiqi.zhang@konghq.com>
ADD-SP
commented
Jul 20, 2025
Comment on lines
+258
to
+259
| let is_time_enabled = scheduler::Handle::with_current(|hdl| hdl.driver().time.is_some()); | ||
| assert!(is_time_enabled, "{TIME_DISABLED_ERROR}"); |
Member
Author
There was a problem hiding this comment.
For reviewers
Triggering panic inside the closure doesn't report the panic location of the caller, so we need to panic outside the closure.
ADD-SP
commented
Jul 20, 2025
Member
Author
|
superseded by #7473 |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
blocks #7467
Background
This improvement was found while working on the delayed cancellation (#7384),
Since I don't like to include a un-relevant change into a big patch, I made it a separate commit
This might be a low-hanging fruit.
Motivation
tokio/tokio/src/time/sleep.rs
Lines 250 to 256 in 0a3fe46
The current implementation always clone the
scheduler::Handlefor each timer, even this timer is not registered.There are two usage of this handle for timer:
tokio/tokio/src/runtime/time/entry.rs
Lines 480 to 484 in 0a3fe46
tokio/tokio/src/runtime/time/entry.rs
Lines 590 to 595 in 0a3fe46
For (1), A
&Handleis enough, no need to make a clone.For (2), we can delay the
.clone()until we are about to register the entry.Delaying the
Arc::cloneimproves the performance on multi-core machine.Solution
&schedule:::Handlein theTimerShared.Benchmark (AMD64 16 cores)