missing optimization opportunity for const std::vector compared to std::array

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

missing optimization opportunity for const std::vector compared to std::array

David Blaikie via cfe-dev
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


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: missing optimization opportunity for const std::vector compared to std::array

David Blaikie via cfe-dev
so i've rembered correct - Richard Smith already fixed that with an
patch to LibC++ years ago

http://clang-developers.42468.n3.nabble.com/missing-optimization-opportunity-for-const-std-vector-compared-to-std-array-td4034587.html#none 


see post ~Jun 04, 2014; 12:16am
http://clang-developers.42468.n3.nabble.com/missing-optimization-opportunity-for-const-std-vector-compared-to-std-array-tp4034587p4039822.html

this post informed about the patch
http://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20140602/106887.html

what happend to this optimization - was it removed or broken over time?
or is it just not possible to test the behavior on gcc.godbolt.org with
the laste clang, missing flags or something?

Am 20.10.2018 um 12:00 schrieb Dennis Luehring via cfe-dev:

> 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
>
>
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: missing optimization opportunity for const std::vector compared to std::array

David Blaikie via cfe-dev
its is fixed: forgot to use libc+++ at all

godbolt: -O2 -stdlib=libc++

is the answer

thx

Am 22.10.2018 um 16:53 schrieb Dennis Luehring via cfe-dev:

> so i've rembered correct - Richard Smith already fixed that with an
> patch to LibC++ years ago
>
> http://clang-developers.42468.n3.nabble.com/missing-optimization-opportunity-for-const-std-vector-compared-to-std-array-td4034587.html#none
>
>
> see post ~Jun 04, 2014; 12:16am
> http://clang-developers.42468.n3.nabble.com/missing-optimization-opportunity-for-const-std-vector-compared-to-std-array-tp4034587p4039822.html
>
> this post informed about the patch
> http://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20140602/106887.html
>
> what happend to this optimization - was it removed or broken over time?
> or is it just not possible to test the behavior on gcc.godbolt.org with
> the laste clang, missing flags or something?
>
> Am 20.10.2018 um 12:00 schrieb Dennis Luehring via cfe-dev:
> > 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
> >
> >
> > _______________________________________________
> > cfe-dev mailing list
> > [hidden email]
> > http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
>
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev