• Martin Möhrmann's avatar
    cmd/compile: optimize append(x, make([]T, y)...) slice extension · a8a60ac2
    Martin Möhrmann authored
    Changes the compiler to recognize the slice extension pattern
    
      append(x, make([]T, y)...)
    
    and replace it with growslice and an optional memclr to avoid an allocation for make([]T, y).
    
    Memclr is not called in case growslice already allocated a new cleared backing array
    when T contains pointers.
    
    amd64:
    name                      old time/op    new time/op    delta
    ExtendSlice/IntSlice         103ns ± 4%      57ns ± 4%   -44.55%  (p=0.000 n=18+18)
    ExtendSlice/PointerSlice     155ns ± 3%      77ns ± 3%   -49.93%  (p=0.000 n=20+20)
    ExtendSlice/NoGrow          50.2ns ± 3%     5.2ns ± 2%   -89.67%  (p=0.000 n=18+18)
    
    name                      old alloc/op   new alloc/op   delta
    ExtendSlice/IntSlice         64.0B ± 0%     32.0B ± 0%   -50.00%  (p=0.000 n=20+20)
    ExtendSlice/PointerSlice     64.0B ± 0%     32.0B ± 0%   -50.00%  (p=0.000 n=20+20)
    ExtendSlice/NoGrow           32.0B ± 0%      0.0B       -100.00%  (p=0.000 n=20+20)
    
    name                      old allocs/op  new allocs/op  delta
    ExtendSlice/IntSlice          2.00 ± 0%      1.00 ± 0%   -50.00%  (p=0.000 n=20+20)
    ExtendSlice/PointerSlice      2.00 ± 0%      1.00 ± 0%   -50.00%  (p=0.000 n=20+20)
    ExtendSlice/NoGrow            1.00 ± 0%      0.00       -100.00%  (p=0.000 n=20+20)
    
    Fixes #21266
    
    Change-Id: Idc3077665f63cbe89762b590c5967a864fd1c07f
    Reviewed-on: https://go-review.googlesource.com/109517
    Run-TryBot: Martin Möhrmann <moehrmann@google.com>
    TryBot-Result: Gobot Gobot <gobot@golang.org>
    Reviewed-by: 's avatarJosh Bleecher Snyder <josharian@gmail.com>
    a8a60ac2
slices.go 1.43 KB