Skip to content

Commit aaa3d39

Browse files
committed
cmd/compile: include all entries in map literal hint size
Currently we only include static entries in the hint for sizing the map when allocating a map for a map literal. Change that to include all entries. This will be an overallocation if the dynamic entries in the map have equal keys, but equal keys in map literals are rare, and at worst we waste a bit of space. Fixes #43020 Change-Id: I232f82f15316bdf4ea6d657d25a0b094b77884ce Reviewed-on: https://go-review.googlesource.com/c/go/+/383634 Run-TryBot: Keith Randall <[email protected]> Trust: Keith Randall <[email protected]> Trust: Josh Bleecher Snyder <[email protected]> Reviewed-by: Josh Bleecher Snyder <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent d40e7bb commit aaa3d39

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

src/cmd/compile/internal/ir/expr.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,10 @@ type CompLitExpr struct {
202202
Ntype Ntype
203203
List Nodes // initialized values
204204
Prealloc *Name
205-
Len int64 // backing array length for OSLICELIT
205+
// For OSLICELIT, Len is the backing array length.
206+
// For OMAPLIT, Len is the number of entries that we've removed from List and
207+
// generated explicit mapassign calls for. This is used to inform the map alloc hint.
208+
Len int64
206209
}
207210

208211
func NewCompLitExpr(pos src.XPos, op Op, typ Ntype, list []Node) *CompLitExpr {

src/cmd/compile/internal/walk/complit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ func maplit(n *ir.CompLitExpr, m ir.Node, init *ir.Nodes) {
419419
// make the map var
420420
a := ir.NewCallExpr(base.Pos, ir.OMAKE, nil, nil)
421421
a.SetEsc(n.Esc())
422-
a.Args = []ir.Node{ir.TypeNode(n.Type()), ir.NewInt(int64(len(n.List)))}
422+
a.Args = []ir.Node{ir.TypeNode(n.Type()), ir.NewInt(n.Len + int64(len(n.List)))}
423423
litas(m, a, init)
424424

425425
entries := n.List

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,15 @@ func (o *orderState) expr1(n, lhs ir.Node) ir.Node {
14331433
typecheck.Stmt(as) // Note: this converts the OINDEX to an OINDEXMAP
14341434
o.stmt(as)
14351435
}
1436+
1437+
// Remember that we issued these assignments so we can include that count
1438+
// in the map alloc hint.
1439+
// We're assuming here that all the keys in the map literal are distinct.
1440+
// If any are equal, this will be an overcount. Probably not worth accounting
1441+
// for that, as equal keys in map literals are rare, and at worst we waste
1442+
// a bit of space.
1443+
n.Len += int64(len(dynamics))
1444+
14361445
return m
14371446
}
14381447

test/codegen/maps.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,33 @@ func MapClearSideEffect(m map[int]int) int {
122122
}
123123
return k
124124
}
125+
126+
func MapLiteralSizing(x int) (map[int]int, map[int]int) {
127+
// amd64:"MOVL\t[$]10,"
128+
m := map[int]int{
129+
0: 0,
130+
1: 1,
131+
2: 2,
132+
3: 3,
133+
4: 4,
134+
5: 5,
135+
6: 6,
136+
7: 7,
137+
8: 8,
138+
9: 9,
139+
}
140+
// amd64:"MOVL\t[$]10,"
141+
n := map[int]int{
142+
0: x,
143+
1: x,
144+
2: x,
145+
3: x,
146+
4: x,
147+
5: x,
148+
6: x,
149+
7: x,
150+
8: x,
151+
9: x,
152+
}
153+
return m, n
154+
}

0 commit comments

Comments
 (0)