[haiku-gsoc] Re: locale kit patch : use haiku hash map instead of stl one.

  • From: Stephan Assmus <superstippi@xxxxxx>
  • To: haiku-gsoc@xxxxxxxxxxxxx
  • Date: Tue, 21 Jul 2009 22:37:07 +0200

Hi,

On 2009-07-21 at 21:10:16 [+0200], Oliver Tappe <zooey@xxxxxxxxxxxxxxx> 
wrote:
> On 2009-07-21 at 16:55:02 [+0200], Stephan Assmus <superstippi@xxxxxx> 
> wrote:
> > 
> [ ... ]
> > 
> > This code works fine, but you can save some cycles if you change it 
> > like this:
> > 
> > -    CatMap::const_iterator iter;
> > -    for (iter = fCatMap.begin(); res == B_OK && iter != fCatMap.end();
> > ++iter) {
> > +    CatMap::Iterator iter = fCatMap.GetIterator();
> > +    while (res == B_OK && iter.HasNext()) {
> >          archive.MakeEmpty();
> > -        res = archive.AddString("c:ostr", iter->first.fString.String())
> > -            || archive.AddString("c:ctxt", 
> > iter->first.fContext.String())
> > -            || archive.AddString("c:comt", 
> > iter->first.fComment.String())
> > -            || archive.AddInt32("c:hash", iter->first.fHashVal)
> > -            || archive.AddString("c:tstr", iter->second.String());
> > +        const CatMap::Entry& entry = iter.Next();
> 
> This can't work since iter.Next() does return a temp value. So while in 
> general the hint is good, of course, it can't be applied in this case.

Wow, sorry about that. That tip came from memory, and maybe HashMap should 
be improved instead. I remember that it needs to return an 
uninitialzed/invalid Entry when there is out of bounds access, but this 
could as well be a reference to some kind of const "Null" entry.


> [ ... ]
> > 
> > You declare the entry right where you need it, which is even something 
> > in our coding style, and thereby save the operator=() invokation. 
> 
> Well, I hope that our coding style does not recommend declaring variables 
> inside of loops unless necessary, as that should really waste a couple of 
> cycles instead of saving them (because of the additional invocations of 
> constructors and destructors on that variable).
> 
> Or am I missing something?

The compiler should usually notice the possibility for optimization, no? 
But when I am in doubt and I really want to save the cycles, I declare the 
variable outside. However, in most situations, it would work as I proposed, 
or it's a while loop that has the object declared outside anyway, like 
BDirectory::GetNextEntry()...

Best regards,
-Stephan

Other related posts: