1
0
mirror of https://github.com/golang/go synced 2024-10-02 02:18:33 -06:00

runtime: short-circuit typedslicecopy when dstp == srcp

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>
This commit is contained in:
Josh Bleecher Snyder 2017-11-12 22:11:51 -08:00
parent bf9f1c1503
commit 3658299f44

View 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