From 00f4cacb4994ba9004b79a1dd3329dd827785667 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20M=C3=B6hrmann?= Date: Sat, 25 Feb 2017 00:34:21 +0100 Subject: [PATCH] cmd/compile: reduce allocs when appending to Node slices MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Rewrite Append function such that the *Node slice argument does not escape. Passes toolstash -cmp. name old alloc/op new alloc/op delta Template 40.8MB ± 0% 40.8MB ± 0% -0.17% (p=0.000 n=20+19) Unicode 30.3MB ± 0% 30.2MB ± 0% -0.11% (p=0.000 n=19+20) GoTypes 115MB ± 0% 115MB ± 0% -0.20% (p=0.000 n=20+20) Compiler 492MB ± 0% 491MB ± 0% -0.25% (p=0.000 n=20+20) SSA 858MB ± 0% 858MB ± 0% -0.08% (p=0.000 n=20+20) Flate 26.2MB ± 0% 26.2MB ± 0% -0.13% (p=0.000 n=20+19) GoParser 32.5MB ± 0% 32.4MB ± 0% -0.14% (p=0.000 n=20+20) Reflect 80.6MB ± 0% 80.4MB ± 0% -0.27% (p=0.000 n=20+20) Tar 27.3MB ± 0% 27.3MB ± 0% -0.12% (p=0.000 n=20+19) XML 43.1MB ± 0% 43.0MB ± 0% -0.14% (p=0.000 n=20+20) name old allocs/op new allocs/op delta Template 400k ± 1% 397k ± 0% -0.81% (p=0.000 n=20+18) Unicode 321k ± 1% 320k ± 0% -0.43% (p=0.000 n=20+20) GoTypes 1.17M ± 0% 1.16M ± 0% -0.89% (p=0.000 n=20+20) Compiler 4.59M ± 0% 4.54M ± 0% -1.26% (p=0.000 n=20+19) SSA 7.68M ± 0% 7.65M ± 0% -0.37% (p=0.000 n=18+18) Flate 242k ± 1% 240k ± 1% -0.70% (p=0.000 n=20+20) GoParser 323k ± 1% 321k ± 1% -0.64% (p=0.000 n=20+20) Reflect 1.01M ± 0% 1.00M ± 0% -0.92% (p=0.000 n=20+19) Tar 258k ± 1% 256k ± 1% -0.60% (p=0.000 n=20+19) XML 403k ± 1% 400k ± 0% -0.78% (p=0.000 n=20+20) Change-Id: Ie1eb603dc46f729574f6a76c08085b2619249be4 Reviewed-on: https://go-review.googlesource.com/37437 Reviewed-by: Brad Fitzpatrick Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- src/cmd/compile/internal/gc/syntax.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/cmd/compile/internal/gc/syntax.go b/src/cmd/compile/internal/gc/syntax.go index eb610119ae..b1cf638530 100644 --- a/src/cmd/compile/internal/gc/syntax.go +++ b/src/cmd/compile/internal/gc/syntax.go @@ -637,16 +637,17 @@ func (n Nodes) Addr(i int) **Node { } // Append appends entries to Nodes. -// If a slice is passed in, this will take ownership of it. func (n *Nodes) Append(a ...*Node) { if len(a) == 0 { return } if n.slice == nil { - n.slice = &a - } else { - *n.slice = append(*n.slice, a...) + s := make([]*Node, len(a)) + copy(s, a) + n.slice = &s + return } + *n.slice = append(*n.slice, a...) } // Prepend prepends entries to Nodes.