[PATCH] libstdc++: fix hash container clear() complexity (DR2550) [PR67922]

Nathan Myers ncm@cantrip.org
Mon Oct 26 09:21:27 GMT 2026


[
This is benchmark output comparing two methods of clearing a 
hash table, as described in the README below. In the first line,
we see it is 2.73 times faster to re-hash and clear just the
10000 elements and their buckets than to memset all 1M buckets
and then walk the elements without re-hashing, but as the table
loading increases, the relative benefit declines. With a 
50k-bucket table, four stanzas down, with more than 3125
elements for a load factor of 1/16, it is faster to memset all
the buckets.

int 1000k 100  10000   3704  1354 273%  <===
int 1000k 50  20000   1855  858 216% 
int 1000k 35  28571   1139  538 211% 
int 1000k 20  50000   283  261 108% 
int 1000k 16  62500   180  211 85% *
int 1000k 12  83333   119  73 163% 
int 1000k 10  100000   344  261 131% 
int 1000k 7  142857   260  223 116% 
int 1000k 5  200000   192  165 116% 

int 500k 100  5000   8354  4338 192% 
int 500k 50  10000   3317  2557 129% 
int 500k 35  14285   2347  1929 121% 
int 500k 20  25000   1148  1036 110% 
int 500k 16  31250   665  810 82% *
int 500k 12  41666   246  242 101% 
int 500k 10  50000   773  722 107% 
int 500k 7  71428   569  507 112% 
int 500k 5  100000   376  319 117% 

int 100k 100  1000   43798  32076 136% 
int 100k 50  2000   21871  18017 121% 
int 100k 35  2857   15186  12794 118% 
int 100k 20  5000   8461  7857 107% 
int 100k 16  6250   6358  6222 102% 
int 100k 12  8333   4436  4390 101% 
int 100k 10  10000   3956  3952 100% *
int 100k 7  14285   2861  2867 99% *
int 100k 5  20000   2068  2055 100% *

int 50k 100  500   89691  69117 129% 
int 50k 50  1000   43761  38813 112% 
int 50k 35  1428   31061  28513 108% 
int 50k 20  2500   17403  16734 103% 
int 50k 16  3125   13681  13571 100% * <===
int 50k 12  4166   9649  10117 95% *
int 50k 10  5000   8534  8517 100% *
int 50k 7  7142   5935  5863 101% 
int 50k 5  10000   4199  4205 99% *

int 30k 100  300   148349  118288 125% 
int 30k 50  600   74224  65732 112% 
int 30k 35  857   51832  48530 106% 
int 30k 20  1500   29707  28024 106% 
int 30k 16  1875   23387  22605 103% 
int 30k 12  2500   17096  17526 97% *
int 30k 10  3000   14002  14645 95% *
int 30k 7  4285   10172  10112 100% *
int 30k 5  6000   7187  7363 97% *

int 10k 100  100   448669  368733 121% 
int 10k 50  200   223602  200766 111% 
int 10k 35  285   138224  148280 93% *
int 10k 20  500   86811  84937 102% 
int 10k 16  625   70022  70648 99% *
int 10k 12  833   52805  51891 101% 
int 10k 10  1000   44157  44818 98% *
int 10k 7  1428   30999  31416 98% *
int 10k 5  2000   21699  22680 95% *

int 5k 100  50   946249  777544 121% 
int 5k 50  100   461186  415291 111% 
int 5k 35  142   312477  299016 104% 
int 5k 20  250   174259  176539 98% *
int 5k 16  312   139497  141465 98% *
int 5k 12  416   107936  106617 101% 
int 5k 10  500   87448  89284 97% *
int 5k 7  714   61012  63015 96% *
int 5k 5  1000   44795  45289 98% *

str 1000k 100  10000   568  446 127% 
str 1000k 50  20000   86  114 75% *
str 1000k 35  28571   61  88 69% *
str 1000k 20  50000   34  49 69% *
str 1000k 16  62500   26  38 68% *
str 1000k 12  83333   19  27 70% *
str 1000k 10  100000   15  25 60% *
str 1000k 7  142857   11  14 78% *
str 1000k 5  200000   7  9 77% *

str 500k 100  5000   2433  1990 122% 
str 500k 50  10000   859  862 99% *
str 500k 35  14285   163  443 36% *
str 500k 20  25000   90  127 70% *
str 500k 16  31250   65  86 75% *
str 500k 12  41666   49  61 80% *
str 500k 10  50000   38  50 76% *
str 500k 7  71428   25  32 78% *
str 500k 5  100000   16  23 69% *

str 100k 100  1000   15812  13453 117% 
str 100k 50  2000   7762  6747 115% 
str 100k 35  2857   5323  4598 115% 
str 100k 20  5000   2773  2721 101% 
str 100k 16  6250   2163  2135 101% 
str 100k 12  8333   1467  1546 94% *
str 100k 10  10000   1107  1215 91% *
str 100k 7  14285   534  799 66% *
str 100k 5  20000   152  330 46% *

str 50k 100  500   33258  28659 116% 
str 50k 50  1000   16024  15134 105% 
str 50k 35  1428   11270  10506 107% 
str 50k 20  2500   6168  5923 104% 
str 50k 16  3125   4875  4730 103% 
str 50k 12  4166   3535  3316 106% 
str 50k 10  5000   2767  2850 97% *
str 50k 7  7142   1869  1906 98% *
str 50k 5  10000   1132  1238 91% *

str 30k 100  300   58315  50595 115% 
str 30k 50  600   26962  26490 101% 
str 30k 35  857   19510  18690 104% 
str 30k 20  1500   10717  10418 102% 
str 30k 16  1875   8387  8244 101% 
str 30k 12  2500   6175  6128 100% *
str 30k 10  3000   5042  5052 99% *
str 30k 7  4285   3319  3377 98% *
str 30k 5  6000   2304  2316 99% *

str 10k 100  100   187339  152781 122% 
str 10k 50  200   81287  82075 99% *
str 10k 35  285   58663  57070 102% 
str 10k 20  500   32950  32214 102% 
str 10k 16  625   25829  25431 101% 
str 10k 12  833   19234  18815 102% 
str 10k 10  1000   15746  15438 101% 
str 10k 7  1428   10618  10933 97% *
str 10k 5  2000   7653  7698 99% *

str 5k 100  50   396017  351776 112% 
str 5k 50  100   188726  173441 108% 
str 5k 35  142   126609  121234 104% 
str 5k 20  250   69695  66496 104% 
str 5k 16  312   54886  53491 102% 
str 5k 12  416   40146  39912 100% *
str 5k 10  500   33333  32880 101% 
str 5k 7  714   22568  22758 99% *
str 5k 5  1000   15966  15940 100% *

int_nc 1000k 100  10000   4212  3007 140% 
int_nc 1000k 50  20000   2076  1727 120% 
int_nc 1000k 35  28571   1421  1236 114% 
int_nc 1000k 20  50000   802  740 108% 
int_nc 1000k 16  62500   618  572 108% 
int_nc 1000k 12  83333   494  464 106% 
int_nc 1000k 10  100000   415  393 105% 
int_nc 1000k 7  142857   296  283 104% 
int_nc 1000k 5  200000   209  204 102% 

int_nc 500k 100  5000   8829  6686 132% 
int_nc 500k 50  10000   4139  3786 109% 
int_nc 500k 35  14285   2989  2748 108% 
int_nc 500k 20  25000   1689  1649 102% 
int_nc 500k 16  31250   1327  1320 100% *
int_nc 500k 12  41666   1008  1013 99% *
int_nc 500k 10  50000   852  855 99% *
int_nc 500k 7  71428   599  601 99% *
int_nc 500k 5  100000   424  435 97% *

int_nc 100k 100  1000   45680  34499 132% 
int_nc 100k 50  2000   22966  19967 115% 
int_nc 100k 35  2857   16008  14691 108% 
int_nc 100k 20  5000   8948  8647 103% 
int_nc 100k 16  6250   7011  6719 104% 
int_nc 100k 12  8333   5156  5265 97% *
int_nc 100k 10  10000   4331  4275 101% 
int_nc 100k 7  14285   3011  3042 98% *
int_nc 100k 5  20000   2205  2230 98% *

int_nc 50k 100  500   92637  71260 129% 
int_nc 50k 50  1000   46484  40848 113% 
int_nc 50k 35  1428   31596  30142 104% 
int_nc 50k 20  2500   18173  17907 101% 
int_nc 50k 16  3125   14685  14394 102% 
int_nc 50k 12  4166   10924  10909 100% *
int_nc 50k 10  5000   9206  9145 100% *
int_nc 50k 7  7142   6310  6459 97% *
int_nc 50k 5  10000   4490  4468 100% *

int_nc 30k 100  300   154685  124302 124% 
int_nc 30k 50  600   76378  70257 108% 
int_nc 30k 35  857   54362  49678 109% 
int_nc 30k 20  1500   31031  29896 103% 
int_nc 30k 16  1875   24332  24233 100% *
int_nc 30k 12  2500   18525  18481 100% *
int_nc 30k 10  3000   15355  15581 98% *
int_nc 30k 7  4285   10503  10518 99% *
int_nc 30k 5  6000   7508  7680 97% *

int_nc 10k 100  100   473638  387375 122% 
int_nc 10k 50  200   232898  208954 111% 
int_nc 10k 35  285   164722  153014 107% 
int_nc 10k 20  500   93451  90279 103% 
int_nc 10k 16  625   74621  73021 102% 
int_nc 10k 12  833   55522  54547 101% 
int_nc 10k 10  1000   46860  46336 101% 
int_nc 10k 7  1428   32779  32881 99% *
int_nc 10k 5  2000   22738  23889 95% *

int_nc 5k 100  50   1033395  810543 127% 
int_nc 5k 50  100   474094  425004 111% 
int_nc 5k 35  142   327841  308429 106% 
int_nc 5k 20  250   185548  181538 102% 
int_nc 5k 16  312   147695  144079 102% 
int_nc 5k 12  416   111920  113043 99% *
int_nc 5k 10  500   92847  92707 100% *
int_nc 5k 7  714   65566  65548 100% *
int_nc 5k 5  1000   46328  47255 98% *

str_nc 1000k 100  10000   1397  1182 118% 
str_nc 1000k 50  20000   647  594 108% 
str_nc 1000k 35  28571   403  393 102% 
str_nc 1000k 20  50000   162  178 91% *
str_nc 1000k 16  62500   106  139 76% *
str_nc 1000k 12  83333   65  88 73% *
str_nc 1000k 10  100000   51  68 75% *
str_nc 1000k 7  142857   32  42 76% *
str_nc 1000k 5  200000   20  27 74% *

str_nc 500k 100  5000   3118  2696 115% 
str_nc 500k 50  10000   1460  1391 104% 
str_nc 500k 35  14285   925  939 98% *
str_nc 500k 20  25000   522  533 97% *
str_nc 500k 16  31250   395  409 96% *
str_nc 500k 12  41666   261  302 86% *
str_nc 500k 10  50000   187  238 78% *
str_nc 500k 7  71428   114  146 78% *
str_nc 500k 5  100000   58  77 75% *

str_nc 100k 100  1000   17280  14731 117% 
str_nc 100k 50  2000   8305  7852 105% 
str_nc 100k 35  2857   5803  5368 108% 
str_nc 100k 20  5000   3079  3090 99% *
str_nc 100k 16  6250   2428  2427 100% *
str_nc 100k 12  8333   1816  1817 99% *
str_nc 100k 10  10000   1487  1502 99% *
str_nc 100k 7  14285   1003  1017 98% *
str_nc 100k 5  20000   681  711 95% *

str_nc 50k 100  500   35898  30968 115% 
str_nc 50k 50  1000   17813  16043 111% 
str_nc 50k 35  1428   12308  11858 103% 
str_nc 50k 20  2500   6746  6685 100% *
str_nc 50k 16  3125   5192  5161 100% *
str_nc 50k 12  4166   3831  3829 100% *
str_nc 50k 10  5000   3156  3102 101% 
str_nc 50k 7  7142   2105  2135 98% *
str_nc 50k 5  10000   1478  1497 98% *

str_nc 30k 100  300   61627  53474 115% 
str_nc 30k 50  600   29791  28267 105% 
str_nc 30k 35  857   20324  19709 103% 
str_nc 30k 20  1500   11591  11169 103% 
str_nc 30k 16  1875   9058  8616 105% 
str_nc 30k 12  2500   6643  6648 99% *
str_nc 30k 10  3000   5642  5483 102% 
str_nc 30k 7  4285   3820  3660 104% 
str_nc 30k 5  6000   2625  2672 98% *

str_nc 10k 100  100   196989  174086 113% 
str_nc 10k 50  200   94774  90450 104% 
str_nc 10k 35  285   65087  61659 105% 
str_nc 10k 20  500   35869  34111 105% 
str_nc 10k 16  625   28658  27981 102% 
str_nc 10k 12  833   21104  20615 102% 
str_nc 10k 10  1000   17675  17432 101% 
str_nc 10k 7  1428   12288  11958 102% 
str_nc 10k 5  2000   8527  8537 99% *

str_nc 5k 100  50   442427  376096 117% 
str_nc 5k 50  100   203775  183180 111% 
str_nc 5k 35  142   135235  130047 103% 
str_nc 5k 20  250   75492  71858 105% 
str_nc 5k 16  312   58461  51302 113% 
str_nc 5k 12  416   42973  42560 100% *
str_nc 5k 10  500   35906  35530 101% 
str_nc 5k 7  714   24542  24473 100% *
str_nc 5k 5  1000   17318  17554 98% *

These were run on an AMD Ryzen 9 7840U.
]

The hash tables std::unordered_set, _map, _multiset, _multimap as
implemented take time linear in the size of the bucket array (via
std::fill), rather than in the number of elements stored, in
violation of Standard requirements. With this patch, it performs
work only for the elements present, but only for a sparsely-
populated table. With a bucket loading above a fixed threshold,
clearing the whole table is assumed to be faster, so the new
method is used only when loading is less.

The optimal definition for "sparsely-populated" depends on
information rarely available, so we choose a default threshold,
expressed as a fraction of the number of buckets, 16 indicating
that if size() is less than 1/16th bucket_count(), the table is
considered sparsely-populated. Users may set a preprocessor
symbol _GLIBCXX_HASH_CLEAR_THRESHOLD when building with hashed
containers to choose a different value.

This also adds a directory under testsuite/performance with
code to help identify, for a given target, a good choice of
threshold.

libstdc++-v3/Changelog:
	PR libstdc++/67922
	* include/bits/hashtable.h (clear): Special-case sparse population.
	* testsuite/performance/23_containers/hash_clear/README: Add.
	* testsuite/performance/23_containers/hash_clear/Makefile: Add.
	* testsuite/performance/23_containers/hash_clear/hashbench.cc: Add.
	* testsuite/performance/23_containers/hash_clear/bench_int.cc: Add.
	* testsuite/performance/23_containers/hash_clear/bench_str.cc: Add.
	* testsuite/performance/23_containers/hash_clear/run: Add.
---
 libstdc++-v3/include/bits/hashtable.h         | 18 +++-
 .../23_containers/hash_clear/Makefile         | 56 +++++++++++++
 .../23_containers/hash_clear/README           | 82 +++++++++++++++++++
 .../23_containers/hash_clear/bench_int.cc     | 22 +++++
 .../23_containers/hash_clear/bench_str.cc     | 38 +++++++++
 .../23_containers/hash_clear/hashbench.cc     | 77 +++++++++++++++++
 .../performance/23_containers/hash_clear/run  | 14 ++++
 7 files changed, 305 insertions(+), 2 deletions(-)
 create mode 100644 libstdc++-v3/testsuite/performance/23_containers/hash_clear/Makefile
 create mode 100644 libstdc++-v3/testsuite/performance/23_containers/hash_clear/README
 create mode 100644 libstdc++-v3/testsuite/performance/23_containers/hash_clear/bench_int.cc
 create mode 100644 libstdc++-v3/testsuite/performance/23_containers/hash_clear/bench_str.cc
 create mode 100644 libstdc++-v3/testsuite/performance/23_containers/hash_clear/hashbench.cc
 create mode 100755 libstdc++-v3/testsuite/performance/23_containers/hash_clear/run

diff --git a/libstdc++-v3/include/bits/hashtable.h b/libstdc++-v3/include/bits/hashtable.h
index 5b98b1193ed..f04d897258d 100644
--- a/libstdc++-v3/include/bits/hashtable.h
+++ b/libstdc++-v3/include/bits/hashtable.h
@@ -2778,8 +2778,22 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	       _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
     clear() noexcept
     {
-      this->_M_deallocate_nodes(_M_begin());
-      std::fill_n(_M_buckets, _M_bucket_count, nullptr);
+#     ifndef  _GLIBCXX_HASH_CLEAR_THRESHOLD
+#       define _GLIBCXX_HASH_CLEAR_THRESHOLD 16  // [0 .. ~size_t()]
+#     endif
+      const size_t __threshold = _GLIBCXX_HASH_CLEAR_THRESHOLD;
+      if (__threshold != 0 && _M_bucket_count / __threshold < _M_element_count)
+	{ // Avoid computing hashes.
+	  this->_M_deallocate_nodes(_M_begin());
+	  std::fill_n(_M_buckets, _M_bucket_count, nullptr);
+	}
+      else for (auto __n = this->_M_begin(); __n != nullptr;)
+	{
+	  auto __tmp = __n;
+	  __n = __n->_M_next();
+	  _M_buckets[_M_bucket_index(*__tmp)] = nullptr;
+	  this->_M_deallocate_node(__tmp);
+	}
       _M_element_count = 0;
       _M_before_begin._M_nxt = nullptr;
     }
diff --git a/libstdc++-v3/testsuite/performance/23_containers/hash_clear/Makefile b/libstdc++-v3/testsuite/performance/23_containers/hash_clear/Makefile
new file mode 100644
index 00000000000..2701108edfe
--- /dev/null
+++ b/libstdc++-v3/testsuite/performance/23_containers/hash_clear/Makefile
@@ -0,0 +1,56 @@
+CACHE_SIZE=16 # Megabytes of L3 cache.
+THREADS=4     # Assume a 4-way cache
+FIRST_CORE=0  # Run on cores [0..THREADS).
+CACHELINE=64  # Size of cache line, 128 on POWER.
+
+THR_N=-DTHREADS=$(THREADS)
+THR_1=-DTHREADS=1
+# CXX=
+CXXFLAGS=-W -Wall -O2 -g -std=c++26 \
+  -DCACHELINE=$(CACHELINE) \
+  -DCACHE_SIZE=$(CACHE_SIZE) \
+  -DFIRST_CORE=$(FIRST_CORE)
+
+run: all; bash run
+
+ALL =	bench_int_walk \
+	bench_int_fill \
+	bench_str_walk \
+	bench_str_fill \
+	bench_int_walk_nc \
+	bench_int_fill_nc \
+	bench_str_walk_nc \
+	bench_str_fill_nc
+
+all: $(ALL)
+clean:; rm -f $(ALL)
+
+# These run with one or more threads clobbering cache
+
+bench_int.cc bench_str.cc: hashtable.h
+
+bench_int_walk: bench_int.cc hashbench.cc
+	$(CXX) $(CXXFLAGS) $(THR_N) -o $@ $^
+
+bench_int_fill: bench_int.cc hashbench.cc
+	$(CXX) $(CXXFLAGS) $(THR_N) -DFILL -o $@ $^
+
+bench_str_walk: bench_str.cc hashbench.cc
+	$(CXX) $(CXXFLAGS) $(THR_N) -o $@ $^
+
+bench_str_fill: bench_str.cc hashbench.cc
+	$(CXX) $(CXXFLAGS) $(THR_N) -DFILL -o $@ $^
+
+# These run without cache interference.
+
+bench_int_walk_nc: bench_int.cc hashbench.cc
+	$(CXX) $(CXXFLAGS) $(THR_1) -o $@ $^
+
+bench_int_fill_nc: bench_int.cc hashbench.cc
+	$(CXX) $(CXXFLAGS) $(THR_1) -DFILL -o $@ $^
+
+bench_str_walk_nc: bench_str.cc hashbench.cc
+	$(CXX) $(CXXFLAGS) $(THR_1) -o $@ $^
+
+bench_str_fill_nc: bench_str.cc hashbench.cc
+	$(CXX) $(CXXFLAGS) $(THR_1) -DFILL -o $@ $^
diff --git a/libstdc++-v3/testsuite/performance/23_containers/hash_clear/README b/libstdc++-v3/testsuite/performance/23_containers/hash_clear/README
new file mode 100644
index 00000000000..1cff6b6d447
--- /dev/null
+++ b/libstdc++-v3/testsuite/performance/23_containers/hash_clear/README
@@ -0,0 +1,82 @@
+This benchmark compares performance of two strategies for
+clearing a hashing container. When the container has few enough
+elements, it is faster to rehash each element and zero only
+those elements' buckets; but with more elements, it is faster
+to zero all the buckets, and avoid re-hashing.
+
+See <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67922> and WG21
+LEWG DR 2550. The Standard requires that the `clear` member's
+performance be linear in the number of elements being cleared,
+not the number of buckets, but we don't want clearing a loaded
+table to take longer than necessary just to conform.
+
+We want a rule for when a container has enough elements that it
+is not worth rehashing all the elements just to avoid zeroing
+empty buckets. The correct number for a given use case depends
+on details of the target architecture, on how expensive it is
+to hash the elements, and on what is cached, details not
+generally available, so we must approximate it with a guess.
+
+Zeroing an individual bucket not in cache may require reading
+(usually) 8 adjacent buckets from backing RAM, poking out the
+one bucket, and writing them all back, where zeroing the whole
+table mostly does not require loading current values. Walking
+the chain of nodes when they are not in cache is commonly much
+slower than actually hashing its elements, but there is no
+avoiding this walk because the nodes must be destroyed and freed.
+
+Much of the cost of zeroing already-empty buckets is borne by
+other code whose cache entries are displaced by zeroed bucket
+contents. It is in general hard to measure that cost, which
+anyway does not figure in the formal complexity guarantee.
+
+This test uses a program that starts two or more threads, one
+that loads up and clears hash tables with varying bucket counts
+and number of elements relative to that count, and others that
+spin, clobbering cache entries. Just two threads may suffice to
+differentiate cache-hot from cache-cold tests, but CPUs with
+many-way caches might need more. The number is chosen in the
+Makefile.
+
+The Makefile also hard-codes the size of the L3 cache, which
+should be adjusted to the host running it, and the ids (0..n)
+of CPU cores that share an L3 cache, likewise. It #defines a
+symbol `FILL` that forces `clear` to use one strategy or the
+other. The Makefile builds eight versions of the program, for
+the two strategies ("walk" and "fill"), for fast and slow (`int`
+and `str`) hash functions, and for running, or not (`_nc`), the
+threads to clobber the cache. `make all` builds and runs tests.
+
+The script `run` executes these programs for a chosen set of
+bucket table sizes and number of elements, reporting the
+relative performance (runs per second; larger numbers indicate
+better performance) of walking the element list rehashing and
+clearing buckets, vs. filling the bucket table. Runs that seem
+to favor zeroing buckets by `fill` are marked with a "*".
+ Running it, we are interested in the transition from more
+than 100% to that or less.
+
+Tests are run without and with cache interference ("" vs. "_nc"),
+for tables with fast and slow hashes ("int" vs.  "str"), and for
+each the set of table sizes and table loading.  Raw numbers
+reported are the actual number of elements inserted each cycle,
+and how many cycles of table load-and-clear were run in one
+second using the "walk" and "fill" strategies.
+
+A sample output stanza with fast hash, and cache interference:
+
+int 100k 100  1000   51627  30467 169% 
+int 100k 50  2000   23923  17621 135% 
+int 100k 35  2857   17774  14033 126% 
+int 100k 20  5000   9382  7817 120% 
+int 100K 16  6250   6678  6015 111% 
+int 100k 12  8333   4260  4766 89% *
+int 100k 10  10000   4183  4079 102% 
+int 100k 7  14285   3095  2909 106% 
+int 100k 5  20000   2108  2113 99% *
+
+Here we see the transition from 111% to 89%, indicating that with
+100k buckets, a fast hash function, and cold cache, in the first
+line there is a definite advantage to rehashing and erasing just
+1000 elements directly, but with 8333 elements, clearing the
+whole table without rehashing is better.
diff --git a/libstdc++-v3/testsuite/performance/23_containers/hash_clear/bench_int.cc b/libstdc++-v3/testsuite/performance/23_containers/hash_clear/bench_int.cc
new file mode 100644
index 00000000000..24ce6ecffee
--- /dev/null
+++ b/libstdc++-v3/testsuite/performance/23_containers/hash_clear/bench_int.cc
@@ -0,0 +1,22 @@
+#ifdef FILL
+# define _GLIBCXX_HASH_CLEAR_THRESHOLD ~0ULL
+#else
+# define _GLIBCXX_HASH_CLEAR_THRESHOLD 0
+#endif
+// #include "../../../../include/bits/hashtable.h"
+#include <unordered_set>
+#include <atomic>
+
+void bench(std::size_t size, unsigned element_count,
+    std::atomic<int>& flag, unsigned& count)
+{
+  std::unordered_set<std::size_t> set; set.rehash(size);
+  std::size_t step = size / element_count;
+  while (!flag.load(std::memory_order::acquire))
+    {
+      for (std::size_t i = 0; i != element_count; ++i)
+	set.insert(i * step);
+      set.clear();
+      ++count;
+    }
+}
diff --git a/libstdc++-v3/testsuite/performance/23_containers/hash_clear/bench_str.cc b/libstdc++-v3/testsuite/performance/23_containers/hash_clear/bench_str.cc
new file mode 100644
index 00000000000..ed5577cf8f7
--- /dev/null
+++ b/libstdc++-v3/testsuite/performance/23_containers/hash_clear/bench_str.cc
@@ -0,0 +1,38 @@
+#ifdef FILL
+# define _GLIBCXX_HASH_CLEAR_THRESHOLD ~0ULL
+#else
+# define _GLIBCXX_HASH_CLEAR_THRESHOLD 0
+#endif
+// #include "../../../../include/bits/hashtable.h"
+#include <string>
+#include <atomic>
+#include <unordered_set>
+#include <functional>
+#include <charconv>
+
+struct S
+{
+  std::string s{"abcdefghijklmnop"};
+  S(unsigned n) { std::to_chars(&s[0], &s[s.size()], n); }
+  bool operator==(S const&) const = default;
+};
+namespace std
+{
+  template <> struct hash<S>
+  {
+    static std::size_t operator()(S const& s)
+      { return std::hash<std::string>{}(s.s); }
+  };
+}
+void bench(std::size_t size, unsigned element_count,
+    std::atomic<int>& flag, unsigned& count)
+{
+  std::unordered_set<S> set; set.rehash(size);
+  while (!flag.load(std::memory_order::acquire))
+    {
+      for (std::size_t i = 0; i != element_count; ++i)
+	set.emplace(i);
+      set.clear();
+      ++count;
+    }
+}
diff --git a/libstdc++-v3/testsuite/performance/23_containers/hash_clear/hashbench.cc b/libstdc++-v3/testsuite/performance/23_containers/hash_clear/hashbench.cc
new file mode 100644
index 00000000000..4ba4571118a
--- /dev/null
+++ b/libstdc++-v3/testsuite/performance/23_containers/hash_clear/hashbench.cc
@@ -0,0 +1,77 @@
+#include <iostream>
+#include <atomic>
+#include <cstdint>
+#include <pthread.h>
+#include <sched.h>
+#include <time.h>
+
+const std::size_t l3_cache_size = CACHE_SIZE * 1024 * 1024;
+const unsigned cacheline_size = CACHELINE;
+const unsigned first_core = FIRST_CORE;
+const unsigned threads = THREADS;
+
+unsigned buckets = l3_cache_size  / sizeof(int*);
+unsigned ratio = 16;  // buckets per element
+alignas(cacheline_size) std::atomic<int> stop_flag{0};
+
+struct alignas(cacheline_size) Ctx
+{
+  unsigned id;
+  unsigned count = 0;
+  ::pthread_t thr{};
+};
+
+struct alignas(cacheline_size) Cacheline
+{
+  std::atomic<unsigned> words[cacheline_size / sizeof(unsigned)];
+};
+
+void* run(void* in)
+{
+  auto& ctx = *(Ctx*)in;
+  if (ctx.id >= ::threads) exit(1);
+  ::cpu_set_t cpu_set; CPU_ZERO(&cpu_set);
+  CPU_SET(first_core + ctx.id, &cpu_set);
+  if (0 != ::pthread_setaffinity_np(ctx.thr, sizeof(cpu_set), &cpu_set))
+    ::exit(1);
+  const std::size_t size = l3_cache_size / cacheline_size;
+  if (ctx.id != 0)
+    {
+      // Clobber L3 cache continually.
+      Cacheline* a = new Cacheline[size];
+      while (!stop_flag.load(std::memory_order::acquire)) {
+	// walk through memory clobbering each cache line.
+	for (std::size_t i = 0; i != size; ++i) {
+	  a[i].words[0].store(i+ctx.count, std::memory_order::relaxed);
+	}
+	++ctx.count;
+      }
+    }
+  else
+    {
+      void bench(std::size_t, unsigned, std::atomic<int>&, unsigned&);
+      bench(buckets, buckets/ratio, stop_flag, ctx.count);
+    }
+  ::pthread_exit(nullptr);
+}
+
+int main(int ac, char** av)
+{
+  if (ac > 1) buckets = ::atoi(av[1]);
+  if (buckets < 1000) exit(1);
+  if (ac > 2) ratio = ::atoi(av[2]);
+  if (ratio < 1) exit(1);
+  Ctx ctxs[::threads];
+  for (unsigned id = ::threads; id-- != 0;)
+    {
+      ctxs[id].id = id;
+      if (0 != ::pthread_create(&ctxs[id].thr, nullptr, ::run, ctxs + id))
+	exit(1);
+    }
+  ::timespec time{::time_t(1),{}};
+  ::nanosleep(&time, nullptr);
+  stop_flag.store(1, std::memory_order::release);
+  for (auto& ctx : ctxs)
+    ::pthread_join(ctx.thr, nullptr);
+  std::cout << ctxs[0].count << " \n";
+}
diff --git a/libstdc++-v3/testsuite/performance/23_containers/hash_clear/run b/libstdc++-v3/testsuite/performance/23_containers/hash_clear/run
new file mode 100755
index 00000000000..06a15e57644
--- /dev/null
+++ b/libstdc++-v3/testsuite/performance/23_containers/hash_clear/run
@@ -0,0 +1,14 @@
+#/bin/bash
+for cache in "" "_nc"; do
+  for ty in int str; do 
+    for buckets in 1000 500 100 50 30 10 5; do # thousands
+      for ratio in 100 50 35 20 16 12 10 7 5; do  # buckets / element
+	bux=$((buckets*1000))
+	echo -n ${ty}${cache} ${buckets}k $ratio " "$((bux/ratio)) "  " 
+	a=$(./bench_${ty}_walk${cache} $bux $ratio)
+	echo -n $a " "
+	b=$(./bench_${ty}_fill${cache} $bux $ratio)
+	r=$((100 * a / b))
+	s= ; if ((r < 101)); then s='*'; fi
+	echo $b $r% "$s"
+done; echo; done; done; done
-- 
2.56.0



More information about the Libstdc++ mailing list