c++ - virtual member functions are good or bad for locality in modern CPUs? -
considering new cpus new instructions moving , new memory controllers, if in c++ have vector of derived
objects derived
composed of virtual member functions, or bad thing locality ?
and if have vector of pointers base class base*
store references derived objects 1-2-3 level base
?
basically dynamic typing applies both cases, 1 better caching , memory access ?
i have preference between 2 see complete answer on subject.
there new consider ground-braking hardware industry in last 2-3 years ?
storing derived
rather base *
in vector better because eliminates 1 level of indirection , have objects laid out «together» in continuous memory, in turn makes life easier hardware prefetcher, helps paging, tlb misses, etc. however, if this, make sure don't introduce slicing problem.
as virtual dispatch in case, not matter exception of adjustment required «this» pointer. example, if derived
overrides virtual function calling , have pointer devied *
, «this» adjustment not required, , otherwise should adjusted 1 of base class`s «this» value (this depends on size of classes in inheritance hierarchy).
as long classes in vector have same overloads, cpu able predict what's going on. however, if have mix of different implementations, cpu have no clue function called every next object, , might cause performance issues.
and don't forget profile before , after make changes.
Comments
Post a Comment