near / near-sdk-contract-tools

Helpful functions and macros for developing smart contracts on NEAR Protocol.
https://crates.io/crates/near-sdk-contract-tools
Apache License 2.0
41 stars 12 forks source link

Account ID compression #122

Closed encody closed 1 year ago

encody commented 1 year ago

The character set for NEAR Account IDs is 38 + 1 separator, so 39 distinct characters.

This can be trivially compressed into $\lceil log_2(39) \rceil = 6$ bits per character, a 25% savings.

Additionally, since account IDs are limited to a max length of 64, we only need to store one byte for length. (This might break Borsh spec.)

Therefore, using only trivial compression, we should be able to optimize the binary representation of an implicit account ID from Borsh's 4 (length) + 64 (value) = 68 bytes to 1 (length) + 64 * .75 (value) = 49 bytes, almost 28% smaller.