Commit 91cb9edd authored by Keith Randall's avatar Keith Randall

cmd/compile: improve static map initialization

When static maps are large, we try to initialize them
by iterating over an array of key/value pairs.

Currently this optimization only works if the keys and values
are of primitive type.  This CL improves this optimization
by allowing any static composite literals as well.

Fixes #22010

Change-Id: Ie493e02ab8b8a228a3472b5c6025a33f7b92daf3
Reviewed-on: https://go-review.googlesource.com/66050
Run-TryBot: Keith Randall <khr@golang.org>
Reviewed-by: 's avatarMatthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent c82ee792
...@@ -941,7 +941,7 @@ func maplit(n *Node, m *Node, init *Nodes) { ...@@ -941,7 +941,7 @@ func maplit(n *Node, m *Node, init *Nodes) {
if r.Op != OKEY { if r.Op != OKEY {
Fatalf("maplit: rhs not OKEY: %v", r) Fatalf("maplit: rhs not OKEY: %v", r)
} }
if isliteral(r.Left) && isliteral(r.Right) { if isStaticCompositeLiteral(r.Left) && isStaticCompositeLiteral(r.Right) {
stat = append(stat, r) stat = append(stat, r)
} else { } else {
dyn = append(dyn, r) dyn = append(dyn, r)
...@@ -966,24 +966,14 @@ func maplit(n *Node, m *Node, init *Nodes) { ...@@ -966,24 +966,14 @@ func maplit(n *Node, m *Node, init *Nodes) {
vstatv := staticname(tv) vstatv := staticname(tv)
vstatv.Name.SetReadonly(true) vstatv.Name.SetReadonly(true)
for i, r := range stat { datak := nod(OARRAYLIT, nil, nil)
index := r.Left datav := nod(OARRAYLIT, nil, nil)
value := r.Right for _, r := range stat {
datak.List.Append(r.Left)
// build vstatk[b] = index datav.List.Append(r.Right)
setlineno(index)
lhs := nod(OINDEX, vstatk, nodintconst(int64(i)))
as := nod(OAS, lhs, index)
as = typecheck(as, Etop)
genAsStatic(as)
// build vstatv[b] = value
setlineno(value)
lhs = nod(OINDEX, vstatv, nodintconst(int64(i)))
as = nod(OAS, lhs, value)
as = typecheck(as, Etop)
genAsStatic(as)
} }
fixedlit(inInitFunction, initKindStatic, datak, vstatk, init)
fixedlit(inInitFunction, initKindStatic, datav, vstatv, init)
// loop adding structure elements to map // loop adding structure elements to map
// for i = 0; i < len(vstatk); i++ { // for i = 0; i < len(vstatk); i++ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment