From 0d5073038245fb389f25a4ea7d140a775c92c9a3 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 12 Nov 2021 16:43:59 +0300 Subject: [PATCH] compiler: optimize map initialization with PACKMAP --- pkg/compiler/codegen.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index 2328e8f50..e295498e1 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -1793,14 +1793,14 @@ func (c *codegen) convertByteArray(elems []ast.Expr) { } func (c *codegen) convertMap(lit *ast.CompositeLit) { - emit.Opcodes(c.prog.BinWriter, opcode.NEWMAP) - for i := range lit.Elts { + l := len(lit.Elts) + for i := l - 1; i >= 0; i-- { elem := lit.Elts[i].(*ast.KeyValueExpr) - emit.Opcodes(c.prog.BinWriter, opcode.DUP) - ast.Walk(c, elem.Key) ast.Walk(c, elem.Value) - emit.Opcodes(c.prog.BinWriter, opcode.SETITEM) + ast.Walk(c, elem.Key) } + emit.Int(c.prog.BinWriter, int64(l)) + emit.Opcodes(c.prog.BinWriter, opcode.PACKMAP) } func (c *codegen) getStruct(typ types.Type) (*types.Struct, bool) {