chalang
chalang copied to clipboard
proposal for opcodes for loading integers efficiently
Currently, loading an integer always takes 5 bytes. one byte to warn the interpreter that the next 4 bytes encode an integer, and then those 4 bytes of integer.
Looking at the code being produced by our compiler, we spend a lot of time referencing
- functions
- global variables
- local variables and for each of these things, we need to reference them by using integers. We tend to use the integers from 0-16 with high frequency.
So I want to add: 36 opcodes to load an integer from 0-35. so we can encode 0-35 in 1 byte. 1 opcode to indicate that the next byte is an integer. so we can encode 0-255 in 2 bytes. 1 opcode to indicate that the next 2 bytes are an integer. so we can encode 0-65535 in 3 bytes.
Historically speaking, bitcoin has opcodes to load the integers from 0-15 in one byte.
Looking at the smart contracts I have already written, and the bytecode being produced by the current version of the compiler, it seems like this update would reduce the size of the typical contract by about 1/2.
2-byte and 3-byte versions are now live in the experimental branch.
the 1 byte versions are now live in the experimental branch