Can't sleep, who wants to hear about how high-performance interpreters store objects?

Basically they sneak data into unused bit-patterns in floating point numbers.
Most floating point implementations use IEEE 754 representation. Double-precision floats are 64 bits and stored like this:

1 bit: sign (whether it's -ive or +ive)
11 bits: exponent (where the point should go)
52 bits: significand (the numbers in the float)
You may have encountered NaN, which stands for Not a Number. IEEE 754 double NaNs are any bit pattern where the 11 exponent bits are all 1.

This means that there's 52 bits which just don't matter*. So there's approximately 4 gazillion NaN representation.
*actually there's a couple classes of NaNs: signalling NaNs, which produce some kind of hardware exception, and quiet NaNs, which don't. These are differentiated by the most significant bit in the significand (wow so significant)
So we have a whole 51 bits to play with. What can we do with that?

Well 64-bit pointers typically only use 48-bits of the representation space. This means we could store a whole pointer in a NaN and still have room to spare.
We can then use the extra few bits as a tag which tells you the type of the object which is stored in the NaN. This way an interpreter can tell the type of an object by doing a single bitmask and shift.
There's an incredible amount you can do with this space.

- Embed a 32-bit integer in your NaN for faster arithmetic.
- Store offsets into a string table.
- Implement complex object lookup schemes with part of a key embedded in the object.
- Whatever you can fit in those bits.
If you want more details, search for NaNboxing, which is the name for this technique. There are variants used in all the major Javascript engines, LuaJIT, etc., and you can find explanations of the different schemes they all use.
*correction: V8 uses tagged pointers instead of NaNboxing
Another correction from Steve, who has likely forgotten more about floating point than I've ever learned https://twitter.com/stephentyrone/status/1296945535765696515?s=19
Anyway my kid is gonna wake me up in like 5 hours so I'm gonna a try and get some sleep, wish me luck ily đź’ś
You can follow @TartanLlama.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled:

By continuing to use the site, you are consenting to the use of cookies as explained in our Cookie Policy to improve your experience.