diff options
author | Keith Randall <khr@golang.org> | 2016-05-29 11:16:13 -0700 |
---|---|---|
committer | Keith Randall <khr@golang.org> | 2016-08-16 17:19:48 +0000 |
commit | 1faea596e4f94e0a4170eda3d1a217b4936d8aa6 (patch) | |
tree | 9604dcfae425134a466159429e784d5de0c73a7c | |
parent | 35e25ef62efc5917481e11ff6e7a5cc12468b0e2 (diff) | |
download | go-1faea596e4f94e0a4170eda3d1a217b4936d8aa6.tar.gz go-1faea596e4f94e0a4170eda3d1a217b4936d8aa6.zip |
cmd/compile: add size hint to map literal allocations
Might as well tell the runtime how large the map is going to be.
This avoids grow work and allocations while the map is being built.
Will wait for 1.8.
Fixes #15880
Fixes #16279
Change-Id: I377e3e5ec1e2e76ea2a50cc00810adda20ad0e79
Reviewed-on: https://go-review.googlesource.com/23558
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
-rw-r--r-- | src/cmd/compile/internal/gc/sinit.go | 2 | ||||
-rw-r--r-- | src/cmd/compile/internal/gc/syntax.go | 5 |
2 files changed, 6 insertions, 1 deletions
diff --git a/src/cmd/compile/internal/gc/sinit.go b/src/cmd/compile/internal/gc/sinit.go index 4469d71f1c..72c06dde2d 100644 --- a/src/cmd/compile/internal/gc/sinit.go +++ b/src/cmd/compile/internal/gc/sinit.go @@ -866,7 +866,7 @@ func maplit(ctxt int, n *Node, var_ *Node, init *Nodes) { nerr := nerrors a := Nod(OMAKE, nil, nil) - a.List.Set1(typenod(n.Type)) + a.List.Set2(typenod(n.Type), Nodintconst(int64(len(n.List.Slice())))) litas(var_, a, init) // count the initializers diff --git a/src/cmd/compile/internal/gc/syntax.go b/src/cmd/compile/internal/gc/syntax.go index df9f838e77..58f95e82c9 100644 --- a/src/cmd/compile/internal/gc/syntax.go +++ b/src/cmd/compile/internal/gc/syntax.go @@ -545,6 +545,11 @@ func (n *Nodes) Set1(node *Node) { n.slice = &[]*Node{node} } +// Set2 sets n to a slice containing two nodes. +func (n *Nodes) Set2(n1, n2 *Node) { + n.slice = &[]*Node{n1, n2} +} + // MoveNodes sets n to the contents of n2, then clears n2. func (n *Nodes) MoveNodes(n2 *Nodes) { n.slice = n2.slice |