This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
tr1::hashtable::operator[]
- From: Peter Doerfler <gcc at pdoerfler dot com>
- To: libstdc++ at gcc dot gnu dot org
- Date: Sat, 13 May 2006 17:10:53 +0200
- Subject: tr1::hashtable::operator[]
Hi.
Sorry about the incremental nature of these suggestions/patches. I'm
finding things by testing different use cases.
Now, I've been testing an unordered_map where making a pair takes
considerable amounts of time.
(it's <const int, unordered_map<const int, int> >)
Doing the same with std::map was about factor 2 faster.
The reason for this lies in map_base::operator[] which calls insert
without checking if the key is already in the unordered_map. Using
std::map as a model I applied the following patch.
===================================================================
--- hashtable (revision 113737)
+++ hashtable (working copy)
@@ -680,8 +680,10 @@
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.
Like this my testcase is about 30% faster with unordered_map compared to
std::map (and about factor 12 faster that ext/hash_map BTW )
Best regards,
Peter