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

removes offset > 0 branches from WeightedShuffle implementation #4128

Merged
merged 1 commit into from
Dec 16, 2024

Conversation

behzadnouri
Copy link

@behzadnouri behzadnouri commented Dec 15, 2024

Problem

Changing the offset definition to:

let offset = (index - 1) & BIT_MASK

will avoid all the

if offset > 0 { /* ... */ }

branches and massively simplifies the implementation.
It also improves performance in the benchmarks:

On master:

test bench_weighted_shuffle_collect ... bench:     150,789.57 ns/iter (+/- 2,123.07)
test bench_weighted_shuffle_new     ... bench:      33,039.23 ns/iter (+/- 1,696.02)
test bench_weighted_shuffle_shuffle ... bench:     140,193.88 ns/iter (+/- 2,739.95)

On the branch:

test bench_weighted_shuffle_collect ... bench:     145,451.45 ns/iter (+/- 1,307.71)
test bench_weighted_shuffle_new     ... bench:      30,309.99 ns/iter (+/- 3,577.51)
test bench_weighted_shuffle_shuffle ... bench:     136,634.04 ns/iter (+/- 3,124.60)

Summary of Changes

removed offset > 0 branches from WeightedShuffle implementation

@behzadnouri behzadnouri force-pushed the weighted-shuffle-rm-branch branch from e0c6f1c to 67ae6c4 Compare December 15, 2024 23:13
gregcusack
gregcusack previously approved these changes Dec 16, 2024
Copy link

@gregcusack gregcusack left a comment

Choose a reason for hiding this comment

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

lgtm!

Changing the offset definition to:

    let offset = (index - 1) & BIT_MASK

will avoid all the

    if offset > 0 { /* ... */ }

branches and massively simplifies the implementation.
It also improves performance in the benchmarks:

On master:

    test bench_weighted_shuffle_collect ... bench:     150,789.57 ns/iter (+/- 2,123.07)
    test bench_weighted_shuffle_new     ... bench:      33,039.23 ns/iter (+/- 1,696.02)
    test bench_weighted_shuffle_shuffle ... bench:     140,193.88 ns/iter (+/- 2,739.95)

On the branch:

    test bench_weighted_shuffle_collect ... bench:     145,451.45 ns/iter (+/- 1,307.71)
    test bench_weighted_shuffle_new     ... bench:      30,309.99 ns/iter (+/- 3,577.51)
    test bench_weighted_shuffle_shuffle ... bench:     136,634.04 ns/iter (+/- 3,124.60)
Copy link

@gregcusack gregcusack left a comment

Choose a reason for hiding this comment

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

lgtm!

@behzadnouri behzadnouri merged commit 2fe885a into anza-xyz:master Dec 16, 2024
40 checks passed
@behzadnouri behzadnouri deleted the weighted-shuffle-rm-branch branch December 16, 2024 21:04
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