]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: short-circuit typedslicecopy when dstp == srcp
authorJosh Bleecher Snyder <josharian@gmail.com>
Mon, 13 Nov 2017 06:11:51 +0000 (22:11 -0800)
committerJosh Bleecher Snyder <josharian@gmail.com>
Thu, 15 Feb 2018 15:05:15 +0000 (15:05 +0000)
If copying from a slice to itself, skip the write barriers
and actual memory copies.

This happens in practice in code like this snippet from
the trim pass in the compiler, when k ends up being 0:

copy(s.Values[k:], s.Values[:m])

Change-Id: Ie6924acfd56151f874d87f1d7f1f74320b4c4f10
Reviewed-on: https://go-review.googlesource.com/94023
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
src/runtime/mbarrier.go

index c0717289001482fd0946f97a2ebc4f73dcd6b8ab..c446db93d2ab25a152720b5b0048474d63250e48 100644 (file)
@@ -254,6 +254,10 @@ func typedslicecopy(typ *_type, dst, src slice) int {
                cgoCheckSliceCopy(typ, dst, src, n)
        }
 
+       if dstp == srcp {
+               return n
+       }
+
        // Note: No point in checking typ.kind&kindNoPointers here:
        // compiler only emits calls to typedslicecopy for types with pointers,
        // and growslice and reflect_typedslicecopy check for pointers