5 ms·
A C implementation of objc_msgSend would look like: ... objc_msgSend(id self, SEL _cmd, ...) { fptr = ...lookup code... return fptr(self, _
by mikeash 9y ago
A C implementation of objc_msgSend would look like:
... objc_msgSend(id self, SEL _cmd, ...) {
fptr = ...lookup code...
return fptr(self, _cmd, args...)
}
There's no way to express that args... argument when calling the function pointer, and no way to express forwarding an arbitrary return value.
However, Objective-C does not require objc_msgSend. With objc_msgSend, a method call site generates code that's essentially equivalent to (for a method that takes one object parameter and returns void):
((void (*)(id, SEL, id))objc_msgSend)(object, selector, parameter);
In other words, take objc_msgSend, cast it to a function pointer of the correct type, and call it.
Instead of objc_msgSend, the runtime can provide a function which looks up the method implementation and returns it to the caller. The caller can then invoke that implementation itself. This is how the GNU runtime does it, since it needs to be more portable. Their lookup function is called objc_msg_lookup. The generated code would look like this:
void (*imp)(id, SEL, id) = (void (*)(id, SEL, id))objc_msgLookup(object, selector);
imp(object, selector, parameter);
However, each call now suffers the overhead of two function calls, so it's a bit slower. Apple prefers to put in the extra effort of writing assembly code to avoid this, since it's so critical to their platform.
- tom_mellior 9y ago> There's no way to express that args... argument when calling the function pointer Yes there is: va_list. > no way to express forwarding an arbitrary return value Of course there is, and lots and lots of language runtimes implemented in C use those ways. Usually it boils down to having a base type called Object or Value and passing around pointers to that. In fact, from your example it looks like the "id" type is meant to play this role. This is not syntax checked, but the code above would be something like: Object *objc_msgSend(id self, SEL cmd, ...) { fptr = ...lookup code... va_list args; va_start(cmd, args); Object *result = fptr(self, cmd, args); va_end(args); return result; } Yes, this can be faster in assembly, but it's not true that there is no way to express this. (Unless I'm misunderstanding something.)
- connorcpu 9y agoThe missing bit is that objc_msgSend doesn't know how many parameters are being forwarded, and fptr is just a normal function on the other end, it isn't expecting a va_list, it expects arguments to be passed in the C ABI exactly how they're passed to objc_msgSend
- mikeash 9y agoThese are ways to simulate it. Of course you can simulate it; the language is Turing-complete, after all. But it does not actually do it. You can write something similar to objc_msgSend in C, but you cannot write objc_msgSend in C. Using varargs and passing va_list into the method would mean that your method is no longer a plain C function with the declared parameters plus two hidden parameters. It's now a different sort of beast, and has to use va_ calls to extract the values. This would require a lot more work in the method, and hurt performance. Returning everything as an object would mean boxing and unboxing primitive values at every call, which would be horrendously inefficient. And if you don't care about extracting every last bit of performance, it's much easier to do the lookup approach I discussed than it is to faff around with varargs and wrapping return values.
- CodeWriter23 9y ago@mikeash it looks like you might have a topic for an upcoming Friday.
- tom_mellior 9y ago> These are ways to simulate it. Come on. Taking an argument list in an argument list object and passing that argument list as an argument list to a function is exactly what this was about. It's not a "simulation". It's a C feature for capturing and passing around argument lists. It actually does it. > has to use va_ calls to extract the values. This would require a lot more work in the method Loads from the stack at fixed offsets for every argument instead of having some of the arguments in registers and loading others from the stack at fixed offsets. Yes, that is more work. > Returning everything as an object would mean boxing and unboxing primitive values at every call, which would be horrendously inefficient. True, the runtimes I was thinking of box many things. But you can type-pun pointers to other things, so you don't necessarily have to box everything. I don't know enough about Objective-C's constraints, but I do note that the linked article did talk about using tagged pointers already.
- lgg 9y agoIt actually is not the extra function call that is the big hit, since if you think about it objc_msgSend also does two calls (the call to msgSend, which at the end then tail calls the imp). The dynamic instruction count is also roughly the same. In fact objc_msgLookup actually ends up being faster in a some micro benches since it plays a lot better with modern CPU branch predictors: objc_msgSend defeats them by making every call site jump to the same dispatch function, which then makes a completely unpredictable jump to the imp. By using msgLookup you essentially decouple the branch source from the lookup which greatly improves predictably. Also, with a “sufficiently smart” compiler it can be win because it allows you to do things like hoist the lookup out of loops, etc (essentially really clever automated IMP caching tricks). There are also a number of minor regressions, like now you are doing some of the work on a stack frame (which might require spilling if you need a register, vs avoiding spills by using exclusively non-preserved registers in an assembly function that tail calls). In the end what kills it is that the profiles of most objC is large flat sections that do not really benefit from the compiler tricks or the improved prediction, and the added call site instructions end up in increased binary sizes and negative CPU i-cache impacts.
- mikeash 9y agoInteresting! Making two separate calls at the call site would have some extra overhead compared to what objc_msgSend does. The caller needs to load the self and _cmd arguments twice, for example, and stash the IMP somewhere convenient in between the two calls. If objc_msg_lookup has a standard prologue and epilogue then you'll end up running two of those each time. You'll push and pop two return addresses on the stack rather than just one. However, I'll happily accept that these are probably pretty small costs, especially since so much of it is just register gains which probably result in cost-free renamings in the hardware. It makes sense that the i-cache impact is more important.
- dfox 9y agoHaving lookup instead of send as the primitive operation also allows you to generate code like this for the call site: ({ static SEL last_isa = NULL; static IMP last_imp = NULL; if (object->isa != isa){ last_isa = object->isa; last_imp = lookup(object->isa, sel); } last_isa(object, selector, arguments...); }) (modulo the fact that you cannot generate this by dumb string substitution without compiler extension like gcc's ({...})) Smalltalk/X takes this to the extreme by compiling all sends into code like: { static struct cache = {.imp = &magic_global_method, .class=NULL} cache->imp(&cache, object, selector, arguments...); } And generates something like this into every method prologue: if (cache && self->isa != cache->class){ cache->class = self->isa; cache->imp = lookup(object, selector); return cache->imp(NULL, object, selector, arguments...) } It looks convoluted and uses one additional word of stack space per call, but does not contain any unpredictable indirect branches in the fast path (and in fact reduces overall code size as it can be expected that there are many more sends than methods).