[PATCH] D18154: DenseMap: make .resize() do the intuitive thing

escha via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 14 11:42:17 PDT 2016


escha created this revision.
escha added reviewers: bogner, resistor.
escha added a subscriber: llvm-commits.
escha set the repository for this revision to rL LLVM.

In some places, like InstCombine, we resize a DenseMap to fit the elements we intend to put in it, then insert those elements (to avoid continual reallocations as it grows). But .resize(foo) doesn't actually do what people think; it resizes to foo buckets (which is really an implementation detail the user of DenseMap probably shouldn't care about), not the space required to fit foo elements. DenseMap grows if 3/4 of its buckets are full, so this actually causes one forced reallocation every time instead of avoiding a reallocation.

This patch makes .resize(foo) do the intuitive thing: it grows to the size necessary to fit foo elements without new allocations.

Repository:
  rL LLVM

http://reviews.llvm.org/D18154

Files:
  include/llvm/ADT/DenseMap.h

Index: include/llvm/ADT/DenseMap.h
===================================================================
--- include/llvm/ADT/DenseMap.h
+++ include/llvm/ADT/DenseMap.h
@@ -81,8 +81,11 @@
   }
   unsigned size() const { return getNumEntries(); }
 
-  /// Grow the densemap so that it has at least Size buckets. Does not shrink
+  /// Grow the densemap so that it can contain at least Size items before
+  /// resizing again. This means somewhat more than Size buckets because
+  /// densemap resizes upon reaching 3/4 full.
   void resize(size_type Size) {
+    Size = (Size * 4 + 2) / 3;
     incrementEpoch();
     if (Size > getNumBuckets())
       grow(Size);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D18154.50619.patch
Type: text/x-patch
Size: 662 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160314/a4f258ee/attachment.bin>


More information about the llvm-commits mailing list