From 8d623806cbb1c374559fa6f3bf5ba7e0d89e1342 Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Tue, 22 Apr 2025 21:56:42 +0300 Subject: [PATCH 1/2] optimize LKG search and find_end Avoid calling equal in a loop. Similar to #4654 --- .../VSO_0000000_vector_algorithms/test.cpp | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/tests/std/tests/VSO_0000000_vector_algorithms/test.cpp b/tests/std/tests/VSO_0000000_vector_algorithms/test.cpp index 13134bdd862..a291ecc304e 100644 --- a/tests/std/tests/VSO_0000000_vector_algorithms/test.cpp +++ b/tests/std/tests/VSO_0000000_vector_algorithms/test.cpp @@ -271,10 +271,19 @@ auto last_known_good_find_first_of(FwdItH h_first, FwdItH h_last, FwdItN n_first template auto last_known_good_search(RanItH h_first, RanItH h_last, RanItN n_first, RanItN n_last) { - const auto n_len = n_last - n_first; + const ptrdiff_t n_len = n_last - n_first; for (; h_last - h_first >= n_len; ++h_first) { - if (equal(h_first, h_first + n_len, n_first, n_last)) { + bool is_eq = true; + + for (ptrdiff_t i = 0; i != n_len; ++i) { + if (*(h_first + i) != *(n_first + i)) { + is_eq = false; + break; + } + } + + if (is_eq) { return h_first; } } @@ -284,7 +293,7 @@ auto last_known_good_search(RanItH h_first, RanItH h_last, RanItN n_first, RanIt template auto last_known_good_find_end(RanItH h_first, RanItH h_last, RanItN n_first, RanItN n_last) { - const auto n_len = n_last - n_first; + const ptrdiff_t n_len = n_last - n_first; if (n_len > h_last - h_first) { return h_last; @@ -293,7 +302,16 @@ auto last_known_good_find_end(RanItH h_first, RanItH h_last, RanItN n_first, Ran auto h_mid = h_last - n_len; for (;;) { - if (equal(h_mid, h_mid + n_len, n_first, n_last)) { + bool is_eq = true; + + for (ptrdiff_t i = 0; i != n_len; ++i) { + if (*(h_mid + i) != *(n_first + i)) { + is_eq = false; + break; + } + } + + if (is_eq) { return h_mid; } From 903eb1f67a9c921f35a88cf78b4df96b9d09f6e2 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Tue, 22 Apr 2025 20:32:18 -0700 Subject: [PATCH 2/2] is_eq => is_equal --- .../std/tests/VSO_0000000_vector_algorithms/test.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/std/tests/VSO_0000000_vector_algorithms/test.cpp b/tests/std/tests/VSO_0000000_vector_algorithms/test.cpp index 210d0431f7b..c016fccac22 100644 --- a/tests/std/tests/VSO_0000000_vector_algorithms/test.cpp +++ b/tests/std/tests/VSO_0000000_vector_algorithms/test.cpp @@ -274,16 +274,16 @@ auto last_known_good_search(RanItH h_first, RanItH h_last, RanItN n_first, RanIt const ptrdiff_t n_len = n_last - n_first; for (; h_last - h_first >= n_len; ++h_first) { - bool is_eq = true; + bool is_equal = true; for (ptrdiff_t i = 0; i != n_len; ++i) { if (*(h_first + i) != *(n_first + i)) { - is_eq = false; + is_equal = false; break; } } - if (is_eq) { + if (is_equal) { return h_first; } } @@ -302,16 +302,16 @@ auto last_known_good_find_end(RanItH h_first, RanItH h_last, RanItN n_first, Ran auto h_mid = h_last - n_len; for (;;) { - bool is_eq = true; + bool is_equal = true; for (ptrdiff_t i = 0; i != n_len; ++i) { if (*(h_mid + i) != *(n_first + i)) { - is_eq = false; + is_equal = false; break; } } - if (is_eq) { + if (is_equal) { return h_mid; }