Integer as Byte Array - pinknetworkx/atomicassets-contract GitHub Wiki
Note: This is not being used in the current version of the code.
Integer as Byte Array
In the presets table, max_supply and issued_supply are stored as byte vectors, despite them actually representing 64 bit unsigned integers. This is done in an effort to save on RAM costs, especially considering that we expect the majority of presets not to have any max_supply at all.
The encoding is simple; A uint64 is looked at as 8 bytes, and these 8 bytes are pushed to the byte vector, starting with the lowest value byte. Trailing zero bytes are however not stored. For example
100 = 00 00 00 00 00 00 00 64
-> [64]
= [100]
1000 = 00 00 00 00 00 00 03 E8
-> [E8, 03]
= [232, 3]
4294967296 = 00 00 00 01 00 00 00 00
-> [00, 00, 00, 00, 01]
= [0, 0, 0, 0, 1]
As vectors (of length < 128) only cost one byte of RAM on top of that data cost, using this kind of encoding will only cost one more byte than using an integer type, and will save RAM costs in the vast majority of cases where the max_supply is either 0 or relatively low.