Reading — step 1 of 5
Read
~1 min readComposite Types
Arrays
Arrays are length-prefixed sequences of values.
- fixarray:
0x90..0x9f— top 4 bits1001, low 4 bits = length (0-15). - array 16:
0xdc+ 2 byte length BE. - array 32:
0xdd+ 4 byte length BE.
Then: each element encoded recursively.
Example: [1, 2, 3]:
- fixarray of length 3:
0x93. - elements:
01 02 03(each is positive fixint). - Total:
93 01 02 03(4 bytes).
vs JSON [1,2,3] (7 bytes).
Decoding:
python
Heterogeneous arrays: each element can be any type. JSON-style.
Nested arrays: recursive. [[1,2],[3,4]]:
92(outer length 2)92(first inner length 2)01 0292(second inner length 2)03 04- Total: 7 bytes.
Length limits:
- fixarray: 15 elements.
- array 16: 65535.
- array 32: 4 billion.
If exceeded, use bigger format.
Encoder strategy:
- If fits in fixarray: use it.
- Else: smallest size class.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…