4 ms·
Looks very similar to the way in which numbers / strings / objects are represented in Javascript (and earlier in some Lisps). The nice thing is that for pointe
by nine_k 11y ago
Looks very similar to the way in which numbers / strings / objects are represented in Javascript (and earlier in some Lisps).
The nice thing is that for pointers, tagging is "free", for the lowest 4 bits of a 16-byte aligned pointer are not used anyway.
- cpr 11y agoSure, free except that every dereference has to clear out those bits, unless the target architecture has a "pointer deref ignoring lower n bits" operator/operand.
- bdash 11y agoThe tag bits are zero when using the pointer as a pointer, so nothing special needs to be done in order to dereference them. The tag bits are only non-zero when non-pointer data is packed into the pointer, and in that case you inherently need special handling anyway.
- cpr 11y agoAh, right. Brain infarction.
- mikeash 11y agoIt does mean that every dereference needs to check for taggedness, though. For example, newer versions of objc_msgSend check the low bit at the beginning of the call before it proceeds to the normal message send path (or not, if it actually is a tagged pointer).
- userbinator 11y agoNot necessarily every dereference, but only those in which the value could be something other than a pointer.
- mikeash 11y agoDo you mean because it already did the check before, and it could remember the results? Certainly it could. Unfortunately this isn't an option in Objective-C because the compiler can't make any assumptions about the tagged pointer implementation, and that means objc_msgSend has to start fresh at each invocation. In theory you could have sub-functions, like objc_msgSend_really_object and objc_msgSend_tagged_pointer that the compiler could generate calls to directly, but then you'd have to support whatever assumptions the compiler made forever. I'm guessing/hoping that with modern branch prediction, the check at the top of objc_msgSend is a tiny penalty. I'm sure Apple measured the heck out of it, anyway.
- apaprocki 11y agoIt's more complicated than that -- x86-64 systems only use 48 bits of addressable space. JS engines use all the remaining bits for tagging, not just bits resulting from 8- or 16-byte alignment. This works great until you want to run on another CPU architecture that actually has 64-bits of addressable space and uses it regularly...
- jhallenworld 11y agoI'm surprised they need so many bits.. you really just need to know if the item is an object (so the type can be embedded in the object), or a primitive type. But there are not so many primitive types. Some dynamically typed systems put the tag at the beginning of a page: so all items of a particular type are allocated out of a page tagged with that type. You don't waste any bits from the pointer or data, but you do have to do some masking and worse, a remote memory access to get the type. Probably safest and fastest to do something like: struct { int type; union { char *s; int n; double d; }};
- bdash 11y agoMany JavaScript engines use IEEE-754 double-precision floats as their immediate value type, and fit pointers and integer within the NaN payload of the double representation. See https://wingolog.org/archives/2011/05/18/value-representation-in-javascript-implementations https://wingolog.org/archives/2011/05/18/value-representatio... for a discussion of a few similar approaches and the tradeoffs of each.