From 1aeeff1b650f0173e9e48f54a130dbd0ec56a770 Mon Sep 17 00:00:00 2001 From: greg Date: Thu, 25 Jul 2019 20:43:58 -0400 Subject: [PATCH] reserve and capacity not matching #18 reserve(), especially for parallel_*_hash_*, was reserving excessive sizes. Now the size is closer to the one requested, even though the "power-of-two" capacity requirement still creates a capacity() greater than what is asked in reserve(). --- parallel_hashmap/phmap.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/parallel_hashmap/phmap.h b/parallel_hashmap/phmap.h index eac882f..8c844ef 100644 --- a/parallel_hashmap/phmap.h +++ b/parallel_hashmap/phmap.h @@ -1541,7 +1541,7 @@ public: } // bitor is a faster way of doing `max` here. We will round up to the next // power-of-2-minus-1, so bitor is good enough. - auto m = NormalizeCapacity(n | GrowthToLowerboundCapacity(size())); + auto m = NormalizeCapacity(std::max(n, size())); // n == 0 unconditionally rehashes as per the standard. if (n == 0 || m > capacity_) { resize(m); @@ -3008,7 +3008,12 @@ public: } } - void reserve(size_t n) { rehash(GrowthToLowerboundCapacity(n)); } + void reserve(size_t n) + { + size_t target = GrowthToLowerboundCapacity(n); + size_t normalized = 16 * NormalizeCapacity(n / num_tables); + rehash(normalized > target ? normalized : target); + } // Extension API: support for heterogeneous keys. //