Question:
Why is return "";
faster than return {};
when initializing an empty std::string
container?
To test this, I compiled the following code:
#include <string>
std::string make_default() {
return {};
}
std::string make_empty() {
return "";
}
The compiled code (using clang 16 and libc++) is as follows:
make_default():
mov rax, rdi
xorps xmm0, xmm0
movups xmmword ptr [rdi], xmm0
mov qword ptr [rdi + 16], 0
ret
make_empty():
mov rax, rdi
mov word ptr [rdi], 0
ret
See live example at Compiler Explorer.
Notice how return {};
is zeroing 24 bytes in total, but return "";
is only zeroing 2 bytes.
Question: Why is return "";
faster than return {};
when initializing an empty std::string
container?