13 ms·
There's a big difference between the data structures, yes, but the basic logic for message sends is still pretty similar. They both boil down to a simple lookup
by chc 11y ago
There's a big difference between the data structures, yes, but the basic logic for message sends is still pretty similar. They both boil down to a simple lookup in a data structure and a jump. Would you really characterize the difference between a vtable and a hash table as "you actually pass a message, that first must be interpreted" in the case of a hash table?
- fleitz 11y agoI think the biggest issue is that in a vtable you jump directly to the method by index, and with objc_msgsend it looks up a string in a hash table. Mostly the difference is ints vs. strings in my opinion. As well in C++ if you don't mark your method virtual you can bypass all the vtable crap. I love ObjC but method resolution is far more expensive than in C++, especially non-virtual methods.
- astrange 11y agoMethod selectors in Obj-C don't have to be strings, as long as you can turn a string into one occasionally. You do still need to do a hash lookup to get the method implementation, since the selector table changes at runtime and objects have to safely respond to unhandled selectors and all that.
- fleitz 11y agoI meant string as in char* not NSString, selectors specifically the SEL datatype are strings They certainly don't have to be, however, they are. SEL selector = @selector(applicationDidBecomeActive:); printf("%s",(char*)(void*)selector); // Prints applicationDidBecomeActive: