]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: avoid allocation in Nodes.Set in common case
authorRobert Griesemer <gri@golang.org>
Mon, 28 Mar 2016 21:12:10 +0000 (14:12 -0700)
committerRobert Griesemer <gri@golang.org>
Mon, 28 Mar 2016 21:41:11 +0000 (21:41 +0000)
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 <adonovan@google.com>
src/cmd/compile/internal/gc/syntax.go

index 3e8452b4c75c32abfa4580aa9af4a862a8fd863c..76f3123ebfd6640dae453b89a826d965f2ec4025 100644 (file)
@@ -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
        }
 }