home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 31,958 of 33,346   
   Seungbeom Kim to All   
   Re: How to avoid repeated run-time looku   
   29 Feb 12 03:53:39   
   
   8438ed98   
   From: musiphil@bawi.org   
      
   On 2012-02-24 15:50, GH wrote:   
   >   
   >    while (! gameOver()) { // (1)   
   >      for (vector::const_iterator i=players.begin(); i!   
   > =players.end(); ++i)  // (2)   
   >        (*i)->play(this);  // (3)   
   >    }   
   [...]   
   >   
   > At (3), there is some overhead because of the virtual function play(),   
   > which is quite significant when play() itself is simple. But the   
   > runtime resolution for the virtual function for each element in the   
   > vector needs to be done only once at the first iteration of loop (1).   
   > Is there a good way to achieve that?   
      
   If you knew their dynamic types "statically", then you could bypass the   
   run-time resolution by writing (*i)->PlayerDerivedX::play(this)), but   
   I guess that's not the case here.   
      
   If you're thinking something along the line of:   
      
        for (i: players) {   
            store the result of the run-time resolution for (*i)->play(this);   
        }   
        while (!gameOver()) {   
            for (i: players) {   
                call (*i)->[use the stored resolution]::play(this);   
            }   
        }   
      
   I guess the standard virtual call should be as efficient as that.   
   If the run-time resolution were as in some other languages where any   
   overriding functions are "searched" along the class hierarchy in run-time,   
   the lookup would be quite expensive and it would be worthwhile to "cache"   
   the results. However, in C++, the lookup can(*) be a simple pointer   
   indirection, and should be as efficient as any other manual mechanism   
   you could write to use cached results. In other words, the "cache" is   
   already implemented as the vptrs in the objects.   
      
   (*) I say "can" because implementations are not required to be that   
        efficient, but AFAIK all popular implementations are already so.   
      
   Of course, this is on the instruction level, and things that happen   
   below that level (e.g. branch prediction) are a different story. :)   
   If you care that much, you might need something like a self-modifying   
   process resolving the virtual calls and replacing them with static calls.   
      
   --   
   Seungbeom Kim   
      
      
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca