From 3658299f449e63026caf08a02eba855aab6755b6 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Sun, 12 Nov 2017 22:11:51 -0800 Subject: [PATCH] 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 TryBot-Result: Gobot Gobot Reviewed-by: Keith Randall --- src/runtime/mbarrier.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/runtime/mbarrier.go b/src/runtime/mbarrier.go index c071728900..c446db93d2 100644 --- a/src/runtime/mbarrier.go +++ b/src/runtime/mbarrier.go @@ -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