Bug 1477626 - Replace some bespoke code with a call to CeilingLog2(). r=Waldo draft
authorNicholas Nethercote <nnethercote@mozilla.com>
Thu, 26 Jul 2018 18:52:46 +1000
changeset 822905 dc004fa485c07dc63f93dd1a20ddecb85e43c213
parent 822377 5da2166fd301cdfe782fa8a778454def0dc03c17
child 822906 4c406be0116248ea23074dccf7bae85c3a4c82a6
push id117517
push usernnethercote@mozilla.com
push dateThu, 26 Jul 2018 10:24:07 +0000
reviewersWaldo
bugs1477626, 543034
milestone63.0a1
Bug 1477626 - Replace some bespoke code with a call to CeilingLog2(). r=Waldo After all, bug 543034 was fixed 9 years ago. MozReview-Commit-ID: HDPO3gGuQMx
js/public/HashTable.h
--- a/js/public/HashTable.h
+++ b/js/public/HashTable.h
@@ -6,16 +6,17 @@
 
 #ifndef js_HashTable_h
 #define js_HashTable_h
 
 #include "mozilla/Assertions.h"
 #include "mozilla/Attributes.h"
 #include "mozilla/Casting.h"
 #include "mozilla/HashFunctions.h"
+#include "mozilla/MathAlgorithms.h"
 #include "mozilla/MemoryChecking.h"
 #include "mozilla/MemoryReporting.h"
 #include "mozilla/Move.h"
 #include "mozilla/Opaque.h"
 #include "mozilla/PodOperations.h"
 #include "mozilla/ReentrancyGuard.h"
 #include "mozilla/TemplateLib.h"
 #include "mozilla/TypeTraits.h"
@@ -1248,18 +1249,17 @@ class HashTable : private AllocPolicy
     } stats;
 #   define METER(x) x
 #else
 #   define METER(x)
 #endif
 
     // The default initial capacity is 32 (enough to hold 16 elements), but it
     // can be as low as 4.
-    static const unsigned sMinCapacityLog2 = 2;
-    static const unsigned sMinCapacity  = 1 << sMinCapacityLog2;
+    static const unsigned sMinCapacity  = 4;
     static const unsigned sMaxInit      = JS_BIT(CAP_BITS - 1);
     static const unsigned sMaxCapacity  = JS_BIT(CAP_BITS);
     static const unsigned sHashBits     = mozilla::tl::BitSize<HashNumber>::value;
 
     // Hash-table alpha is conceptually a fraction, but to avoid floating-point
     // math we implement it as a ratio of integers.
     static const uint8_t sAlphaDenominator = 4;
     static const uint8_t sMinAlphaNumerator = 1; // min alpha: 1/4
@@ -1356,32 +1356,28 @@ class HashTable : private AllocPolicy
         // Compute the smallest capacity allowing |length| elements to be
         // inserted without rehashing: ceil(length / max-alpha).  (Ceiling
         // integral division: <http://stackoverflow.com/a/2745086>.)
         uint32_t newCapacity =
             (length * sAlphaDenominator + sMaxAlphaNumerator - 1) / sMaxAlphaNumerator;
         if (newCapacity < sMinCapacity)
             newCapacity = sMinCapacity;
 
-        // FIXME: use JS_CEILING_LOG2 when PGO stops crashing (bug 543034).
-        uint32_t roundUp = sMinCapacity, roundUpLog2 = sMinCapacityLog2;
-        while (roundUp < newCapacity) {
-            roundUp <<= 1;
-            ++roundUpLog2;
-        }
+        // Round up capacity to next power-of-two.
+        uint32_t log2 = mozilla::CeilingLog2(newCapacity);
+        newCapacity = 1u << log2;
 
-        newCapacity = roundUp;
         MOZ_ASSERT(newCapacity >= length);
         MOZ_ASSERT(newCapacity <= sMaxCapacity);
 
         table = createTable(*this, newCapacity);
         if (!table)
             return false;
 
-        setTableSizeLog2(roundUpLog2);
+        setTableSizeLog2(log2);
         METER(memset(&stats, 0, sizeof(stats)));
         return true;
     }
 
     bool initialized() const
     {
         return !!table;
     }