From 098126103e861264bc746e8de1dae2063ad7ba71 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Martin=20M=C3=B6hrmann?= Date: Sun, 10 Sep 2017 19:36:38 +0200 Subject: [PATCH] cmd/compile: preserve escape information for map literals MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Daniel Martí Reviewed-by: Keith Randall TryBot-Result: Gobot Gobot --- src/cmd/compile/internal/gc/sinit.go | 1 + src/runtime/map_test.go | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/src/cmd/compile/internal/gc/sinit.go b/src/cmd/compile/internal/gc/sinit.go index f38692e310..27863b323b 100644 --- a/src/cmd/compile/internal/gc/sinit.go +++ b/src/cmd/compile/internal/gc/sinit.go @@ -931,6 +931,7 @@ func slicelit(ctxt initContext, n *Node, var_ *Node, init *Nodes) { func maplit(n *Node, m *Node, init *Nodes) { // make the map var a := nod(OMAKE, nil, nil) + a.Esc = n.Esc a.List.Set2(typenod(n.Type), nodintconst(int64(n.List.Len()))) litas(m, a, init) diff --git a/src/runtime/map_test.go b/src/runtime/map_test.go index 1d1de3f740..a3a21e2f80 100644 --- a/src/runtime/map_test.go +++ b/src/runtime/map_test.go @@ -650,6 +650,13 @@ var testNonEscapingMapVariable int = 8 func TestNonEscapingMap(t *testing.T) { 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[0] = 0 }) -- 2.50.0