This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
PATCH RFA: Make hashtable::clear return quickly if table is empty
- From: Ian Lance Taylor <iant at google dot com>
- To: gcc-patches at gcc dot gnu dot org, libstdc++ at gcc dot gnu dot org
- Cc: iant at google dot com
- Date: Fri, 27 Mar 2009 22:38:11 -0700
- Subject: PATCH RFA: Make hashtable::clear return quickly if table is empty
This is a patch for the old hashtable class used for the old hash_map
and hash_set classes. These classes are superseded by the newer tr1
unordered_map and unordered_set classes, but they are still widely used.
Classes and functions can use hash tables which are only used in some
code paths. When those code paths are not followed, the empty hash
table will be destroyed when the class is destroyed or the function
returns. Destroying a hash table calls clear, which walks through all
the buckets. This is a waste of time if the hash table is empty.
Testing whether it is empty is a fast test, permitting a fast return
when empty. The test will take very little time when the hash table is
not empty, particularly since the _M_num_elements field is right next to
the _M_buckets field and quite possibly on the same cache line.
Is this patch OK to commit to mainline?
Ian
2009-03-27 Ian Lance Taylor <iant@google.com>
* include/backward/hashtable.h (clear): Return quickly if the
table is empty.
Index: include/backward/hashtable.h
===================================================================
--- include/backward/hashtable.h (revision 145132)
+++ include/backward/hashtable.h (working copy)
@@ -1076,6 +1076,9 @@
hashtable<_Val, _Key, _HF, _Ex, _Eq, _All>::
clear()
{
+ if (_M_num_elements == 0)
+ return;
+
for (size_type __i = 0; __i < _M_buckets.size(); ++__i)
{
_Node* __cur = _M_buckets[__i];