-
Notifications
You must be signed in to change notification settings - Fork 1.6k
<algorithm>: Optimize sample() and shuffle() with Lemire's algorithm
#5735
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
Merged
StephanTLavavej
merged 12 commits into
microsoft:main
from
StephanTLavavej:shuffle-your-library
Sep 25, 2025
+259
−149
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
bcadc2a
Add benchmarks for sample() and shuffle().
StephanTLavavej 8c0cbe6
Move `_Rng_from_urng_v2` up to `<algorithm>`, no other changes.
StephanTLavavej 5fa2951
Move `_Has_static_min_max` up to `<algorithm>`, no other changes.
StephanTLavavej 30f3317
Extract `_Rng_from_urng_v1_or_v2`.
StephanTLavavej e62b977
Cleanup `_Rng_from_urng_v2`: `static constexpr` => `constexpr` for a …
StephanTLavavej ae1fa8a
Fix URBG usage: `min()` and `max()` must be `constexpr`.
StephanTLavavej 1829230
Fix URBG usage: Must provide `result_type`, must be unsigned integer.
StephanTLavavej ca969a9
Behavioral change: Use `_Rng_from_urng_v1_or_v2` to optimize `sample(…
StephanTLavavej f0c8420
Fix warning C4018: '<': signed/unsigned mismatch
StephanTLavavej bb03a9e
Fix warning C4365: 'initializing': conversion from 'int' to 'unsigned…
StephanTLavavej fedbd6c
Fix error C2397: conversion from 'const int' to 'unsigned __int64' re…
StephanTLavavej 01090a7
Fix warning C4018: '<': signed/unsigned mismatch
StephanTLavavej File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #include <algorithm> | ||
| #include <benchmark/benchmark.h> | ||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <numeric> | ||
| #include <random> | ||
| #include <type_traits> | ||
| #include <vector> | ||
| using namespace std; | ||
|
|
||
| enum class alg_type { std_fn, rng }; | ||
|
|
||
| template <class T, alg_type Alg> | ||
| void bm_sample(benchmark::State& state) { | ||
| static_assert(is_unsigned_v<T>, "T must be unsigned so iota() doesn't have to worry about overflow."); | ||
|
|
||
| const auto population_size = static_cast<size_t>(state.range(0)); | ||
| const auto sampled_size = static_cast<size_t>(state.range(1)); | ||
|
|
||
| vector<T> population(population_size); | ||
| vector<T> sampled(sampled_size); | ||
| iota(population.begin(), population.end(), T{0}); | ||
| mt19937_64 urbg; | ||
|
|
||
| for (auto _ : state) { | ||
| benchmark::DoNotOptimize(population); | ||
| if constexpr (Alg == alg_type::rng) { | ||
| ranges::sample(population, sampled.begin(), sampled_size, urbg); | ||
| } else { | ||
| sample(population.begin(), population.end(), sampled.begin(), sampled_size, urbg); | ||
| } | ||
| benchmark::DoNotOptimize(sampled); | ||
| } | ||
| } | ||
|
|
||
| void common_args(auto bm) { | ||
| bm->Args({1 << 20, 1 << 15}); | ||
| } | ||
|
|
||
| BENCHMARK(bm_sample<uint8_t, alg_type::std_fn>)->Apply(common_args); | ||
| BENCHMARK(bm_sample<uint16_t, alg_type::std_fn>)->Apply(common_args); | ||
| BENCHMARK(bm_sample<uint32_t, alg_type::std_fn>)->Apply(common_args); | ||
| BENCHMARK(bm_sample<uint64_t, alg_type::std_fn>)->Apply(common_args); | ||
|
|
||
| BENCHMARK(bm_sample<uint8_t, alg_type::rng>)->Apply(common_args); | ||
| BENCHMARK(bm_sample<uint16_t, alg_type::rng>)->Apply(common_args); | ||
| BENCHMARK(bm_sample<uint32_t, alg_type::rng>)->Apply(common_args); | ||
| BENCHMARK(bm_sample<uint64_t, alg_type::rng>)->Apply(common_args); | ||
|
|
||
| BENCHMARK_MAIN(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #include <algorithm> | ||
| #include <benchmark/benchmark.h> | ||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <numeric> | ||
| #include <random> | ||
| #include <type_traits> | ||
| #include <vector> | ||
| using namespace std; | ||
|
|
||
| enum class alg_type { std_fn, rng }; | ||
|
|
||
| template <class T, alg_type Alg> | ||
| void bm_shuffle(benchmark::State& state) { | ||
| static_assert(is_unsigned_v<T>, "T must be unsigned so iota() doesn't have to worry about overflow."); | ||
|
|
||
| const auto n = static_cast<size_t>(state.range(0)); | ||
| vector<T> v(n); | ||
| iota(v.begin(), v.end(), T{0}); | ||
| mt19937_64 urbg; | ||
|
|
||
| for (auto _ : state) { | ||
| benchmark::DoNotOptimize(v); | ||
| if constexpr (Alg == alg_type::rng) { | ||
| ranges::shuffle(v, urbg); | ||
| } else { | ||
| shuffle(v.begin(), v.end(), urbg); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void common_args(auto bm) { | ||
| bm->Arg(1 << 20); | ||
| } | ||
|
|
||
| BENCHMARK(bm_shuffle<uint8_t, alg_type::std_fn>)->Apply(common_args); | ||
| BENCHMARK(bm_shuffle<uint16_t, alg_type::std_fn>)->Apply(common_args); | ||
| BENCHMARK(bm_shuffle<uint32_t, alg_type::std_fn>)->Apply(common_args); | ||
| BENCHMARK(bm_shuffle<uint64_t, alg_type::std_fn>)->Apply(common_args); | ||
|
|
||
| BENCHMARK(bm_shuffle<uint8_t, alg_type::rng>)->Apply(common_args); | ||
| BENCHMARK(bm_shuffle<uint16_t, alg_type::rng>)->Apply(common_args); | ||
| BENCHMARK(bm_shuffle<uint32_t, alg_type::rng>)->Apply(common_args); | ||
| BENCHMARK(bm_shuffle<uint64_t, alg_type::rng>)->Apply(common_args); | ||
|
|
||
| BENCHMARK_MAIN(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.