Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
devhawk opened this issue Feb 7, 2023 · 0 comments

Comments

@devhawk
Copy link
Contributor

devhawk commented Feb 7, 2023

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant