https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94082

--- Comment #9 from Filip Sajdak <filip.sajdak at gmail dot com> ---
A note from PR 128901, which was marked a duplicate of this one: part of what
it measured needs no change in the compiler, and is not what this report asks
for.

In constant evaluation libstdc++'s char_traits<char>::compare and length always
take the character loop (__gnu_cxx::char_traits), also when the arguments are
string literals that the built-in can fold today. An experiment that tries the
built-in first under __builtin_constant_p, and keeps the loop otherwise, gave
these numbers on 16.2.0 (aarch64-unknown-linux-gnu, release checking;
user-space instructions of cc1plus, least of three runs, per call, 4000 calls
on distinct names of 34 characters, a twin without the call subtracted):

                                   starts_with("lv_0") on a literal view   the
same on a std::string
16.2.0                             307 k                                   308
k
trunk 9eb44775b9                   312 k                                   294
k
16.2.0, library change only        53 k                                    242
k
16.2.0, library and front end      53 k                                    60 k

For a real program (one translation unit of a C++26 library that compares many
names from reflection) the front end goes from 54.09 G to 42.52 G instructions
with the library change alone (-21.4 %), and to 41.82 G with the front-end
change of this report added.

So most of the gain for string_view::starts_with, operator== and
string_view(const char*) on literals is available in bits/char_traits.h alone.
The front-end part (what this report is about) then matters for strings on the
constexpr heap and in automatic storage.

libstdc++ conformance.exp on 21_strings, 23_containers/vector and
20_util/specialized_algorithms: 1478 passes, 14 expected failures, 39
unsupported, before and after. Not checked: wchar_t, char8_t, char16_t and
char32_t traits (only char was changed), memchr and find, and trunk.

The experiment is the attachment of PR 128901. It was written with the help of
an LLM, so with regard to the GCC AI policy it is not offered as a patch
submission, only as an analysis. If the library part should be a report of its
own under libstdc++, I am glad to open one.

These measurements were prepared with AI assistance and run on a machine of
mine.

Reply via email to