diff --git a/libcxx/include/__hash_table b/libcxx/include/__hash_table index aefa8e19c1864..c31e1205c8fec 100644 --- a/libcxx/include/__hash_table +++ b/libcxx/include/__hash_table @@ -1771,9 +1771,9 @@ template template typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator __hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) { - size_t __hash = hash_function()(__k); size_type __bc = bucket_count(); - if (__bc != 0) { + if (__bc != 0 && size() != 0) { + size_t __hash = hash_function()(__k); size_t __chash = std::__constrain_hash(__hash, __bc); __next_pointer __nd = __bucket_list_[__chash]; if (__nd != nullptr) { @@ -1792,9 +1792,9 @@ template template typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator __hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const { - size_t __hash = hash_function()(__k); size_type __bc = bucket_count(); - if (__bc != 0) { + if (__bc != 0 && size() != 0) { + size_t __hash = hash_function()(__k); size_t __chash = std::__constrain_hash(__hash, __bc); __next_pointer __nd = __bucket_list_[__chash]; if (__nd != nullptr) { diff --git a/libcxx/test/benchmarks/containers/associative/associative_container_benchmarks.h b/libcxx/test/benchmarks/containers/associative/associative_container_benchmarks.h index fb4455c4aa9da..7c17a5798aee3 100644 --- a/libcxx/test/benchmarks/containers/associative/associative_container_benchmarks.h +++ b/libcxx/test/benchmarks/containers/associative/associative_container_benchmarks.h @@ -57,7 +57,7 @@ void associative_container_benchmarks(std::string container) { auto get_key = [](Value const& v) { return adapt_operations::key_from_value(v); }; auto bench = [&](std::string operation, auto f) { - benchmark::RegisterBenchmark(container + "::" + operation, f)->Arg(32)->Arg(1024)->Arg(8192); + benchmark::RegisterBenchmark(container + "::" + operation, f)->Arg(0)->Arg(32)->Arg(1024)->Arg(8192); }; static constexpr bool is_multi_key_container = @@ -172,7 +172,7 @@ void associative_container_benchmarks(std::string container) { // Insertion ///////////////////////// bench("insert(value) (already present)", [=](auto& st) { - const std::size_t size = st.range(0); + const std::size_t size = st.range(0) ? st.range(0) : 1; std::vector in = make_value_types(generate_unique_keys(size)); Value to_insert = in[in.size() / 2]; // pick any existing value std::vector c(BatchSize, Container(in.begin(), in.end())); @@ -325,7 +325,7 @@ void associative_container_benchmarks(std::string container) { // Erasure ///////////////////////// bench("erase(key) (existent)", [=](auto& st) { - const std::size_t size = st.range(0); + const std::size_t size = st.range(0) ? st.range(0) : 1; // avoid empty container std::vector in = make_value_types(generate_unique_keys(size)); Value element = in[in.size() / 2]; // pick any element std::vector c(BatchSize, Container(in.begin(), in.end())); @@ -369,7 +369,7 @@ void associative_container_benchmarks(std::string container) { }); bench("erase(iterator)", [=](auto& st) { - const std::size_t size = st.range(0); + const std::size_t size = st.range(0) ? st.range(0) : 1; // avoid empty container std::vector in = make_value_types(generate_unique_keys(size)); Value element = in[in.size() / 2]; // pick any element @@ -448,7 +448,7 @@ void associative_container_benchmarks(std::string container) { Container c(in.begin(), in.end()); while (st.KeepRunningBatch(BatchSize)) { - for (std::size_t i = 0; i != BatchSize; ++i) { + for (std::size_t i = 0; i != keys.size(); ++i) { // possible empty keys when Arg(0) auto result = func(c, keys[i]); benchmark::DoNotOptimize(c); benchmark::DoNotOptimize(result); diff --git a/libcxx/test/benchmarks/containers/associative/unordered_set.bench.cpp b/libcxx/test/benchmarks/containers/associative/unordered_set.bench.cpp index 56420bdaadfbf..89443a597e85a 100644 --- a/libcxx/test/benchmarks/containers/associative/unordered_set.bench.cpp +++ b/libcxx/test/benchmarks/containers/associative/unordered_set.bench.cpp @@ -8,6 +8,7 @@ // UNSUPPORTED: c++03, c++11, c++14, c++17 +#include #include #include @@ -27,6 +28,7 @@ struct support::adapt_operations> { int main(int argc, char** argv) { support::associative_container_benchmarks>("std::unordered_set"); + support::associative_container_benchmarks>("std::unordered_set"); benchmark::Initialize(&argc, argv); benchmark::RunSpecifiedBenchmarks();