Merge pull request #2256 from nspcc-dev/optimize-gas-cost-of-conversion

compiler: optimize GAS cost of type conversion
This commit is contained in:
Roman Khimov 2021-11-15 10:21:13 +03:00 committed by GitHub
commit f742bb6dd2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1272,7 +1272,7 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
goTyp := c.typeOf(n.Type) goTyp := c.typeOf(n.Type)
if canConvert(goTyp.String()) { if canConvert(goTyp.String()) {
typ := toNeoType(goTyp) typ := toNeoType(goTyp)
emit.Instruction(c.prog.BinWriter, opcode.CONVERT, []byte{byte(typ)}) c.emitConvert(typ)
} }
return nil return nil
} }
@ -1763,6 +1763,9 @@ func transformArgs(fs *funcScope, fun ast.Expr, args []ast.Expr) []ast.Expr {
// emitConvert converts top stack item to the specified type. // emitConvert converts top stack item to the specified type.
func (c *codegen) emitConvert(typ stackitem.Type) { func (c *codegen) emitConvert(typ stackitem.Type) {
emit.Opcodes(c.prog.BinWriter, opcode.DUP)
emit.Instruction(c.prog.BinWriter, opcode.ISTYPE, []byte{byte(typ)})
emit.Instruction(c.prog.BinWriter, opcode.JMPIF, []byte{2 + 2}) // After CONVERT.
emit.Instruction(c.prog.BinWriter, opcode.CONVERT, []byte{byte(typ)}) emit.Instruction(c.prog.BinWriter, opcode.CONVERT, []byte{byte(typ)})
} }