Tagged Pointer Strings (2015) (via) Mike Ash digs into a fascinating implementation detail of macOS.
Tagged pointers provide a way to embed a literal value in a pointer reference. Objective-C pointers on macOS are 64 bit, providing plenty of space for representing entire values. If the least significant bit is 1 (the pointer is a 64 bit odd number) then the pointer is "tagged" and represents a value, not a memory reference.
Here's where things get really clever. Storing an integer value up to 60 bits is easy. But what about strings?
There's enough space for three UTF-16 characters, with 12 bits left over. But if the string fits ASCII we can store 7 characters.
Drop everything except a-z A-Z.0-9
and we need 6 bits per character, allowing 10 characters to fit in the pointer.
Apple take this a step further: if the string contains just eilotrm.apdnsIc ufkMShjTRxgC4013
("b" is apparently uncommon enough to be ignored here) they can store 11 characters in that 60 bits!
Recent articles
- Teresa T is name of the whale in Pillar Point Harbor near Half Moon Bay - 8th September 2024
- Calling LLMs from client-side JavaScript, converting PDFs to HTML + weeknotes - 6th September 2024
- Building a tool showing how Gemini Pro can return bounding boxes for objects in images - 26th August 2024