Skip to content

Commit

Permalink
Improve: Single-byte search bench
Browse files Browse the repository at this point in the history
In the past we called `strstr` & `memmem`
assuming they'd directly call `strchr` & `memchr`,
if the needle is only a byte long. Now the benchmark
itself routes the call.
  • Loading branch information
ashvardanian committed Mar 19, 2024
1 parent c30bf94 commit 08bb646
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions scripts/bench_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ tracked_binary_functions_t find_functions() {
tracked_binary_functions_t result = {
{"std::string_view.find",
[](std::string_view h, std::string_view n) {
auto match = h.find(n);
auto match = n.size() == 1 ? h.find(n.front()) : h.find(n);
return (match == std::string_view::npos ? h.size() : match);
}},
{"sz_find_serial", wrap_sz(sz_find_serial), true},
Expand All @@ -38,15 +38,17 @@ tracked_binary_functions_t find_functions() {
#if SZ_USE_ARM_NEON
{"sz_find_neon", wrap_sz(sz_find_neon), true},
#endif
{"strstr",
{"strstr/strchr",
[](std::string_view h, std::string_view n) {
sz_cptr_t match = strstr(h.data(), n.data());
sz_cptr_t match = n.size() == 1 ? (sz_cptr_t)strchr(h.data(), n.front()) //
: (sz_cptr_t)strstr(h.data(), n.data());
return (match ? match - h.data() : h.size());
}},
#ifdef _GNU_SOURCE
{"memmem", // Not supported on MSVC
{"memmem/memchr", // Not supported on MSVC
[](std::string_view h, std::string_view n) {
sz_cptr_t match = (sz_cptr_t)memmem(h.data(), h.size(), n.data(), n.size());
sz_cptr_t match = n.size() == 1 ? (sz_cptr_t)memchr(h.data(), n.front(), h.size())
: (sz_cptr_t)memmem(h.data(), h.size(), n.data(), n.size());
return (match ? match - h.data() : h.size());
}},
#endif
Expand Down Expand Up @@ -84,7 +86,7 @@ tracked_binary_functions_t rfind_functions() {
tracked_binary_functions_t result = {
{"std::string_view.rfind",
[](std::string_view h, std::string_view n) {
auto match = h.rfind(n);
auto match = n.size() == 1 ? h.rfind(n.front()) : h.rfind(n);
return (match == std::string_view::npos ? 0 : match);
}},
{"sz_rfind_serial", wrap_sz(sz_rfind_serial), true},
Expand Down

0 comments on commit 08bb646

Please sign in to comment.