[cfe-dev] missing optimization opportunity for const std::vector compared to std::array

Dennis Luehring via cfe-dev cfe-dev at lists.llvm.org
Sat Oct 20 03:00:45 PDT 2018


for gcc-discussion see: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58483

is there a (good) reason not to remove the new/delete combination when 
the result is already known?

#include <vector>
#include <numeric>
#include <array>

static int calc(const std::array<int,3> p_ints, const int& p_init)
//static int calc(const std::vector<int> p_ints, const int& p_init)
{
   return std::accumulate(p_ints.begin(), p_ints.end(), p_init);
}

int main()
{
   const int result = calc({10,20,30},100);
   return result;
}

results:

gcc.godbolt x86-64 clang trunk, with -O2

with std::array

main: # @main
mov eax, 160
ret

with std::vector

main: # @main
push    rax
mov     edi, 12
call    operator new(unsigned long)
movabs  rcx, 85899345930
mov     qword ptr [rax], rcx
mov     dword ptr [rax + 8], 30
mov     rdi, rax
call    operator delete(void*)
mov     eax, 160
pop     rcx
ret

---

clang reduces new/delete with std::string but vector seems to be missing 
(buildin_new/delete missing in libc++?)

#include <string>

int main()
{
   return std::string("hello").size();
}

down to

main: # @main
mov eax, 5
ret





More information about the cfe-dev mailing list