vftable performance penalty vs. switch statement

前端 未结 5 1457
遥遥无期
遥遥无期 2020-12-24 02:00

C++ question here. I have a system where I\'m going to have hundreds of mini-subclasses of a given superclass. They all will have a \"foo\" method that does something. Or

5条回答
  •  甜味超标
    2020-12-24 02:49

    Vtable should be faster in nearly all cases, but if performance is so critical, the right thing to ask is by how much.

    Vtable call is triple indirection (three memory accesses to get the target CALL address). Cache misses should not be an issue if there're many calls. So, it is roughly 2-3 switch label comparisons (though the latter offer even less chance for CPU cache miss, but less for pipe usage).

    You should of course not rely on anything I said here, and test it all with true performance measurements on your target architecture.

提交回复
热议问题