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

time: clean up redundant check in Wheel::poll() #5574

Merged
merged 2 commits into from
Mar 23, 2023

Conversation

marcelo-gonzalez
Copy link
Contributor

The condition checked in the and_then() call is the same as is checked in the match below, so we can clean it up by just matching on next_expiration() directly.

The condition checked in the and_then() call is the same as is checked
in the match below, so we can clean it up by just matching on
next_expiration() directly.
@github-actions github-actions bot added the R-loom Run loom tests on this PR label Mar 22, 2023
@marcelo-gonzalez
Copy link
Contributor Author

also the comment looks like it referred to the code that existed before the PR that added it? https://github.com/tokio-rs/tokio/pull/2905/files#diff-f0d9f5707877769d0883b4982ca9788bd07fe6d4b7bee13549ad9ffe29475d73L141-L149

Copy link
Member

@hawkw hawkw left a comment

Choose a reason for hiding this comment

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

is this actually equivalent? if expiration.deadline is greater than now, the and_then closure returns None, so the value of the variable expiration is None. in the match, if expiration is None, we call set_elapsed and skip popping from the list of pending timers. after this change, the value of expiration will now be Some when entering the match, and we will execute a different match arm that doesn't call set_elapsed, which changes the behavior. this doesn't seem correct to me...

@marcelo-gonzalez
Copy link
Contributor Author

oh good call my bad!

@hawkw
Copy link
Member

hawkw commented Mar 22, 2023

i believe what we actually should do is remove the Some(ref expiration) if expiration.deadline > now match arm, because it would previously never have executed at all (since when the deadline is greater than now, we set the value of expiration to None). this could potentially be replaced with a debug_assert!(expiration.deadline <= now) in the Some match arm...

@marcelo-gonzalez
Copy link
Contributor Author

marcelo-gonzalez commented Mar 22, 2023

i believe what we actually should do is remove the Some(ref expiration) if expiration.deadline > now match arm, because it would previously never have executed at all (since when the deadline is greater than now, we set the value of expiration to None). this could potentially be replaced with a debug_assert!(expiration.deadline <= now) in the Some match arm...

@hawkw Yeah I just messed it up here... but I think what I should have written was

diff --git a/tokio/src/runtime/time/wheel/mod.rs b/tokio/src/runtime/time/wheel/mod.rs
index c3ba3643..bf13b7b2 100644
--- a/tokio/src/runtime/time/wheel/mod.rs
+++ b/tokio/src/runtime/time/wheel/mod.rs
@@ -148,23 +148,13 @@ impl Wheel {
                 return Some(handle);
             }
 
-            // under what circumstances is poll.expiration Some vs. None?
-            let expiration = self.next_expiration().and_then(|expiration| {
-                if expiration.deadline > now {
-                    None
-                } else {
-                    Some(expiration)
-                }
-            });
-
-            match expiration {
-                Some(ref expiration) if expiration.deadline > now => return None,
-                Some(ref expiration) => {
+            match self.next_expiration() {
+                Some(ref expiration) if expiration.deadline <= now => {
                     self.process_expiration(expiration);
 
                     self.set_elapsed(expiration.deadline);
                 }
-                None => {
+                _ => {
                     // in this case the poll did not indicate an expiration
                     // _and_ we were not able to find a next expiration in
                     // the current list of timers.  advance to the poll's

@hawkw
Copy link
Member

hawkw commented Mar 22, 2023

Hmm, that diff also doesn't look like it actually implements the current behavior, as the case where the expiration's deadline is greater than now is still handled by the Some(ref expiration) match arm, which results in us processing expirations at that deadline (which is in the future).

Did you mean something like this?

            match expiration {
                Some(ref expiration) if expiration.deadline <= now => {
                    self.process_expiration(expiration);

                    self.set_elapsed(expiration.deadline);
                }
                _ => {
                    // in this case the poll did not indicate an expiration
                    // _and_ we were not able to find a next expiration in
                    // the current list of timers.  advance to the poll's
                    // current time and do nothing else.
                    self.set_elapsed(now);
                    break;
                }
            }

@hawkw
Copy link
Member

hawkw commented Mar 22, 2023

Ah, yup, the edit you just made to the diff looks correct --- looks like we noticed the same thing :)

@marcelo-gonzalez
Copy link
Contributor Author

oof yeah I must be low on sleep today, updated. That's what I meant indeed :D

@marcelo-gonzalez
Copy link
Contributor Author

Ah, yup, the edit you just made to the diff looks correct --- looks like we noticed the same thing :)

@hawkw worth reopening this w/ that diff do you think?

@hawkw
Copy link
Member

hawkw commented Mar 22, 2023

I think it's worth reopening, the current code is certainly a lot harder to understand than I think it needs to be (as illustrated by the amount of tries it took us to get this right...:slightly_smiling_face:)

@Darksonn Darksonn added A-tokio Area: The main tokio crate M-time Module: tokio/time labels Mar 23, 2023
Copy link
Member

@hawkw hawkw left a comment

Choose a reason for hiding this comment

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

This looks good to me, thanks for cleaning this up!

@hawkw hawkw requested a review from carllerche March 23, 2023 16:27
@carllerche carllerche merged commit 1cb7bf1 into tokio-rs:master Mar 23, 2023
crapStone pushed a commit to Calciumdibromid/CaBr2 that referenced this pull request Mar 29, 2023
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [tokio](https://tokio.rs) ([source](https://github.com/tokio-rs/tokio)) | dependencies | minor | `1.26.0` -> `1.27.0` |
| [tokio](https://tokio.rs) ([source](https://github.com/tokio-rs/tokio)) | dev-dependencies | minor | `1.26.0` -> `1.27.0` |

---

### Release Notes

<details>
<summary>tokio-rs/tokio</summary>

### [`v1.27.0`](https://github.com/tokio-rs/tokio/releases/tag/tokio-1.27.0): Tokio v1.27.0

[Compare Source](tokio-rs/tokio@tokio-1.26.0...tokio-1.27.0)

##### 1.27.0 (March 27th, 2023)

This release bumps the MSRV of Tokio to 1.56. ([#&#8203;5559])

##### Added

-   io: add `async_io` helper method to sockets ([#&#8203;5512])
-   io: add implementations of `AsFd`/`AsHandle`/`AsSocket` ([#&#8203;5514], [#&#8203;5540])
-   net: add `UdpSocket::peek_sender()` ([#&#8203;5520])
-   sync: add `RwLockWriteGuard::{downgrade_map, try_downgrade_map}` ([#&#8203;5527])
-   task: add `JoinHandle::abort_handle` ([#&#8203;5543])

##### Changed

-   io: use `memchr` from `libc` ([#&#8203;5558])
-   macros: accept path as crate rename in `#[tokio::main]` ([#&#8203;5557])
-   macros: update to syn 2.0.0 ([#&#8203;5572])
-   time: don't register for a wakeup when `Interval` returns `Ready` ([#&#8203;5553])

##### Fixed

-   fs: fuse std iterator in `ReadDir` ([#&#8203;5555])
-   tracing: fix `spawn_blocking` location fields ([#&#8203;5573])
-   time: clean up redundant check in `Wheel::poll()` ([#&#8203;5574])

##### Documented

-   macros: define cancellation safety ([#&#8203;5525])
-   io: add details to docs of `tokio::io::copy[_buf]` ([#&#8203;5575])
-   io: refer to `ReaderStream` and `StreamReader` in module docs ([#&#8203;5576])

[#&#8203;5512]: tokio-rs/tokio#5512

[#&#8203;5514]: tokio-rs/tokio#5514

[#&#8203;5520]: tokio-rs/tokio#5520

[#&#8203;5525]: tokio-rs/tokio#5525

[#&#8203;5527]: tokio-rs/tokio#5527

[#&#8203;5540]: tokio-rs/tokio#5540

[#&#8203;5543]: tokio-rs/tokio#5543

[#&#8203;5553]: tokio-rs/tokio#5553

[#&#8203;5555]: tokio-rs/tokio#5555

[#&#8203;5557]: tokio-rs/tokio#5557

[#&#8203;5558]: tokio-rs/tokio#5558

[#&#8203;5559]: tokio-rs/tokio#5559

[#&#8203;5572]: tokio-rs/tokio#5572

[#&#8203;5573]: tokio-rs/tokio#5573

[#&#8203;5574]: tokio-rs/tokio#5574

[#&#8203;5575]: tokio-rs/tokio#5575

[#&#8203;5576]: tokio-rs/tokio#5576

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about these updates again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNS4yNC41IiwidXBkYXRlZEluVmVyIjoiMzUuMjQuNSJ9-->

Co-authored-by: cabr2-bot <cabr2.help@gmail.com>
Reviewed-on: https://codeberg.org/Calciumdibromid/CaBr2/pulls/1838
Reviewed-by: crapStone <crapstone@noreply.codeberg.org>
Co-authored-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
Co-committed-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants