From 621aa713d46c3eebbc03e352436d109f28779e47 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Mon, 28 Mar 2016 14:12:10 -0700 Subject: [PATCH] cmd/compile: avoid allocation in Nodes.Set in common case When building make.bash, calling Nodes.Set(s) where len(s) == 0 occurs 4738678 times vs 1465415 calls where len(s) > 0; i.e., it is over 3x more common to set Nodes.slice to nil rather than to s. Make a copy of slice (header) and take address of that copy instead to avoid allocating the argument slice on the heap always even when not needed. Saves 4738678 slice header allocations and slice header value copies. Change-Id: I88e8e919ea9868ceb2df46173d187af4109bd947 Reviewed-on: https://go-review.googlesource.com/21241 Reviewed-by: Alan Donovan --- src/cmd/compile/internal/gc/syntax.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/cmd/compile/internal/gc/syntax.go b/src/cmd/compile/internal/gc/syntax.go index 3e8452b4c7..76f3123ebf 100644 --- a/src/cmd/compile/internal/gc/syntax.go +++ b/src/cmd/compile/internal/gc/syntax.go @@ -415,7 +415,11 @@ func (n *Nodes) Set(s []*Node) { if len(s) == 0 { n.slice = nil } else { - n.slice = &s + // Copy s and take address of t rather than s to avoid + // allocation in the case where len(s) == 0 (which is + // over 3x more common, dynamically, for make.bash). + t := s + n.slice = &t } } -- 2.48.1