3

I am currently writing a Maya 2013 plugin in c++ that optimizes the geometry of given mesh for specific constraints. i need to achieve maximum performance. for now i only implemented an hard-coded version of the algorithm for a specific constraint and it's very fast, but i need achieve some king of generic implementation of the algorithm for any given constraint (thus i need some kind of polymorphism).

a pseudo code for the general structure of the algorithm is:

(1) do k times:
(2)      for every face fj in the mesh do: 
(3)            some manipulation on the vertices qi incident to the face fj
(4)      for every vertex  vi in the mesh do: 
(5)            some manipulation  on the faces fj incident to the vertex vi 

the problem is that in my specific implementation there is no function calls for the calculations in steps 3 and 5 in the pseudo code, when i tried calculating those steps with auxiliary functions there was a big reduction on performance.

for a generic implementation i need to make function calls in steps 3 and 5 for every constraint.

i thought of two kinds of solutions:

  1. write a generic class for constraints. derive classes for every constraint i need with methods for calculaing steps 3 and 5 in the algorithm.
  2. write a general functor (class with only the () operator) and derive from it functors for calculating steps 3 and 5. the reason for this idea is that the call for the () operator in a functor is inlined (answer about reusable code in this link).

first question:

is there any way to reduce the overhead for the function calls?

second question:

is there any way to make the compiler always inline the functions in the two solutions above? from this question i know that virtual functions called from pointers to objects can't be inlined.

third question:

is there a more efficient way then the solutions i proposed?

Community
  • 1
  • 1
DontCareBear
  • 825
  • 2
  • 11
  • 25
  • 1
    `operator()` is just a regular member function with a funny name. It has no more or less chance to get inlined than a member function with any other name. In particular, when it's virtual for a good reason, this chance is virtually zero. – n. m. could be an AI Aug 29 '13 at 03:23

1 Answers1

1

I would recommend you take look at templates and pass a template function/functor as a parameter.
With dynamic polymorphism you almost always have indirect call to function, ie the body of the function isn't known to compiler - hence optimization options are limited. Templates allow you to do what is called a static polymorphism.

see these links for more info:

Static polymorphism definition and implementation

C++ templates for performance?

Community
  • 1
  • 1
Anycorn
  • 50,217
  • 42
  • 167
  • 261
  • is it possible to inline static functions inside classes that are using static polymorphism? – DontCareBear Aug 29 '13 at 03:40
  • yes. however keep in mind that inline keyword is merely a hint. Most of compilers will inline a small function regardless. your best bet is to check assembly to be sure. – Anycorn Aug 29 '13 at 03:52
  • thank you very much!! i have a few more questions :P i need a list of derived classes. how to i keep such a list if i use static polymorphism? because the derived classes take the shape base. is it possible to take a variable number of template parameters? – DontCareBear Aug 29 '13 at 03:59
  • 1
    I am not sure what you are asking but it sounds vaguely like CRTP pattern. You can have variable number of template parameters in C++11 but a lot of times you can emulate that using default template parameters. – Anycorn Aug 29 '13 at 04:03
  • yes i asked about CRTP pattern. after i read your answer i searched about static polymorphism and found about CRTP in Wikipedia. i think i will use it to optimize performance in my code, thank you for directing me to static polymorphism and CRTP pattern, great c++ tricks!!!!!! – DontCareBear Aug 29 '13 at 04:09
  • @DontCareBear np man. if you want to see some really cool stuff, check out Eigen - my hunch you are in scientific computing and it may be of use to you. – Anycorn Aug 29 '13 at 18:52
  • i use it for my project already :) very very good library!!!! the first time i implemented my algorithm was on matlab and it took half a minute to run. with Eigen and parallel computing (open mp) it was instantaneous (40 milliseconds or something like that maybe much less)!!! – DontCareBear Aug 29 '13 at 21:29