c++ - In generic object update loop, is it better to update per controller or per object? -


i'm writing generic code have vector of objects being updated set of controllers.

the code bit complex in specific context simplification be:

template< class t > class controller {  public:     virtual ~controller(){}     virtual void update( t& ) = 0;     // , potentially other functions used in other cases update }  template< class t > class group { public:     typedef std::shared_ptr< controller<t> > controllerptr;      void add_controller( controllerptr );    // register controller     void remove_controller( controllerptr ); // remove controller      void update(); // udpate objects using controllers  private:      std::vector< t > m_objects;     std::vector< controllerptr > m_controllers; }; 

i intentionally didn't use std::function because can't use in specific case. intentionally use shared pointers instead of raw pointers, not important question actually.

anyway here it's update() implementation interest me. can 2 ways.

a) each controller, update objects.

template< class t > void group<t>::update() {     for( auto& controller : m_controllers )         for( auto& object : m_objects )             controller->update( object ); } 

b) each object, update applying controllers.

template< class t > void group<t>::update() {     for( auto& object : m_objects )         for( auto& controller : m_controllers )             controller->update( object ); } 

"measure! measure! measure!" , agree, can't measure don't use. problem it's generic code. don't know size of t, assume not gigantic, maybe small, maybe still bit big. can't assume t other designed contained in vector. don't know how many controllers or t instances used. in current use cases, there different counts.

the question is: which solution efficient in general?

i'm thinking cache coherency here. also, assume code used on different compilers , platforms.

my guts tells me updating instruction cache faster updating data cache, make solution b) more efficient in general. however, learnt not trust gusts when have doubts performance, i'm asking here.

the solution i'm getting allow user choose (using compile-time policy) update implementation use each group instance, want provide default policy , can't decide 1 efficient of cases.

we have living proof modern compilers (intel c++ in particular) able swap loops, shouldn't matter you.

i have remembered great @mysticial's answer:

intel compiler 11 miraculous. interchanges 2 loops, thereby hoisting unpredictable branch outer loop. not immune mispredictions, twice fast whatever vc++ , gcc can generate!

wikipedia article topic

detecting whether loop interchange can done requires checking if swapped code produce same results. in theory possible prepare classes won't allow swap, again, possible prepare classes benefit either version more.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -