compiler: do not load map key twice in for-range loop
This commit is contained in:
parent
1847c28b42
commit
e54149d547
1 changed files with 10 additions and 2 deletions
|
@ -924,16 +924,24 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
|
|||
emit.Opcode(c.prog.BinWriter, opcode.OVER)
|
||||
emit.Jmp(c.prog.BinWriter, opcode.JMPLEL, end)
|
||||
|
||||
var keyLoaded bool
|
||||
needValue := n.Value != nil && n.Value.(*ast.Ident).Name != "_"
|
||||
if n.Key != nil && n.Key.(*ast.Ident).Name != "_" {
|
||||
if isMap {
|
||||
c.rangeLoadKey()
|
||||
if needValue {
|
||||
emit.Opcode(c.prog.BinWriter, opcode.DUP)
|
||||
keyLoaded = true
|
||||
}
|
||||
} else {
|
||||
emit.Opcode(c.prog.BinWriter, opcode.DUP)
|
||||
}
|
||||
c.emitStoreVar(n.Key.(*ast.Ident).Name)
|
||||
}
|
||||
if n.Value != nil && n.Value.(*ast.Ident).Name != "_" {
|
||||
c.rangeLoadKey()
|
||||
if needValue {
|
||||
if !isMap || !keyLoaded {
|
||||
c.rangeLoadKey()
|
||||
}
|
||||
if isMap {
|
||||
// we have loaded only key from key array, now load value
|
||||
emit.Int(c.prog.BinWriter, 4)
|
||||
|
|
Loading…
Reference in a new issue