Skip to content

Remove the modulo operations in spsc#652

Open
sgued wants to merge 1 commit intorust-embedded:mainfrom
sgued:rem-perf
Open

Remove the modulo operations in spsc#652
sgued wants to merge 1 commit intorust-embedded:mainfrom
sgued:rem-perf

Conversation

@sgued
Copy link
Copy Markdown
Contributor

@sgued sgued commented Apr 2, 2026

These modulo operations used to be well optimized when N was a power of 2 However, the consumer and producer now use view-types that make N runtime dependant, preventing the compiler from optimizing these modulo operations even when N is always a power of 2.

This patch leverages the fact that head and tail are always kept lower than N to replace the modulo operations with a simple if, which gets optimized pretty well by the compiler and no branch is left.

Closes #650

These modulo operations used to be well optimized when N was a power of 2
However, the consumer and producer now use view-types that make N runtime
dependant, preventing the compiler from optimizing these modulo operations
even when N is always a power of 2.

This patch leverages the fact that `head` and `tail` are always kept lower
than N to replace the modulo operations with a simple if, which gets optimized
pretty well by the compiler and no branch is left.

Closes rust-embedded#650
@sgued
Copy link
Copy Markdown
Contributor Author

sgued commented Apr 2, 2026

I wish we had some benchmarking infra in place. This would help me make sure this actually solves the problem and would have helped detect it in the first place.

@sgued
Copy link
Copy Markdown
Contributor Author

sgued commented Apr 2, 2026

Clippy lint fixes are already included in #644

@sgued
Copy link
Copy Markdown
Contributor Author

sgued commented Apr 2, 2026

@823984418 does this fix your performance issues?

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.

The %(rem) operation seems to cause unexpected overhead.

1 participant