I am watching an old video Jason Turner: Practical Performance Practices.
Right at the beginning there's an example for optimized code in GCC 5.1:
#include <string>
int main() {
return std::string("a").size();
}
Which compiles to "nothing":
main:
mov eax, 1
ret
However, I was quite surprised to see another output with GCC 13.2:
main:
sub rsp, 40
lea rax, [rsp+16]
mov rdi, rsp
mov QWORD PTR [rsp+8], 1
mov QWORD PTR [rsp], rax
mov eax, 97
mov WORD PTR [rsp+16], ax
call std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_dispose()
mov eax, 1
add rsp, 40
ret
https://godbolt.org/z/45PWox4Gb
Is it a bug that GCC hasn't been able to constant-propagate std::string.size()
since 5.1 (12.3 according to the comments)?
It used to do it without the -std=
argument for -O3
. The new version requires -std=c++20
to provide the same output.