From: Josh Bleecher Snyder Date: Wed, 23 Mar 2016 22:27:23 +0000 (-0700) Subject: cmd/compile: avoid allocating a Nodes for readonly method receivers X-Git-Tag: go1.7beta1~1120 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=fc4358951a451c18267c4eb9ef0fb941963cc995;p=gostls13.git cmd/compile: avoid allocating a Nodes for readonly method receivers We were allocating a Nodes for common method calls that did not modify the Nodes. Though there is no clear wall time impact, this significantly reduces the number of allocations, so it seems worth doing. Passes toolstash -cmp. name old alloc/op new alloc/op delta Template 59.0MB ± 0% 58.6MB ± 0% -0.81% (p=0.000 n=25+25) Unicode 41.4MB ± 0% 41.3MB ± 0% -0.18% (p=0.000 n=25+25) GoTypes 198MB ± 0% 197MB ± 0% -0.80% (p=0.000 n=24+25) Compiler 875MB ± 0% 865MB ± 0% -1.09% (p=0.000 n=25+25) name old allocs/op new allocs/op delta Template 581k ± 0% 520k ± 0% -10.42% (p=0.000 n=25+25) Unicode 413k ± 0% 403k ± 0% -2.30% (p=0.000 n=25+25) GoTypes 1.78M ± 0% 1.58M ± 0% -11.18% (p=0.000 n=25+25) Compiler 7.66M ± 0% 6.47M ± 0% -15.51% (p=0.000 n=25+25) Change-Id: I012a9f4b333821bdf61b4f2bdff4ce5c3b5d3057 Reviewed-on: https://go-review.googlesource.com/21056 Reviewed-by: Brad Fitzpatrick Reviewed-by: Ian Lance Taylor --- diff --git a/src/cmd/compile/internal/gc/syntax.go b/src/cmd/compile/internal/gc/syntax.go index d4a26c459b..3e8452b4c7 100644 --- a/src/cmd/compile/internal/gc/syntax.go +++ b/src/cmd/compile/internal/gc/syntax.go @@ -376,7 +376,7 @@ type Nodes struct{ slice *[]*Node } // Slice returns the entries in Nodes as a slice. // Changes to the slice entries (as in s[i] = n) will be reflected in // the Nodes. -func (n *Nodes) Slice() []*Node { +func (n Nodes) Slice() []*Node { if n.slice == nil { return nil } @@ -384,7 +384,7 @@ func (n *Nodes) Slice() []*Node { } // Len returns the number of entries in Nodes. -func (n *Nodes) Len() int { +func (n Nodes) Len() int { if n.slice == nil { return 0 } @@ -393,19 +393,19 @@ func (n *Nodes) Len() int { // Index returns the i'th element of Nodes. // It panics if n does not have at least i+1 elements. -func (n *Nodes) Index(i int) *Node { +func (n Nodes) Index(i int) *Node { return (*n.slice)[i] } // First returns the first element of Nodes (same as n.Index(0)). // It panics if n has no elements. -func (n *Nodes) First() *Node { +func (n Nodes) First() *Node { return (*n.slice)[0] } // Second returns the second element of Nodes (same as n.Index(1)). // It panics if n has fewer than two elements. -func (n *Nodes) Second() *Node { +func (n Nodes) Second() *Node { return (*n.slice)[1] } @@ -432,13 +432,13 @@ func (n *Nodes) MoveNodes(n2 *Nodes) { // SetIndex sets the i'th element of Nodes to node. // It panics if n does not have at least i+1 elements. -func (n *Nodes) SetIndex(i int, node *Node) { +func (n Nodes) SetIndex(i int, node *Node) { (*n.slice)[i] = node } // Addr returns the address of the i'th element of Nodes. // It panics if n does not have at least i+1 elements. -func (n *Nodes) Addr(i int) **Node { +func (n Nodes) Addr(i int) **Node { return &(*n.slice)[i] }