I'm expecting std::pair and std::tuple have the similar behavior. But it turns out that std::tuple generates worse asm code then std::pair.
https://gcc.godbolt.org/z/Ri4M8z - gcc 10.0.0 nightly build, -O3 -std=c++17
compiling for the x86-64 System V ABI
#include <utility>
std::pair<long, long> test_pair() {
return { 1, 2 };
}
# returned in RDX:RAX
test_pair():
mov eax, 1
mov edx, 2
ret
#include <tuple>
std::tuple<long, long> test_tuple() {
return { 1, 2 };
}
# returned via hidden pointer, not packed into registers
test_tuple():
mov QWORD PTR [rdi], 2
mov QWORD PTR [rdi+8], 1
mov rax, rdi
ret
Those two functions both return two values. test_pair
uses registers to store values which is expected; but test_tuple stores values on stack, which seems unoptimise. Why these two functions behave differently?