mirror of
https://github.com/golang/go
synced 2024-09-15 22:20:06 +00:00
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 <adonovan@google.com>
This commit is contained in:
parent
390d1ce686
commit
621aa713d4
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue