[libcxx-commits] [libcxx] [libc++] fix minor performance issue in `basic_string<C>::append()` (PR #210078)
Pavel Novikov via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Aug 3 16:52:03 PDT 2026
toughengineer wrote:
This seems to work in my setup:
```c++
template <typename Iterator>
struct unoptimizable_forward_iterator : Iterator {
using iterator_category = std::forward_iterator_tag;
unoptimizable_forward_iterator(Iterator i) : Iterator{i} {}
_LIBCPP_NOINLINE unoptimizable_forward_iterator& operator++() {
++static_cast<Iterator&>(*this);
return *this;
}
unoptimizable_forward_iterator operator++(int) {
auto it = *this;
this->operator++();
return it;
}
};
```
And in the benchmark:
```c++
benchmark::DoNotOptimize(
string.append(unoptimizable_forward_iterator(src.begin()), unoptimizable_forward_iterator(src.end())));
```
Any objections?
If everyone is happy with this, I'll create a PR for the benchmark tomorrow.
<details><summary>benchmark results</summary>
before:
```
2026-08-04T02:41:41+03:00
Running ./bench
Run on (20 X 3494.4 MHz CPU s)
CPU Caches:
L1 Data 48 KiB (x10)
L1 Instruction 32 KiB (x10)
L2 Unified 2048 KiB (x10)
L3 Unified 24576 KiB (x1)
Load Average: 0.11, 0.12, 0.05
***WARNING*** ASLR is enabled, the results may have unreproducible noise in them.
-------------------------------------------------------------------------------------------------------
Benchmark Time CPU Iterations
-------------------------------------------------------------------------------------------------------
std::string::append(ForwardIt, ForwardIt) (opaque)/5 16.7 ns 16.7 ns 41817708
std::u8string::append(ForwardIt, ForwardIt) (opaque)/5 16.7 ns 16.7 ns 40527928
std::wstring::append(ForwardIt, ForwardIt) (opaque)/5 22.1 ns 22.1 ns 32384438
std::string::append(ForwardIt, ForwardIt) (opaque)/30 65.7 ns 65.7 ns 10090497
std::u8string::append(ForwardIt, ForwardIt) (opaque)/30 66.9 ns 66.9 ns 10181715
std::wstring::append(ForwardIt, ForwardIt) (opaque)/30 65.4 ns 65.4 ns 10365533
std::string::append(ForwardIt, ForwardIt) (transparent)/5 16.6 ns 16.6 ns 42325432
std::u8string::append(ForwardIt, ForwardIt) (transparent)/5 16.9 ns 16.9 ns 40609161
std::wstring::append(ForwardIt, ForwardIt) (transparent)/5 21.7 ns 21.7 ns 31280904
std::string::append(ForwardIt, ForwardIt) (transparent)/30 66.1 ns 66.1 ns 10379597
std::u8string::append(ForwardIt, ForwardIt) (transparent)/30 67.4 ns 67.4 ns 10247057
std::wstring::append(ForwardIt, ForwardIt) (transparent)/30 65.1 ns 65.1 ns 10302284
```
after:
```
2026-08-04T02:42:29+03:00
Running ./bench
Run on (20 X 3494.4 MHz CPU s)
CPU Caches:
L1 Data 48 KiB (x10)
L1 Instruction 32 KiB (x10)
L2 Unified 2048 KiB (x10)
L3 Unified 24576 KiB (x1)
Load Average: 0.21, 0.15, 0.07
***WARNING*** ASLR is enabled, the results may have unreproducible noise in them.
-------------------------------------------------------------------------------------------------------
Benchmark Time CPU Iterations
-------------------------------------------------------------------------------------------------------
std::string::append(ForwardIt, ForwardIt) (opaque)/5 12.1 ns 12.1 ns 57699076
std::u8string::append(ForwardIt, ForwardIt) (opaque)/5 12.6 ns 12.6 ns 55272438
std::wstring::append(ForwardIt, ForwardIt) (opaque)/5 18.1 ns 18.1 ns 37924018
std::string::append(ForwardIt, ForwardIt) (opaque)/30 40.4 ns 40.4 ns 17080440
std::u8string::append(ForwardIt, ForwardIt) (opaque)/30 39.5 ns 39.5 ns 18209833
std::wstring::append(ForwardIt, ForwardIt) (opaque)/30 39.4 ns 39.4 ns 17736548
std::string::append(ForwardIt, ForwardIt) (transparent)/5 11.6 ns 11.6 ns 61564765
std::u8string::append(ForwardIt, ForwardIt) (transparent)/5 12.6 ns 12.6 ns 54926645
std::wstring::append(ForwardIt, ForwardIt) (transparent)/5 18.2 ns 18.2 ns 37916232
std::string::append(ForwardIt, ForwardIt) (transparent)/30 40.7 ns 40.7 ns 17099742
std::u8string::append(ForwardIt, ForwardIt) (transparent)/30 39.4 ns 39.4 ns 17974869
std::wstring::append(ForwardIt, ForwardIt) (transparent)/30 39.7 ns 39.7 ns 17597919
```
</details>
https://github.com/llvm/llvm-project/pull/210078
More information about the libcxx-commits
mailing list