A few places in bits/basic_string.h use `traits_type::copy` to copy `__str.length() + 1` bytes. Despite the knowledge that `__str.length()` is not greater than 15 the compiler emits (and sometimes 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 This patch always copies `_S_local_capacity + 1` if working with `std::char_traits`. PR libstdc++/112682: * include/bits/basic_string.h: Optimize string moves. P.S.: still not sure that this optimization is not an UB or fine for libstdc++. However, the assembly looks much better with it. -- Best regards, Antony Polukhin