Skip to content
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

remove frequent global is_shutdown flag check on timer operations #5301

Closed

Conversation

HyeonuPark
Copy link
Contributor

@HyeonuPark HyeonuPark commented Dec 17, 2022

Make timer driver's is_shutdown flag nonatomic and move it into the driver lock. On shtudown, the driver wake the all pending timer with shutdown error.

Simillar to the #5300, but for timer driver not io driver.

Refs: #5227, #5300

Motivation

The idea is based on @carllerche's suggestion, and @Darksonn mentioned that the timer driver has a similar issue.

Solution

It's virtually same change with #5300 but for this case the flag was already an atomic flag so I'm not sure how much perf improvement it actually brings. I think it's worth on its own having same structure between timer and io drivers though. I tried to reuse benchmark I've used for #5300 by wrapping each tasks with select!{biased; _ = sleep(1h) but it doesn't show any noticeable difference, which is not surprising as it's comparing atomic loads with (nonblocking) IO.

Edit: "I believe it's worth" -> "I think it's worth" since "believe" sounds too strong. At this point This PR is more like refactoring than optimization.

Make timer driver's is_shutdown flag nonatomic and move it into the driver lock.
On shtudown, the driver wake the all pending timer with shutdown error.

Simillar to the tokio-rs#5300, but for timer driver not io driver.

Refs: tokio-rs#5227, tokio-rs#5300
@github-actions github-actions bot added the R-loom Run loom tests on this PR label Dec 17, 2022
@Darksonn Darksonn added A-tokio Area: The main tokio crate M-time Module: tokio/time T-performance Topic: performance and benchmarks labels Dec 17, 2022
@@ -126,7 +126,7 @@ impl StateCell {
fn when(&self) -> Option<u64> {
let cur_state = self.state.load(Ordering::Relaxed);

if cur_state == u64::MAX {
if cur_state >= STATE_MIN_VALUE {
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems like you've also included a fix for #5082 here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems so, though I discovered the issue just now. During working on this issue I messed up more in this module and cleared up most of them but this line survived. If you think it's better to not mix #5082 here I'll revert it.

Comment on lines -96 to -97
/// True if the driver is being shutdown.
pub(super) is_shutdown: AtomicBool,
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't remember this being an atomic, and I assumed it wasn't when I made the comment that the time driver has a similar issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense. It's questionable that this change improves performance at all. TBH I have no idea how to benchmark timers only.

@Darksonn
Copy link
Contributor

I'll close this because it seems like it wasn't necessary after all.

@Darksonn Darksonn closed this Nov 25, 2023
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 Run loom tests on this PR T-performance Topic: performance and benchmarks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants