Skip to content

Commit

Permalink
Fixes: RNG instantiation UB, reducing avoiding compiler optimization …
Browse files Browse the repository at this point in the history
…for the callback.
  • Loading branch information
ashvardanian committed Nov 30, 2020
1 parent 27d21a3 commit 1cd4d82
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.7.2)
project(substr_search)
project(CppBenchSubstrSearch)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})

set(CMAKE_EXPORT_COMPILE_COMMANDS OFF)
Expand Down
8 changes: 5 additions & 3 deletions substr_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ void fill_buffer() {

haystack_rich.resize(buffer_size);
needles_rich.resize(200);
std::uniform_int_distribution<uint8_t> alphabet_rich('A', 'z');
std::uniform_int_distribution<uint32_t> alphabet_rich('A', 'z');
for (auto &c : haystack_rich)
c = alphabet_rich(rng);
for (auto &needle : needles_rich)
needle = random_part(haystack_rich, needle_len_k);

haystack_poor.resize(buffer_size);
needles_poor.resize(200);
std::uniform_int_distribution<uint8_t> alphabet_poor('a', 'z');
std::uniform_int_distribution<uint32_t> alphabet_poor('a', 'z');
for (auto &c : haystack_poor)
c = alphabet_poor(rng);
for (auto &needle : needles_poor)
Expand All @@ -58,7 +58,9 @@ void search(bm::State &state) {
span_t buffer_span {haystack.data(), haystack.size()};

for (auto _ : state)
bm::DoNotOptimize(find_all(buffer_span, needles[state.iterations() % needles.size()], engine, [](size_t) {}));
bm::DoNotOptimize(find_all(buffer_span, needles[state.iterations() % needles.size()], engine, [](size_t i) {
bm::DoNotOptimize(i);
}));

if (state.thread_index == 0) {
size_t bytes_scanned = state.iterations() * haystack.size() * state.threads;
Expand Down

0 comments on commit 1cd4d82

Please sign in to comment.