From: Andrew Pinski Date: Tue, 18 Nov 2025 20:57:24 +0000 (-0800) Subject: libstdc++: store the length after the store of the null character X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=47ebad54abc127934693e7ace75a3cb08d6bc36c;p=thirdparty%2Fgcc.git libstdc++: store the length after the store of the null character This improves the code generation slightly for std::string because of aliasing. In many cases the length will be read again and the store of the null character will cause the length to be re-read due to aliasing requirements of the char type. So swapping around the stores will allow the length not to have to be reloaded from memory and will allow for more optimizations. Bootstrapped and tested on x86_64-linux-gnu. libstdc++-v3/ChangeLog: * include/bits/basic_string.h (basic_string::M_set_length): Swap around the order of traits_type::assign and _M_length so that _M_length is at the end. Signed-off-by: Andrew Pinski --- diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index 8ae6569f501..c4b6b1064a9 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -269,8 +269,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 void _M_set_length(size_type __n) { - _M_length(__n); traits_type::assign(_M_data()[__n], _CharT()); + _M_length(__n); } _GLIBCXX20_CONSTEXPR