[RFC] libstdc++: Segmented find performance and traversal corner cases
李华圣
lihuasheng@bytedance.com
Tue Nov 10 11:04:19 GMT 2026
Hi,
While looking into std::find performance on std::deque, I noticed that
__for_each_segment is currently wired up for std::for_each but not
std::find. In prototyping a std::find dispatch through
__for_each_segment, I also hit two independent bugs in the existing
segment traversal machinery.
1. Traversal corner cases
The first issue is in join_view when the inner range is not a
common_range and the final segment is partial. Full segments are
delimited by the inner sentinel, whereas a partial final segment is
delimited by an inner iterator. _S_for_each_segment attempts to assign
that inner iterator to the sentinel variable, which is ill-formed when
the types differ.
The second issue is in concat_view, where segment traversal copies the
stored views into local variables. This breaks traversal over move-only
views such as owning_view. For copyable views that own their elements,
it can also yield dangling iterators into the temporary copy, and const
traversal can lose const-qualification.
I have attached standalone reproducers: repro-join.cc for -std=c++20 and
repro-concat.cc for -std=c++26. They exercise __for_each_segment directly,
so both issues can be evaluated independently of the std::find changes.
Both fail to compile at revision 0bc36c00b8f0 and pass with the
corresponding traversal fixes.
2. std::find prototype and semantics
Wiring std::find into __for_each_segment required preserving the outer
iterator's reference type and value category. A heterogeneous
concat_view may dereference to a common prvalue type. Calling std::find
directly on an inner segment would bypass the outer reference conversion
and could select a different operator== overload or change an argument's
value category.
Constant evaluation also needs to retain the generic iterator loop:
deque segment traversal is not constexpr yet. The algorithm change only
covers std::find, not ranges::find.
For mutable byte segments, the segmented path can reach the existing
memchr optimization. The current prototype uses a reference-preserving
loop for const byte segments; GCC vectorizes that loop.
The two attached diffs contain the traversal fixes and the std::find
prototype, including their tests. benchmark.cc and benchmark-README.txt
provide the measurement source, commands and CSV comparison method.
Apply the traversal-fix patch before the find patch.
3. Microbenchmark results
Measured with GCC 17.0.0 20261109 (experimental) and matching libstdc++,
built from revision 0bc36c00b8f0 (2026-11-09), targeting
x86_64-pc-linux-gnu. Both versions use the same compiler and runtime;
the prototype only overlays the modified headers.
Flags were -O3 -march=native, separately in C++17 and C++26 modes. Runs
were pinned to CPU 3 on an Intel Xeon Platinum 8582C VM. The benchmark
repeats hot-cache searches, uses compiler memory barriers and consumes
the result. I ran baseline then prototype and repeated in reverse order,
using sample medians; C++17 also had a third follow-up run with the same
binaries.
65,536 elements, missing C++17 speedup C++26 speedup
deque<int> 7.11-7.14x 9.55-9.62x
deque<unsigned char> 31.05-40.13x 30.68-30.98x
const deque<int> 6.54-6.59x 6.57-6.58x
const deque<unsigned char> 21.67-22.06x 21.23-21.51x
Some short-range cases regress. For an 8-element const deque<int>, a
middle match in C++26 went from about 4.04 ns to 5.20 ns, and a missing
search in C++17 went from about 5.4 ns to 8.0 ns. Empty ranges and
immediate first-element hits also show subnanosecond regressions or
variation. The causes of these differences have not been isolated.
In C++17, the mutable vector<int> large-miss control varied across three
runs (baseline/prototype ratios 1.046x, 0.870x and 0.772x) despite an
unchanged normalized search instruction sequence. This remains
unexplained. C++26 large-miss vector controls stayed within about 2.1%.
One C++17 run also saw the deque<unsigned char> baseline shift from
about 44.9 us to 35.3 us while the prototype stayed at 1.12-1.14 us. The
table reports the full span across all three C++17 runs.
These are measurements of this hot-cache workload, not an application
speedup estimate.
The prototype passed 68 focused native checks: 66 compile-and-run checks
and two compile-only checks. Coverage includes C++11/14/17/20/23/26,
normal and _GLIBCXX_DEBUG modes, C++26 constexpr deque, join_view and
concat_view, reference preservation and owning views. This is not an
exhaustive matrix of all tests in every mode.
The compiler and runtime were built in a single-stage configuration with
--disable-bootstrap. I have not run the full libstdc++ DejaGnu testsuite.
I wrote the implementation changes in the attached patches. OpenAI Codex
helped write the tests and benchmark code; its code-writing assistance
was limited to those parts. Codex also helped edit this email.
I would appreciate feedback on the traversal fixes and the std::find
optimization, including the short-range regressions noted above.
Assisted-by: OpenAI Codex (tests, benchmark code, and email editing)
Thanks,
lihuasheng
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20261110/8ca9b67d/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: repro-join.cc
Type: application/octet-stream
Size: 663 bytes
Desc: not available
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20261110/8ca9b67d/attachment-0005.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: repro-concat.cc
Type: application/octet-stream
Size: 447 bytes
Desc: not available
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20261110/8ca9b67d/attachment-0006.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: benchmark.cc
Type: application/octet-stream
Size: 4027 bytes
Desc: not available
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20261110/8ca9b67d/attachment-0007.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-libstdc-Fix-segmented-traversal-of-join-and-concat-v.patch
Type: application/octet-stream
Size: 7058 bytes
Desc: not available
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20261110/8ca9b67d/attachment-0008.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0002-libstdc-Optimize-find-for-segmented-iterators.patch
Type: application/octet-stream
Size: 17437 bytes
Desc: not available
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20261110/8ca9b67d/attachment-0009.obj>
More information about the Libstdc++
mailing list