Skip to content

time: delay the Arc::clone of the scheduler::Handle until registering timer #7461

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

Closed
wants to merge 14 commits into from

Conversation

ADD-SP
Copy link
Member

@ADD-SP ADD-SP commented Jul 16, 2025

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

pub(crate) fn new_timeout(
deadline: Instant,
location: Option<&'static Location<'static>>,
) -> Sleep {
use crate::runtime::scheduler;
let handle = scheduler::Handle::current();
let entry = TimerEntry::new(handle, deadline);

The current implementation always clone the scheduler::Handle for each timer, even this timer is not registered.

There are two usage of this handle for timer:

  1. Ensure the time driver is enabled.
    impl TimerEntry {
    #[track_caller]
    pub(crate) fn new(handle: scheduler::Handle, deadline: Instant) -> Self {
    // Panic if the time driver is not enabled
    let _ = handle.driver().time();
  2. Registering or clear the entry from the global wheel.
    if reregister {
    unsafe {
    self.driver()
    .reregister(&self.driver.driver().io, tick, inner.into());
    }
    }

For (1), A &Handle is 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::clone improves the performance on multi-core machine.

Solution

  • Storing the &schedule:::Handle in the TimerShared.

Benchmark (AMD64 16 cores)

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>
@github-actions github-actions bot added R-loom-current-thread Run loom current-thread tests on this PR R-loom-multi-thread Run loom multi-thread tests on this PR R-loom-time-driver Run loom time driver tests on this PR labels Jul 16, 2025
@ADD-SP ADD-SP added A-tokio Area: The main tokio crate M-time Module: tokio/time T-performance Topic: performance and benchmarks R-loom-time-driver Run loom time driver tests on this PR R-loom-current-thread Run loom current-thread tests on this PR R-loom-multi-thread Run loom multi-thread tests on this PR and removed R-loom-time-driver Run loom time driver tests on this PR R-loom-current-thread Run loom current-thread tests on this PR R-loom-multi-thread Run loom multi-thread tests on this PR labels Jul 16, 2025
ADD-SP added 2 commits July 16, 2025 21:06
Signed-off-by: ADD-SP <qiqi.zhang@konghq.com>
Signed-off-by: ADD-SP <qiqi.zhang@konghq.com>
Copy link
Member Author

@ADD-SP ADD-SP Jul 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reviewers

Hiding whitespace reduces diffs.

@ADD-SP ADD-SP changed the title time: clone the schedluer handle at the time of registering the entry time: delay the Arc::clone of the scheduler::Handle until registering timer Jul 16, 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}");
Copy link
Member Author

@ADD-SP ADD-SP Jul 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ADD-SP closed this Jul 21, 2025
@ADD-SP ADD-SP deleted the add_sp/time-do-not-clone-arc branch July 21, 2025 12:51
@ADD-SP
Copy link
Member Author

ADD-SP commented Jul 21, 2025

superseded by #7473

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-current-thread Run loom current-thread tests on this PR R-loom-multi-thread Run loom multi-thread tests on this PR R-loom-time-driver Run loom time driver tests on this PR T-performance Topic: performance and benchmarks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant