Commit 09812610 authored by Martin Möhrmann's avatar Martin Möhrmann

cmd/compile: preserve escape information for map literals

While some map literals were marked non-escaping that information
was lost when creating the corresponding OMAKE node which made map
literals always heap allocated.

Copying the escape information to the corresponding OMAKE node allows
stack allocation of hmap and a map bucket for non escaping map literals.

Fixes #21830

Change-Id: Ife0b020fffbc513f1ac009352f2ecb110d6889c9
Reviewed-on: https://go-review.googlesource.com/62790
Run-TryBot: Martin Möhrmann <moehrmann@google.com>
Reviewed-by: default avatarDaniel Martí <mvdan@mvdan.cc>
Reviewed-by: default avatarKeith Randall <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 1a3230f2
...@@ -931,6 +931,7 @@ func slicelit(ctxt initContext, n *Node, var_ *Node, init *Nodes) { ...@@ -931,6 +931,7 @@ func slicelit(ctxt initContext, n *Node, var_ *Node, init *Nodes) {
func maplit(n *Node, m *Node, init *Nodes) { func maplit(n *Node, m *Node, init *Nodes) {
// make the map var // make the map var
a := nod(OMAKE, nil, nil) a := nod(OMAKE, nil, nil)
a.Esc = n.Esc
a.List.Set2(typenod(n.Type), nodintconst(int64(n.List.Len()))) a.List.Set2(typenod(n.Type), nodintconst(int64(n.List.Len())))
litas(m, a, init) litas(m, a, init)
......
...@@ -650,6 +650,13 @@ var testNonEscapingMapVariable int = 8 ...@@ -650,6 +650,13 @@ var testNonEscapingMapVariable int = 8
func TestNonEscapingMap(t *testing.T) { func TestNonEscapingMap(t *testing.T) {
n := testing.AllocsPerRun(1000, func() { n := testing.AllocsPerRun(1000, func() {
m := map[int]int{}
m[0] = 0
})
if n != 0 {
t.Fatalf("mapliteral: want 0 allocs, got %v", n)
}
n = testing.AllocsPerRun(1000, func() {
m := make(map[int]int) m := make(map[int]int)
m[0] = 0 m[0] = 0
}) })
......
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