Performance hit of vtable lookup in C++

前端 未结 6 1188
日久生厌
日久生厌 2020-12-24 03:52

I\'m evaluating to rewrite a piece of real-time software from C/assembly language to C++/assembly language (for reasons not relevant to the question parts of the code are ab

6条回答
  •  Happy的楠姐
    2020-12-24 04:26

    This is unrelated to your question, but if you are that keen on performance you could use templates to do a loop unroll for the todolist:

    void (*todo[3])(void *);
    void *param[3];
    
    void f1(void*) {std::cout<<"1" << std::endl;}
    void f2(void*) {std::cout<<"2" << std::endl;}
    void f3(void*) {std::cout<<"3" << std::endl;}
    
    template
    struct Obj {
        static void apply()
        {
            todo[N-1](param[N-1]);
            Obj::apply();
        }
    };
    
    template<> struct Obj<0> { static void apply() {} };
    
    todo[0] = f1;
    todo[1] = f2;
    todo[2] = f3;
    
    Obj::apply();
    

提交回复
热议问题