Skip to content

Commit ee59c06

Browse files
committed
cmd/compile: evaluate map initializers incrementally
For the code: m := map[int]int { a(): b(), c(): d(), e(): f(), } We used to do: t1 := a() t2 := b() t3 := c() t4 := d() t5 := e() t6 := f() m := map[int]int{} m[t1] = t2 m[t3] = t4 m[t5] = t6 After this CL we do: m := map[int]int{} t1 := a() t2 := b() m[t1] = t2 t3 := c() t4 := d() m[t3] = t4 t5 := e() t6 := f() m[t5] = t6 Ordering the initialization this way limits the lifetime of the temporaries involved. In particular, for large maps the number of simultaneously live temporaries goes from ~2*len(m) to ~2. This change makes the compiler (regalloc, mostly) a lot happier. The compiler runs faster and uses a lot less memory. For #26546, changes compile time of a big map from 8 sec to 0.5 sec. Fixes #26552 Update #26546 Change-Id: Ib7d202dead3feaf493a464779fd9611c63fcc25f Reviewed-on: https://go-review.googlesource.com/c/go/+/174417 Run-TryBot: Keith Randall <khr@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
1 parent 998cc2a commit ee59c06

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/cmd/compile/internal/gc/order.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,58 @@ func (o *Order) expr(n, lhs *Node) *Node {
12391239
n.Left = o.addrTemp(n.Left)
12401240
n.Right = o.addrTemp(n.Right)
12411241
}
1242+
case OMAPLIT:
1243+
// Order map by converting:
1244+
// map[int]int{
1245+
// a(): b(),
1246+
// c(): d(),
1247+
// e(): f(),
1248+
// }
1249+
// to
1250+
// m := map[int]int{}
1251+
// m[a()] = b()
1252+
// m[c()] = d()
1253+
// m[e()] = f()
1254+
// Then order the result.
1255+
// Without this special case, order would otherwise compute all
1256+
// the keys and values before storing any of them to the map.
1257+
// See issue 26552.
1258+
entries := n.List.Slice()
1259+
statics := entries[:0]
1260+
var dynamics []*Node
1261+
for _, r := range entries {
1262+
if r.Op != OKEY {
1263+
Fatalf("OMAPLIT entry not OKEY: %v\n", r)
1264+
}
1265+
if isStaticCompositeLiteral(r.Left) && isStaticCompositeLiteral(r.Right) {
1266+
statics = append(statics, r)
1267+
} else {
1268+
dynamics = append(dynamics, r)
1269+
}
1270+
}
1271+
n.List.Set(statics)
1272+
1273+
// Note: we don't need to recursively call order on the statics.
1274+
// But do it anyway, just in case that's not true in the future.
1275+
o.exprList(n.List)
1276+
1277+
if len(dynamics) == 0 {
1278+
break
1279+
}
1280+
1281+
// Emit the creation of the map (with all its static entries).
1282+
m := o.newTemp(n.Type, false)
1283+
as := nod(OAS, m, n)
1284+
typecheck(as, ctxStmt)
1285+
o.stmt(as)
1286+
n = m
1287+
1288+
// Emit eval+insert of dynamic entries, one at a time.
1289+
for _, r := range dynamics {
1290+
as := nod(OAS, nod(OINDEX, n, r.Left), r.Right)
1291+
typecheck(as, ctxStmt) // Note: this converts the OINDEX to an OINDEXMAP
1292+
o.stmt(as)
1293+
}
12421294
}
12431295

12441296
lineno = lno

src/cmd/compile/internal/gc/sinit.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,11 @@ func anylit(n *Node, var_ *Node, init *Nodes) {
10801080
default:
10811081
Fatalf("anylit: not lit, op=%v node=%v", n.Op, n)
10821082

1083+
case ONAME:
1084+
a := nod(OAS, var_, n)
1085+
a = typecheck(a, ctxStmt)
1086+
init.Append(a)
1087+
10831088
case OPTRLIT:
10841089
if !t.IsPtr() {
10851090
Fatalf("anylit: not ptr")

0 commit comments

Comments
 (0)