[fpc-devel] Using case statement instead of VTable

Jonas Maebe jonas at freepascal.org
Tue Apr 11 12:23:30 CEST 2023



On 2023-04-11 07:23, Hairy Pixels via fpc-devel wrote:
> Strange question but I heard it brought up during a discussion and I 
> was very curious what compiler people think about it.
> 
> The question is, instead of using a virtual method table would it be 
> feasible to use a case statement on the real class type and make a 
> dispatch routine for each method call?

This is sometimes done by compilers when value profiling determines that 
a particular function pointer call (almost) always goes to the same 
target.

It can also be done as part of whole program optimisation, as Marco 
mentioned. FWIW, when WPO devirtualisation is applied to the compiler 
(which can be devirtualised almost completely because of the way it is 
constructed), LLVM can apply your proposed optimisation (and some 
others) and gets 8% extra performance. So don't expect miracles from it, 
unless your main loop consists of only calling tiny virtual methods.


Jonas


More information about the fpc-devel mailing list