https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104547

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #0)
> This is better:
> 
> void shrink_pop(std::vector<int>& v, unsigned n) {
>     while (n--)
>       v.pop_back();
> }
> 
> _Z10shrink_popRSt6vectorIiSaIiEEj:
> .LFB866:
>         .cfi_startproc
>         testl   %esi, %esi
>         je      .L10
>         movq    8(%rdi), %rax
>         movl    %esi, %esi
>         negq    %rsi
>         leaq    (%rax,%rsi,4), %rdx
>         .p2align 4,,10
>         .p2align 3
> .L12:
>         subq    $4, %rax
>         movq    %rax, 8(%rdi)
>         cmpq    %rax, %rdx
>         jne     .L12
> .L10:
>         ret
>         .cfi_endproc

This should be A LOT better, but see PR 96717 comment 6 and 7. It's still
better than the resize(v.size() - n) code even with that regression.

Clang's code for shrink_pop is:

_Z10shrink_popRSt6vectorIiSaIiEEm:      # @_Z10shrink_popRSt6vectorIiSaIiEEm
        .cfi_startproc
# %bb.0:
        testq   %rsi, %rsi
        je      .LBB0_2
# %bb.1:
        shlq    $2, %rsi
        subq    %rsi, 8(%rdi)
.LBB0_2:
        retq

I was expecting something like that for v.resize(v.size() - n) too.

Reply via email to