https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112440
Bug ID: 112440 Summary: Compiler does not grok basic_string::resize and basic_string::reserve if _CharT is char Product: gcc Version: 13.2.1 Status: UNCONFIRMED Keywords: missed-optimization Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: antoshkka at gmail dot com Target Milestone: --- Consider the example: #include <string> void test1(std::size_t summ) { std::string result; result.resize(summ); if (result.size() > summ) { __builtin_abort(); } } The resulting assembly contains `call abort` and code to check the string size: https://godbolt.org/z/zcj3Pc3G8 Looks like this is due to char* aliasing with string internals, switching to std::u8string removes the `call abort` related assembly: https://godbolt.org/z/a6bKaqqn5 I've failed to come up with a generic solution, but looks like adding __builtin_unreachable() to the end of basic_string::resize and basic_string::reserve helps: https://godbolt.org/z/vWcjqGK94 P.S.: such hints help to shorten the assembly for reserve+append*n cases https://godbolt.org/z/nsEGsWdP3 , https://godbolt.org/z/qMf4b7dd8 , https://godbolt.org/z/1r6dd6d5M which are quire common