4 ms·
Even the method cache seems expensive -- compared to an indirect function call (using a virtual method table). Can clang replace msgSend with direct calls when
by nimrody 9y ago
Even the method cache seems expensive -- compared to an indirect function call (using a virtual method table). Can clang replace msgSend with direct calls when the destination class is known at compilation time? (perhaps with a guard to verify that the object class is as expected)
- valleyer 9y agoNo, this never happens. (Method implementations can be replaced at runtime.)
- thought_alarm 9y agoYou have that option in performance-critical situations. The ObjC runtime does allow you to lookup a method's underlying function pointer ahead of time in order to bypass obj_msgSend. The compiler can't do that automatically because the any method and any class can be replaced at any time. Key-value observing is a common feature that replaces method implementations on the fly at runtime.
- plorkyeran 9y agoKVO creates a new subclass and changes your object to that subclass rather than replacing methods on the original class, so checking the isa pointer would be sufficient for that case. Method swizzling would break, but I suspect that most obj-c code could be compiled without support for swizzling without breaking anything.
- icodestuff 9y agoSwizzling is not the only thing that would break. You'd break categories in dynamically loaded frameworks or bundles, and yes, you'd still break KVO because it replaces -dealloc and -class on the newly-created classes. You'd also break dynamically adding methods to classes.
- CodeWriter23 9y agoWhat is a typical use case for dynamically adding methods to a class?
- chrisseaton 9y agoNo - and this is the advantage of JIT compilers which can do that and why they can sometimes outperform static compilers.