-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Optimize std::includes by using ranges::includes approach
#5543
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 11 commits into
microsoft:main
from
AlexGuteniev:includes-ranges
Jun 14, 2025
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fd1d911
benchmark
AlexGuteniev 8199c12
speedup!
AlexGuteniev 01da45f
improve benchmark
AlexGuteniev 556fb65
modernize
AlexGuteniev 817c2bd
Merge branch 'main' into includes-ranges
StephanTLavavej c7d4c44
`enum needle_spread` => `enum class needle_spread`
StephanTLavavej 02fb75f
Include more headers.
StephanTLavavej 316fbd9
`s` => `spread`, `m` => `expected_match`
StephanTLavavej 8df5036
Avoid a 1-in-4-billion chance of UB.
StephanTLavavej 8796e35
Oh, that's why it was a plain enum.
StephanTLavavej dac8ad2
Reestablish potentially broken sorted precondition.
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,124 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #include <algorithm> | ||
| #include <benchmark/benchmark.h> | ||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <cstdlib> | ||
| #include <iostream> | ||
| #include <random> | ||
| #include <ranges> | ||
| #include <type_traits> | ||
| #include <vector> | ||
|
|
||
| #include "skewed_allocator.hpp" | ||
| #include "utility.hpp" | ||
|
|
||
| using namespace std; | ||
|
|
||
| enum class alg_type { std_fn, rng }; | ||
|
|
||
| enum class needle_spread { dense, dense_random, sparse, sparse_random }; | ||
|
|
||
| template <class T, alg_type Alg> | ||
| void bm_includes(benchmark::State& state) { | ||
| const auto hay_size = static_cast<size_t>(state.range(0)); | ||
| const auto needle_size = static_cast<size_t>(state.range(1)); | ||
| const auto spread = static_cast<needle_spread>(state.range(2)); | ||
| const auto expected_match = static_cast<bool>(state.range(3)); | ||
|
|
||
| auto hay = random_vector<T, not_highly_aligned_allocator>(hay_size); | ||
| ranges::sort(hay); | ||
|
|
||
| vector<T, not_highly_aligned_allocator<T>> needle; | ||
| switch (spread) { | ||
| case needle_spread::dense: | ||
| needle.assign(hay.begin() + hay_size / 2 - needle_size / 2, hay.begin() + hay_size / 2 + (needle_size + 1) / 2); | ||
| break; | ||
|
|
||
| case needle_spread::dense_random: | ||
| { | ||
| mt19937 gen{}; | ||
| geometric_distribution<size_t> dis_dis{}; | ||
| vector<size_t> idx(needle_size); | ||
| const size_t mid = needle_size / 2; | ||
| idx[mid] = hay_size / 2; | ||
|
|
||
| const size_t max_shift = hay_size / needle_size; | ||
|
|
||
| for (size_t i = mid; i != 0; --i) { | ||
| idx[i - 1] = idx[i] - min(dis_dis(gen) + 1, max_shift); | ||
| } | ||
|
|
||
| for (size_t i = mid; i != needle_size - 1; ++i) { | ||
| idx[i + 1] = idx[i] + min(dis_dis(gen) + 1, max_shift); | ||
| } | ||
|
|
||
| needle.assign_range(idx | views::transform([&hay](const size_t i) { return hay[i]; })); | ||
| } | ||
| break; | ||
|
|
||
| case needle_spread::sparse: | ||
| needle.resize(needle_size); | ||
| for (size_t i = 0; i != needle_size; ++i) { | ||
| needle[i] = hay[hay_size * i / needle_size + hay_size / (needle_size * 2)]; | ||
| } | ||
| break; | ||
|
|
||
| case needle_spread::sparse_random: | ||
| needle.resize(needle_size); | ||
| ranges::sample(hay, needle.begin(), needle_size, mt19937{}); | ||
| break; | ||
| } | ||
|
|
||
| if (!expected_match) { | ||
| const T v = needle[needle_size / 2]; | ||
| const T r = static_cast<T>(static_cast<make_unsigned_t<T>>(v) + 1); | ||
| ranges::replace(hay, v, r); | ||
| ranges::sort(hay); | ||
| } | ||
|
|
||
| for (auto _ : state) { | ||
| benchmark::DoNotOptimize(hay); | ||
| benchmark::DoNotOptimize(needle); | ||
| bool found; | ||
| if constexpr (Alg == alg_type::rng) { | ||
| found = ranges::includes(hay, needle); | ||
| } else { | ||
| found = includes(hay.begin(), hay.end(), needle.begin(), needle.end()); | ||
| } | ||
| benchmark::DoNotOptimize(found); | ||
| if (found != expected_match) { | ||
| cerr << "Unexpected 'includes' result: " << found << '\n'; | ||
| abort(); | ||
StephanTLavavej marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| void common_args(auto bm) { | ||
| for (const auto& spread : | ||
| {needle_spread::dense, needle_spread::dense_random, needle_spread::sparse, needle_spread::sparse_random}) { | ||
| for (const auto& expected_match : {true, false}) { | ||
| for (const auto& needle_size : {3, 22, 105, 1504, 2750}) { | ||
| bm->Args({3000, needle_size, static_cast<underlying_type_t<needle_spread>>(spread), expected_match}); | ||
| } | ||
|
|
||
| for (const auto& needle_size : {3, 22, 105, 290}) { | ||
| bm->Args({300, needle_size, static_cast<underlying_type_t<needle_spread>>(spread), expected_match}); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| BENCHMARK(bm_includes<int8_t, alg_type::std_fn>)->Apply(common_args); | ||
| BENCHMARK(bm_includes<int16_t, alg_type::std_fn>)->Apply(common_args); | ||
| BENCHMARK(bm_includes<int32_t, alg_type::std_fn>)->Apply(common_args); | ||
| BENCHMARK(bm_includes<int64_t, alg_type::std_fn>)->Apply(common_args); | ||
|
|
||
| BENCHMARK(bm_includes<int8_t, alg_type::rng>)->Apply(common_args); | ||
| BENCHMARK(bm_includes<int16_t, alg_type::rng>)->Apply(common_args); | ||
| BENCHMARK(bm_includes<int32_t, alg_type::rng>)->Apply(common_args); | ||
| BENCHMARK(bm_includes<int64_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
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.