-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Improve search
/find_end
perf by dropping memcmp
#4654
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
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
73c96da
vectorize search
AlexGuteniev 0c17a53
very tail fix
AlexGuteniev 11c05ee
I 🧡 ADL
AlexGuteniev d4fcc96
unify ipsum
AlexGuteniev da5cf2e
-newline
AlexGuteniev da157b1
`strstr` for competition
AlexGuteniev 772c513
missing progress
AlexGuteniev 2c6c329
coverage
AlexGuteniev 81a6000
these tests are too long
AlexGuteniev 0b59b2e
missing include
AlexGuteniev f2806c5
default_searcher
AlexGuteniev 15e54a9
ADL again
AlexGuteniev 26646fe
avoid `memcmp` in fallback
AlexGuteniev 0c473a4
partial review comment
AlexGuteniev 3452fcc
Merge branch 'main' into search
StephanTLavavej 629afd4
Internal static assert `sizeof(_Ty1) == sizeof(_Ty2)`.
StephanTLavavej a24e6eb
Use `+=` and `+` instead of `_RANGES next`.
StephanTLavavej 9d07a40
Style: Return `_Ptr_res1` instead of `_Ptr_last1` when they're equal.
StephanTLavavej d57f9b6
Style: In `<algorithm>` and `<functional>`, `_Ptr_last1` doesn't need…
StephanTLavavej e51b98d
Restore top-level constness for `_UFirst2`.
StephanTLavavej d4462a5
Benchmark classic search().
StephanTLavavej 95ba820
Simplify `last_known_good_search()`.
StephanTLavavej 72a0d29
Revert vectorized implementation.
StephanTLavavej 38b32d6
Drop `memcmp` paths from `_Equal_rev_pred_unchecked` and `_Equal_rev_…
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
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,114 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
#include <algorithm> | ||
#include <benchmark/benchmark.h> | ||
#include <cstdint> | ||
#include <cstring> | ||
#include <functional> | ||
#include <string> | ||
#include <vector> | ||
|
||
const char src_haystack[] = | ||
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam mollis imperdiet massa, at dapibus elit interdum " | ||
"ac. In eget sollicitudin mi. Nam at tellus at sapien tincidunt sollicitudin vel non eros. Pellentesque nunc nunc, " | ||
"ullamcorper eu accumsan at, pulvinar non turpis. Quisque vel mauris pulvinar, pretium purus vel, ultricies erat. " | ||
"Curabitur a magna in ligula tristique ornare. Quisque commodo, massa viverra laoreet luctus, sem nisi aliquam " | ||
"velit, fermentum pulvinar velit leo eget justo. Suspendisse vel erat efficitur, pulvinar eros volutpat, vulputate " | ||
"ex. Phasellus non purus vel velit tristique tristique id at ligula. Quisque mollis sodales magna. Mauris et quam " | ||
"eu quam viverra tempus. Nullam tempus maximus porta. Nunc mattis eleifend fermentum. Nullam aliquam libero " | ||
"accumsan velit elementum, eu laoreet metus convallis. Donec pellentesque lacus ut iaculis iaculis. Curabitur orci " | ||
"elit, bibendum sit amet feugiat at, iaculis sit amet massa. Maecenas imperdiet lacus at vehicula iaculis. Donec " | ||
"volutpat nunc sit amet accumsan tempor. Quisque pretium vestibulum ultricies. Suspendisse potenti. Aenean at diam " | ||
"iaculis, condimentum felis venenatis, condimentum erat. Nam quis elit dui. Duis quis odio vitae metus hendrerit " | ||
"rhoncus ut et magna. Cras ac augue quis nibh pharetra sagittis. Donec ullamcorper vel eros semper pretium. Proin " | ||
"vel sollicitudin eros. Nulla sollicitudin mattis turpis id suscipit. Aliquam sed risus velit. Aliquam iaculis nec " | ||
"nibh ac egestas. Duis finibus semper est sed consequat. Sed in sapien quis nibh dignissim mattis. Vestibulum nec " | ||
"metus sodales, euismod mauris ac, sollicitudin libero. Maecenas non arcu ac velit ullamcorper fringilla et quis " | ||
"nulla. Curabitur posuere leo eget ipsum tincidunt dignissim. Cras ultricies suscipit neque, quis suscipit tortor " | ||
"venenatis non. Cras nisl mi, bibendum in vulputate quis, vestibulum ornare enim. Nunc hendrerit placerat dui, " | ||
"aliquam mollis sem convallis et. Integer vitae urna diam. Phasellus et imperdiet est. Maecenas auctor facilisis " | ||
"nibh non commodo. Suspendisse iaculis quam id bibendum feugiat. Pellentesque felis erat, egestas a libero ac, " | ||
"laoreet consectetur elit. Cras ut suscipit ex. Etiam gravida sem quis ex porta, eu lacinia tortor fermentum. " | ||
"Nulla consequat odio enim, sed condimentum est sagittis a. Quisque nec commodo tellus. Phasellus elementum " | ||
"feugiat dolor et feugiat. Praesent sed mattis tortor. In vitae sodales purus. Morbi accumsan, ligula et interdum " | ||
"lacinia, leo risus suscipit urna, non luctus mi justo eu ipsum. Curabitur venenatis pretium orci id porttitor. " | ||
"Quisque dapibus nisl sit amet elit lobortis sagittis. Orci varius natoque penatibus et magnis dis parturient " | ||
"montes, nascetur ridiculus mus. Mauris varius dui sit amet tortor facilisis vestibulum. Curabitur condimentum " | ||
"justo nec orci mattis auctor. Quisque aliquet condimentum arcu ac sollicitudin. Maecenas elit elit, condimentum " | ||
"vitae auctor a, cursus et sem. Cras vehicula ante in consequat fermentum. Praesent at massa nisi. Mauris pretium " | ||
"euismod eros, ut posuere ligula ullamcorper id. Nullam aliquet malesuada est at dignissim. Pellentesque finibus " | ||
"sagittis libero nec bibendum. Phasellus dolor ipsum, finibus quis turpis quis, mollis interdum felis."; | ||
|
||
const char src_needle[] = "aliquet"; | ||
|
||
void c_strstr(benchmark::State& state) { | ||
const std::string haystack(std::begin(src_haystack), std::end(src_haystack)); | ||
const std::string needle(std::begin(src_needle), std::end(src_needle)); | ||
|
||
for (auto _ : state) { | ||
benchmark::DoNotOptimize(haystack); | ||
benchmark::DoNotOptimize(needle); | ||
auto res = std::strstr(haystack.c_str(), needle.c_str()); | ||
benchmark::DoNotOptimize(res); | ||
} | ||
} | ||
|
||
template <class T> | ||
void classic_search(benchmark::State& state) { | ||
const std::vector<T> haystack(std::begin(src_haystack), std::end(src_haystack)); | ||
const std::vector<T> needle(std::begin(src_needle), std::end(src_needle)); | ||
|
||
for (auto _ : state) { | ||
benchmark::DoNotOptimize(haystack); | ||
benchmark::DoNotOptimize(needle); | ||
auto res = std::search(haystack.begin(), haystack.end(), needle.begin(), needle.end()); | ||
benchmark::DoNotOptimize(res); | ||
} | ||
} | ||
|
||
template <class T> | ||
void ranges_search(benchmark::State& state) { | ||
const std::vector<T> haystack(std::begin(src_haystack), std::end(src_haystack)); | ||
const std::vector<T> needle(std::begin(src_needle), std::end(src_needle)); | ||
|
||
for (auto _ : state) { | ||
benchmark::DoNotOptimize(haystack); | ||
benchmark::DoNotOptimize(needle); | ||
auto res = std::ranges::search(haystack, needle); | ||
benchmark::DoNotOptimize(res); | ||
} | ||
} | ||
|
||
template <class T> | ||
void search_default_searcher(benchmark::State& state) { | ||
const std::vector<T> haystack(std::begin(src_haystack), std::end(src_haystack)); | ||
const std::vector<T> needle(std::begin(src_needle), std::end(src_needle)); | ||
|
||
for (auto _ : state) { | ||
benchmark::DoNotOptimize(haystack); | ||
benchmark::DoNotOptimize(needle); | ||
auto res = std::search(haystack.begin(), haystack.end(), std::default_searcher{needle.begin(), needle.end()}); | ||
benchmark::DoNotOptimize(res); | ||
} | ||
} | ||
|
||
BENCHMARK(c_strstr); | ||
|
||
BENCHMARK(classic_search<std::uint8_t>); | ||
BENCHMARK(classic_search<std::uint16_t>); | ||
BENCHMARK(classic_search<std::uint32_t>); | ||
BENCHMARK(classic_search<std::uint64_t>); | ||
|
||
BENCHMARK(ranges_search<std::uint8_t>); | ||
BENCHMARK(ranges_search<std::uint16_t>); | ||
BENCHMARK(ranges_search<std::uint32_t>); | ||
BENCHMARK(ranges_search<std::uint64_t>); | ||
|
||
BENCHMARK(search_default_searcher<std::uint8_t>); | ||
BENCHMARK(search_default_searcher<std::uint16_t>); | ||
BENCHMARK(search_default_searcher<std::uint32_t>); | ||
BENCHMARK(search_default_searcher<std::uint64_t>); | ||
|
||
|
||
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
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
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.