3 ms·
Calling via a function pointer isn't devirtualization. It's still an indirect call, and does not allow the function to be inlined since the actual function is n
by plorkyeran 11y ago
Calling via a function pointer isn't devirtualization. It's still an indirect call, and does not allow the function to be inlined since the actual function is not known until runtime. It merely gets you from something 2-4x as expensive as a c++ virtual call to something roughly as expensive.
- mpweiher 11y agoLast I checked, a C++ virtual function call loads the method pointer via an indirect memory load. Hard for the CPU to speculate through, so typically a pipeline stall. A function pointer that's in a local variable, so loaded into a register is a completely different beast, as the measurements bear out. In my measurements, a message-send is ~40% slower than a C++ virtual method call, whereas an IMP-cached function call is ~40% faster, and slightly faster than a regular function call.
- plorkyeran 11y agoThe vtable for an object being actively used will be in L1 cache, and when a virtual function is called in a loop, Intel's CPUs have been able to predict the target for many, many years. ARM may not; I've never had recent to deeply investigate virtual call performance on iOS. Finding calling via a function pointer to be faster than calling directly suggests that you were not actually measuring what you thought you were measuring.