Skip to content

Use now_or_never for filling scheduler instead of Stream::next with timeout#734

Merged
phil-opp merged 2 commits intomainfrom
use-try-recv
Nov 5, 2025
Merged

Use now_or_never for filling scheduler instead of Stream::next with timeout#734
phil-opp merged 2 commits intomainfrom
use-try-recv

Conversation

@phil-opp
Copy link
Copy Markdown
Collaborator

@phil-opp phil-opp commented Dec 10, 2024

In #724 (comment), I proposed to use try_recv to avoid the timeout. Unfortunately this approach doesn't work because we implement Stream directly on EventStream. This makes it necessary to store a long-lived stream type in the EventStream struct instead of the underlying channel (as constructing the stream on the fly will not register the async waker properly).

So this PR now uses next().now_or_never() for the same effect.

@phil-opp phil-opp requested a review from haixuanTao December 10, 2024 15:44
Copy link
Copy Markdown
Collaborator

@haixuanTao haixuanTao left a comment

Choose a reason for hiding this comment

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

Ok with me!

But it seems that the python ros2 bridge is now failing.

@phil-opp
Copy link
Copy Markdown
Collaborator Author

phil-opp commented Nov 5, 2025

Rebased against main.

Benchmarks with cargo run --example benchmark --release on my machine:

this PR:

rust-sink: stdout    Latency:
rust-sink: stdout    size 0x0     : 150.983µs
rust-sink: stdout    size 0x8     : 127.49µs
rust-sink: stdout    size 0x40    : 131.712µs
rust-sink: stdout    size 0x200   : 141.501µs
rust-sink: stdout    size 0x800   : 143.563µs
rust-sink: stdout    size 0x1000  : 147.153µs
rust-sink: stdout    size 0x4000  : 170.582µs
rust-sink: stdout    size 0xa000  : 199.952µs
rust-sink: stdout    size 0x64000 : 134.381µs
rust-sink: stdout    size 0x3e8000: 156.102µs
rust-sink: stdout    Throughput:
rust-sink: stdout    size 0x0     : 117188 messages per second
rust-sink: stdout    size 0x8     : 86689 messages per second
rust-sink: stdout    size 0x40    : 80844 messages per second
rust-sink: stdout    size 0x200   : 74798 messages per second
rust-sink: stdout    size 0x800   : 69153 messages per second
rust-sink: stdout    size 0x1000  : 37298 messages per second
rust-sink: stdout    size 0x4000  : 19972 messages per second
rust-sink: stdout    size 0xa000  : 13510 messages per second
rust-sink: stdout    size 0x64000 : 3756 messages per second
rust-sink: stdout    size 0x3e8000: 810 messages per second

main:

rust-sink: stdout    Latency:
rust-sink: stdout    size 0x0     : 508.611µs
rust-sink: stdout    size 0x8     : 452.892µs
rust-sink: stdout    size 0x40    : 480.421µs
rust-sink: stdout    size 0x200   : 459.321µs
rust-sink: stdout    size 0x800   : 482.283µs
rust-sink: stdout    size 0x1000  : 497.282µs
rust-sink: stdout    size 0x4000  : 491.411µs
rust-sink: stdout    size 0xa000  : 476.312µs
rust-sink: stdout    size 0x64000 : 493.109µs
rust-sink: stdout    size 0x3e8000: 480.21µs
rust-sink: stdout    Throughput:
rust-sink: stdout    size 0x0     : 2756 messages per second
rust-sink: stdout    size 0x8     : 2766 messages per second
rust-sink: stdout    size 0x40    : 2770 messages per second
rust-sink: stdout    size 0x200   : 2758 messages per second
rust-sink: stdout    size 0x800   : 2761 messages per second
rust-sink: stdout    size 0x1000  : 2670 messages per second
rust-sink: stdout    size 0x4000  : 2176 messages per second
rust-sink: stdout    size 0xa000  : 2694 messages per second
rust-sink: stdout    size 0x64000 : 1623 messages per second
rust-sink: stdout    size 0x3e8000: 366 messages per second

@phil-opp
Copy link
Copy Markdown
Collaborator Author

phil-opp commented Nov 5, 2025

The ros2 error seems legit, looking into it

edit: should be fixed with 59c9be5

Constructing the `Stream` on the fly in `poll_next` doesn't work as dropping the stream will also drop the registered waker handle. So no wakeup happens anymore.

So we need to keep the long-lived `RecvStream` type, which doesn't have a `try_recv` method. However, we can use `next().now_or_never()` for the same effect.
@phil-opp phil-opp changed the title Use Receiver::try_recv instead of Stream::next with timeout Use now_or_never for filling scheduler instead of Stream::next with timeout Nov 5, 2025
@phil-opp phil-opp merged commit 72aec79 into main Nov 5, 2025
27 checks passed
@phil-opp phil-opp deleted the use-try-recv branch November 5, 2025 15:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants