Skip to content

perf(communication): Avoid shared queue-header reads and add opt-in idle backoff - #180

Merged
Bronek merged 7 commits into
mainfrom
bronek/optimize_MPMC_queue
Sep 22, 2026
Merged

Bronek merged 7 commits into
mainfrom
bronek/optimize_MPMC_queue

Conversation

@Bronek

@Bronek Bronek commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

Derive consumer epochs from the cached ring mask, removing a shared-header load and integer division in ConsumerBare. This applies to both MPMC and SPMC.

The header mask shares a cache line with the producer counter. A consumer reading the mask can cause that line to be shared; the producer’s next counter update must obtain exclusive ownership and invalidate the consumer’s copy. The read itself does not invalidate the producer’s copy. Using the cached mask avoids this source of false sharing. The power-of-two ring length also allows a shift instead of division. These measurements evaluate both changes together; they do not attribute a percentage of the gain to either one.

There is no idle backoff in this PR despite to what the subject line says - it was removed in the last commit

@Bronek
Bronek requested a review from a team September 18, 2026 16:16
Comment thread crates/flux-communication/src/queue/mod.rs Outdated
Comment thread crates/flux/src/tile/mod.rs Outdated

@louisponet louisponet left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would like to know whether it's actually the self.mask and not just the div. Can you compare your benches with only the div change (mainly for my curiosity)

Comment thread crates/flux-communication/src/queue/tests_basic.rs Outdated
Comment thread crates/flux-communication/src/queue/tests_collaborative.rs Outdated
Comment thread crates/flux/tests/e2e.rs Outdated
@Bronek
Bronek force-pushed the bronek/optimize_MPMC_queue branch from adb9817 to 3cead80 Compare September 21, 2026 09:26
Comment thread crates/flux-communication/src/queue/mod.rs Outdated
Use the consumer's cached ring mask and a power-of-two shift when updating
its position and expected seqlock epoch. This avoids reading the header
cache line shared with the producers' count and removes the division.
Retain cursor atomics and make the old epoch helper test-only.

Cover broadcast epoch stamps, wraparound and recovery, plus collaborative
claims with changing consumer polling order. Negative controls reject an
incorrect epoch and duplicate claims. Workspace tests with all features,
check, Clippy, formatting and dependency checks pass.

Nine paired runs on isolated Ryzen 9 9950X cores 12-15 measured 20-35% less
time per message for 2/3 producers and 8/64-byte payloads. The one-producer
64-byte case regressed 33.7%; the improvement is workload-dependent.

Assisted-by: Codex:GPT-6
Add TileConfig::with_idle_backoff with a zero-pause default. Spin only
when the whole iteration recorded no work, after the stop check;
eligible parking takes precedence. Queue reads remain nonblocking.

A drain-to-empty benchmark with two MPMC producers measured 27.55 to
25.79 ns/message for 64-byte payloads at 16 pauses, and 36.25 to 33.66
for 256-byte payloads. Sparse 64-byte SPMC p99 rose from 100 to 280 ns,
so backoff is opt-in and is not a general fix for queue regressions.

Test delivery after idle and scope shutdown with 0, 16 and 32 pauses.
Active-iteration suppression and park precedence were reviewed by
inspection; the test does not assert scheduler-dependent timings.

Assisted-by: Codex:GPT-6
Limit the pause count to 0–255 in TileConfig and its builder method.

Assisted-by: Codex:GPT-6
The unpublished-slot race test still called the removed version_at helper.
Subscribe a probe before reserving the slot and verify that it reports Empty.

Assisted-by: Codex:GPT-6
Remove the idle-backoff configuration and pause loop, restoring the prior
parking flow. Keep PR #180 focused on the cached-mask queue optimization;
backoff can be considered in a separate PR.

Validated flux tests with default and all features, workspace all-feature
compilation, formatting, and Clippy with warnings denied.

Assisted-by: Codex:GPT-6
@Bronek
Bronek force-pushed the bronek/optimize_MPMC_queue branch from 73c0ea6 to 4bb5a7f Compare September 22, 2026 12:58
@Bronek
Bronek requested a review from gd-0 September 22, 2026 12:59
@Bronek
Bronek merged commit cb84386 into main Sep 22, 2026
3 checks passed
@Bronek
Bronek deleted the bronek/optimize_MPMC_queue branch September 22, 2026 16:28
@Bronek

Bronek commented Sep 22, 2026

Copy link
Copy Markdown
Contributor Author

To future readers - there is no idle backoff in this PR. It was removed in the last commit but I forgot to update the topic line, and the merge commit says "idle backoff" when in fact it is not there. Sorry everyone 😭

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.

3 participants