https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112682
Bug ID: 112682 Summary: More efficient std::basic_string move construction Product: gcc Version: 14.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: antoshkka at gmail dot com Target Milestone: --- A few places in bits/basic_string.h have the following code: ``` if (__str._M_is_local()) { _M_init_local_buf(); traits_type::copy(_M_local_buf, __str._M_local_buf, __str.length() + 1); } ``` Despite the knowledge, that `__str.length()` is not greater than 15 the compiler emits (and inlines) a memcpy call. That results in a quite big set of instructions https://godbolt.org/z/j35MMfxzq Replacing `__str.length() + 1` with `_S_local_capacity + 1` explicitly forces the compiler to copy the whole `__str._M_local_buf`. As a result the assembly becomes almost 5 times shorter and without any function calls or multiple conditional jumps https://godbolt.org/z/bfq8bxra9 P.S.: not sure, if it is allowed to copy uninitialized data via traits_type::copy and would the sanitizers be happy with such copy attempt.