Emacs internals: Tagged pointers vs. C++ std:variant and LLVM (Part 3)
2 days ago
- #tagged-pointer
- #memory-layout
- #dynamic-typing
- Emacs represents Lisp values in a 64-bit slot called Lisp_Object, using the lowest 3 bits as a type tag due to 8-byte alignment.
- Tagged unions (like std::variant) store type information inline but can be memory inefficient if type sizes vary greatly.
- Tagged pointers (used in Emacs) utilize unused bits in pointers for type tags, limiting the number of types but saving memory.
- Emacs extends its type system with 'Poor Man's Inheritance' by embedding a common header in structs to allow for more complex types.
- LLVM uses a similar approach to Emacs with Custom RTTI, avoiding standard C++ RTTI for performance reasons.
- Other systems like Linux Kernel, LuaJIT, V8, and PostgreSQL also use tagged pointers or similar techniques for efficiency.
- Different memory layouts (tagged unions, fat pointers, tagged pointers) serve different performance and memory constraints.