Glossary
What is NaN boxing?
NaN boxing is a trick for representing every value of a dynamic language in a single 64-bit word. Numbers are stored as ordinary doubles; everything else hides inside the many bit patterns that all mean "not a number" (NaN), which real arithmetic never produces in those forms.
How it works
An IEEE 754 double is a NaN when its 11 exponent bits are all ones and its fraction is not zero. That leaves about 51 bits free in a "quiet" NaN. Pointers on common 64-bit systems use only about 48 bits, so a pointer fits, with a sign bit or a few spare bits to tag what kind of value it is. nil, true and false get their own fixed patterns.
Checking "is this a number?" becomes one mask and compare, and the value needs no separate type tag, so it is half the size of a tagged union.
In Crafting Interpreters
clox starts with a tagged union for values (a type field plus a union), which is clear but 16 bytes each. Chapter 30, Optimization swaps in NaN boxing behind the same macros, and the benchmarks speed up because more values fit in cache. It is optional in the sense that the rest of clox does not depend on it.
Questions
Is NaN boxing portable?
Mostly, on 64-bit systems where pointers use 48 bits or so. It assumes IEEE 754 doubles and a pointer layout that fits, so it is a deliberate trade of portability for speed.