In an AI application I am writing in C++,
- there is not much numerical computation
- there are lot of structures for which run-time polymorphism is needed
- very often, several polymorphic structures interact during computation
In such a situation, are there any optimization techniques? While I won’t care to optimize the application just now, one aspect of selecting C++ over Java for the project was to enable more leverage to optimize and to be able to use non-object oriented methods (templates, procedures, overloading).
In particular, what are the optimization techniques related to virtual functions? Virtual functions are implemented through virtual tables in memory. Is there some way to pre-fetch these virtual tables onto L2 cache (the cost of fetching from memory/L2 cache is increasing)?
Apart from this, are there good references for data locality techniques in C++? These techniques would reduce the wait time for data fetch into L2 cache needed for computation.
Update: Also see the following related forums: Performance Penalty for Interface, Several Levels of Base Classes
Virtual functions are very efficient. Assuming 32 bit pointers the memory layout is approximately:
The classptr points to memory that is typically on the heap, occasionally on the stack, and starts with a four byte pointer to the vtable for that class. But the important thing to remember is the vtable itself is not allocated memory. It’s a static resource and all objects of the same class type will point to the exactly the same memory location for their vtable array. Calling on different instances won’t pull different memory locations into L2 cache.
This example from msdn shows the vtable for class A with virtual func1, func2, and func3. Nothing more than 12 bytes. There is a good chance the vtables of different classes will also be physically adjacent in the compiled library (you’ll want to verify this is you’re especially concerned) which could increase cache efficiency microscopically.
The other performance concern would be instruction overhead of calling through a vtable function. This is also very efficient. Nearly identical to calling a non-virtual function. Again from the example from msdn:
In this example ebp, the stack frame base pointer, has the variable
A* paat zero offset. The register eax is loaded with the value at location [ebp], so it has the A*, and edx is loaded with the value at location [eax], so it has class A vtable. Then ecx is loaded with [ebp], because ecx represents ‘this’ it now holds the A*, and finally the call is made to the value at location [edx+8] which is the third function address in the vtable.If this function call was not virtual the mov eax and mov edx would not be needed, but the difference in performance would be immeasurably small.