[libcxx-commits] [libcxx] [libc++] Optimize char_traits a bit (PR #72799)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Mon Mar 25 01:26:41 PDT 2024


philnik777 wrote:

> Could you please provide information about:
> 
> (A) Benchmark results for these functions.

These are the numbers from optimizing `std::find`:
```
---------------------------------------------------------------
Benchmark                                   old             new
---------------------------------------------------------------
bm_find<char>/1                         10.5 ns         10.5 ns
bm_find<char>/2                         18.3 ns         10.4 ns
bm_find<char>/3                         21.1 ns         10.4 ns
bm_find<char>/4                         22.4 ns         10.3 ns
bm_find<char>/5                         23.3 ns         10.3 ns
bm_find<char>/6                         23.7 ns         10.4 ns
bm_find<char>/7                         24.0 ns         10.3 ns
bm_find<char>/8                         24.5 ns         10.2 ns
bm_find<char>/16                        26.2 ns         10.2 ns
bm_find<char>/64                        29.9 ns         20.8 ns
bm_find<char>/512                       80.2 ns         36.0 ns
bm_find<char>/4096                       523 ns         43.0 ns
bm_find<char>/32768                     3838 ns          105 ns
bm_find<char>/262144                   30832 ns          988 ns
bm_find<char>/1048576                 122541 ns         4854 ns
bm_find<int>/1                          10.4 ns         12.1 ns
bm_find<int>/2                          18.4 ns         12.0 ns
bm_find<int>/3                          21.9 ns         12.0 ns
bm_find<int>/4                          23.3 ns         11.9 ns
bm_find<int>/5                          24.2 ns         12.1 ns
bm_find<int>/6                          24.7 ns         12.0 ns
bm_find<int>/7                          24.8 ns         11.9 ns
bm_find<int>/8                          25.2 ns         11.9 ns
bm_find<int>/16                         26.2 ns         21.9 ns
bm_find<int>/64                         30.5 ns         33.8 ns
bm_find<int>/512                        80.4 ns         40.4 ns
bm_find<int>/4096                        497 ns         71.7 ns
bm_find<int>/32768                      3844 ns          498 ns
bm_find<int>/262144                    30722 ns         4901 ns
bm_find<int>/1048576                  123303 ns        22864 ns
```

Since it's basically the same optimization I think these are pretty representative of the expected speedup. The numbers for `length` would also be similar, since `strlen` is basically `memchr(str, '\0', <buffer_len>)`, just with possible out-of-bounds reads.

> (B) The changes to compile times and compiler memory usage.

I don't know how to measure memory usage properly, but the time of including `<string>` is within margin of error. I've checked with `-std=c++23`, which eagerly instantiates all the functions.


https://github.com/llvm/llvm-project/pull/72799


More information about the libcxx-commits mailing list