[libcxx-commits] [libcxx] [libcxx] Avoid hash key in __hash_table::find() if no buckets yet. (PR #126837)

via libcxx-commits libcxx-commits at lists.llvm.org
Thu Feb 13 00:49:05 PST 2025


xbcnn wrote:

I added a separate benchmark: `libcxx/test/benchmarks/containers/associative/hash_table_find.bench.cpp`, since I need find on empty unordered_set.

I pre-generate 32K random strings, and do find times vary from 1023~32768.
With the opt:
```
# | ------------------------------------------------------------------------------------------
# | Benchmark                                                Time             CPU   Iterations
# | ------------------------------------------------------------------------------------------
# | BM_UnorderedSet_Find_EmptySet/long_string/1024       12276 ns        12276 ns        56892
# | BM_UnorderedSet_Find_EmptySet/long_string/2048       24622 ns        24622 ns        28436
# | BM_UnorderedSet_Find_EmptySet/long_string/4096       48973 ns        48972 ns        14249
# | BM_UnorderedSet_Find_EmptySet/long_string/8192       98178 ns        98175 ns         7116
# | BM_UnorderedSet_Find_EmptySet/long_string/16384     195976 ns       195965 ns         3569
# | BM_UnorderedSet_Find_EmptySet/long_string/32768     391325 ns       391318 ns         1782
# | BM_UnorderedSet_Find/long_string/1024               190627 ns       190621 ns         3672
# | BM_UnorderedSet_Find/long_string/2048               379007 ns       379003 ns         1847
# | BM_UnorderedSet_Find/long_string/4096               755937 ns       755924 ns          926
# | BM_UnorderedSet_Find/long_string/8192              1507339 ns      1507314 ns          464
# | BM_UnorderedSet_Find/long_string/16384             3019197 ns      3019036 ns          231
# | BM_UnorderedSet_Find/long_string/32768             6041986 ns      6041574 ns          116
```

Without the opt:
```
# | ------------------------------------------------------------------------------------------
# | Benchmark                                                Time             CPU   Iterations
# | ------------------------------------------------------------------------------------------
# | BM_UnorderedSet_Find_EmptySet/long_string/1024      163052 ns       163048 ns         4292
# | BM_UnorderedSet_Find_EmptySet/long_string/2048      324969 ns       324960 ns         2151
# | BM_UnorderedSet_Find_EmptySet/long_string/4096      647079 ns       647064 ns         1081
# | BM_UnorderedSet_Find_EmptySet/long_string/8192     1291289 ns      1291263 ns          542
# | BM_UnorderedSet_Find_EmptySet/long_string/16384    2592642 ns      2592585 ns          270
# | BM_UnorderedSet_Find_EmptySet/long_string/32768    5181394 ns      5181302 ns          135
# | BM_UnorderedSet_Find/long_string/1024               187192 ns       187189 ns         3740
# | BM_UnorderedSet_Find/long_string/2048               371351 ns       371346 ns         1883
# | BM_UnorderedSet_Find/long_string/4096               740937 ns       740902 ns          945
# | BM_UnorderedSet_Find/long_string/8192              1479457 ns      1479432 ns          473
# | BM_UnorderedSet_Find/long_string/16384             2962292 ns      2962147 ns          236
# | BM_UnorderedSet_Find/long_string/32768             5923977 ns      5923911 ns          118
```

On empty set, it's about 10+ times gains since no hash key computed.
On non-empty set, they're very close.


https://github.com/llvm/llvm-project/pull/126837


More information about the libcxx-commits mailing list