News for hackers. Stuff that matters.
Re:64-bit ARM chips in iPhone 5S serve up taste of Intel-fre... by Someone on Wednesday September 11, 2013 @03:06PM (#6369239)
In Mac OS and iOS, 64 bit is a big gain because it more than doubles increases the space avaiable for data in tagged pointers (http://objectivistc.tumblr.com/post/7872364181/tagged-pointe..., http://www.mikeash.com/pyblog/friday-qa-2012-07-27-lets-buil...), making the approach usable in practice.

Given that Apple designs its own CPUs, I guess it would make sense to have a specific instruction for 'read 64 bits from this pointer, or, if it is a tagged pointer, extract the value from the tag'. Is that a good idea?

Re:64-bit ARM chips in iPhone 5S serve up taste of Intel-fre... by mikeash on Wednesday September 11, 2013 @08:28PM (#6371162)
The question of hardware support for tagged pointers is interesting. Currently, objc_msgSend basically does (in pseudo-C):

    if(self & 1) {
        tagged_class_index = self & 0xf;
        class = tagged_class_table[tagged_class_index]
    } else {
        class = *self;
    }
And the rest is all common code that looks up the method implementation and jumps to it.

Within the method implementation itself, there will be some shifting to extract the value, but not much, since the method implementation already knows what class it's in:

    value = self >> 4;
Producing new "instances" of the class as tagged pointers involves some shifting and logical ORing:

    new_tagged_pointer = value << 4 | class_index;
(Note that class_index is conceptually a 3-bit value, but the runtime treats it as a 4-bit value for which the bottom bit is always 1. The tagged_class_table has 16 entries but only the odd-numbered ones are used.)

Most of this is really fast on any modern CPU. The one part that could potentially be slow is the if statement up at the top, since that's a candidate for mispredicted branches. I don't know how much it matters, but I'd say that that would be the one place where a dedicated instruction might potentially be of use. You could eliminate the branch yourself, except for the fact that dereferencing self when it's a tagged pointer will likely crash, so you have to avoid reading that value when you aren't going to use it.

My guess is that the penalty for the branch there is small, but you'd have to do some benchmarking to say for sure. If modern CPUs are indeed able to predict that branch (or run both sides to completion internally, since they're short), then there's no need for a new instruction.