compiler: optimize map initialization with PACKMAP

This commit is contained in:
Roman Khimov 2021-11-12 16:43:59 +03:00
parent a8befeea33
commit 0d50730382

View file

@ -1793,14 +1793,14 @@ func (c *codegen) convertByteArray(elems []ast.Expr) {
} }
func (c *codegen) convertMap(lit *ast.CompositeLit) { func (c *codegen) convertMap(lit *ast.CompositeLit) {
emit.Opcodes(c.prog.BinWriter, opcode.NEWMAP) l := len(lit.Elts)
for i := range lit.Elts { for i := l - 1; i >= 0; i-- {
elem := lit.Elts[i].(*ast.KeyValueExpr) elem := lit.Elts[i].(*ast.KeyValueExpr)
emit.Opcodes(c.prog.BinWriter, opcode.DUP)
ast.Walk(c, elem.Key)
ast.Walk(c, elem.Value) 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) { func (c *codegen) getStruct(typ types.Type) (*types.Struct, bool) {