neo-devpack-dotnet icon indicating copy to clipboard operation
neo-devpack-dotnet copied to clipboard

Is there a way to create a ByteString from an array of bytes w/o converting to buffer?

Open devhawk opened this issue 3 years ago • 0 comments

It's very common to create a ByteString from a byte array to use as a storage key. But NCCS inserts an extraneous CONVERT instruction when you do this. Given that CONVERT is one of the most expensive opcodes (8192), there should be a way to construct a byte string without converting it to a buffer needlessly.

Example, a typical NEP-17 TotalSupply method implemetnation:

[Safe]
public static BigInteger TotalSupply() 
{
    return (BigInteger)Storage.Get(Storage.CurrentContext, new byte[] { Prefix_TotalSupply });
}

NCCS converts this to the following NeoVM instructions (inline and optimization enabled):

# Method Start TotalSupply.DevHawk.Contracts.ApocToken
# Code Apoc.cs line 50: "return (BigInteger)Storage.Get(Storage.CurrentContext, new byte[] { Prefix_TotalSupply });"
0013 PUSHDATA1 00 # as text: ""
0016 CONVERT 30 # Buffer type
0018 SYSCALL 9B-F6-67-CE # System.Storage.GetContext SysCall
0023 SYSCALL 92-5D-E8-31 # System.Storage.Get SysCall
0028 DUP
0029 ISNULL
0030 JMPIFNOT 04 # pos: 34 (offset: 4)
0032 DROP
0033 PUSH0
0034 CONVERT 21 # Integer type
0036 JMP 02 # pos: 38 (offset: 2)
# Code Apoc.cs line 51: "}"
0038 RET
# Method End TotalSupply.DevHawk.Contracts.ApocToken

devhawk avatar Feb 07 '23 19:02 devhawk