This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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];

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]