summaryrefslogtreecommitdiff
path: root/lib/Support/StringMap.cpp
diff options
context:
space:
mode:
authorMehdi Amini <mehdi.amini@apple.com>2016-03-25 05:57:57 +0000
committerMehdi Amini <mehdi.amini@apple.com>2016-03-25 05:57:57 +0000
commitbf941ea066919de92b01666d95cc2f43b1a748a3 (patch)
tree48fa73b133901bd6261613d8cc00bcfa0910bff0 /lib/Support/StringMap.cpp
parent9ed5a8271ed1e6a3ef254875cb951386b12dd44e (diff)
Adjust initial size in StringMap constructor to guarantee no grow()
Summary: StringMap ctor accepts an initialize size, but expect it to be rounded to the next power of 2. The ctor can handle that directly instead of expecting clients to round it. Also, since the map will resize itself when 75% full, take this into account an initialize a larger initial size to avoid any growth. Reviewers: dblaikie Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D18344 From: Mehdi Amini <mehdi.amini@apple.com> git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@264385 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support/StringMap.cpp')
-rw-r--r--lib/Support/StringMap.cpp16
1 files changed, 15 insertions, 1 deletions
diff --git a/lib/Support/StringMap.cpp b/lib/Support/StringMap.cpp
index 7be946642d9..7da9ccbd40c 100644
--- a/lib/Support/StringMap.cpp
+++ b/lib/Support/StringMap.cpp
@@ -17,12 +17,26 @@
#include <cassert>
using namespace llvm;
+/// Returns the number of buckets to allocate to ensure that the DenseMap can
+/// accommodate \p NumEntries without need to grow().
+static unsigned getMinBucketToReserveForEntries(unsigned NumEntries) {
+ // Ensure that "NumEntries * 4 < NumBuckets * 3"
+ if (NumEntries == 0)
+ return 0;
+ // +1 is required because of the strict equality.
+ // For example if NumEntries is 48, we need to return 401.
+ return NextPowerOf2(NumEntries * 4 / 3 + 1);
+}
+
StringMapImpl::StringMapImpl(unsigned InitSize, unsigned itemSize) {
ItemSize = itemSize;
// If a size is specified, initialize the table with that many buckets.
if (InitSize) {
- init(InitSize);
+ // The table will grow when the number of entries reach 3/4 of the number of
+ // buckets. To guarantee that "InitSize" number of entries can be inserted
+ // in the table without growing, we allocate just what is needed here.
+ init(getMinBucketToReserveForEntries(InitSize));
return;
}