-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
time: delay the Arc::clone
until registering timer
#7473
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
base: master
Are you sure you want to change the base?
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 <[email protected]>
b66b050
to
023e956
Compare
We will need to consider the changes to behavior. What if creation and first poll happen on two different runtimes? Maybe the change is acceptable, but we should be aware of what we are changing. |
@Darksonn Thanks for you reminder, I forgot to highlight this part, I have updated the PR description to highlight the changed behavior. I may missed other cases, feel free to point it out. Strictly speaking, this is a breaking change, but I wouldn't expect an reasonable program relies on it. What do you think? |
I think the most significant change is whether creating a Going from panic to no panic is an acceptable change IMO, but I think that the reverse would be more concerning because it introduces panics into programs that used to work. This means that the change is acceptable, but difficult to reverse. |
This comment was marked as outdated.
This comment was marked as outdated.
It looks like there is a test for it: tokio/tokio/tests/time_sleep.rs Lines 173 to 181 in 911ab21
If that still passes with your change, I guess it already panics when created outside of a runtime? |
Ah, yes, this PR also panic, my mind is a little messy. Lines 256 to 259 in 5de7139
|
Signed-off-by: ADD-SP <[email protected]>
Hi @Darksonn , do you have other concerns about the behavior changes introduced by this PR? I'm not rushing anything, behavior changes are always tricky for projects like tokio, it is valuable to do more discussions. Since I don't want to block #7467 for a long time, if you would like to do more discussion about the behavior changes, I can rollback #7467 to the existing behavior to make it ready for review. |
The behavior change is probably okay, but I wonder whether this is really worth it. Yes, the percentage changes are impressive, but in reality they correspond to a few nanoseconds for an atomic increment/decrement. |
@Darksonn This is a reasonable point, the figures in real world should not look like that, let's hold on the merge button for now. I think our concern points out that we currently don't have a proper benchmark script for the time subsystem, It is the time for me to workout a proper benchmark script, I think it would also be useful for #7467. I will rollback #7467 to the existing behavior and make it ready for review. |
Added the |
Converted to draft, waiting benchmark. |
This reworks the #7461 for less diff, also blocks #7467.
Blocked by #7473 (comment),
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::Handle
for 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
&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
schedule:::Handle
in theTimerShared
.Behavior changes
In summary, once the runtime that creates the timer is shutdown,
However, if the Runtime ② didn't
enable_time
, the new impl will panic at the first poll.Benchmark (AMD64 16 cores)
We need to work out a proper benchmark script for the time subsystem.