tr1::hashtable::operator[]
Paolo Carlini
pcarlini@suse.de
Sat May 13 18:22:00 GMT 2006
Hi Peter,
> operator[](const K& k)
> {
> Hashtable* h = static_cast<Hashtable*>(this);
> - typename Hashtable::iterator it =
> - h->insert(std::make_pair(k, mapped_type())).first;
> +
> + typename Hashtable::iterator it = h->find(k);
> + if (it == h->end())
> + it = h->insert(std::make_pair(k, mapped_type())).first;
> return it->second;
> }
> };
>
> I guess if it was ok to have a bit of extra overhead when using
> operator[] for only inserting in std::map than it would be fine here
> as well.
Frankly, I'm not fully convinced, I think we should work on this a
little more. For one, ext/hash_map (the closest relative) also doesn't
use find. Moreover, map, which indeed do use find (lower_bound) then
calls *insert with hint*. It seems to me that in our case we are not
talking about "a bit of extra overhead", because we are essentially
doing an additional completely redundant lookup (in case the element is
not present).
Maybe a slightly larger rework of this code is in order, something very
similar to the current insert but delaying the construction of the pair...
Did you benchmark the effect of your patch together with a lightweight
mapped_type()?
Paolo.
More information about the Libstdc++
mailing list