PATCH RFA: Make hashtable::clear return quickly if table is empty

Ian Lance Taylor iant@google.com
Sat Mar 28 05:38:00 GMT 2009


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.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: foo.patch
Type: text/x-patch
Size: 443 bytes
Desc: Make clear return quickly is table is empty
URL: <http://gcc.gnu.org/pipermail/libstdc++/attachments/20090328/8865a6e2/attachment.bin>


More information about the Libstdc++ mailing list