Bug 685783 - Take advantage of slop bytes in js::Vector. r=luke.
☠☠ backed out by 6f0c66b2ce76 ☠ ☠
authorNicholas Nethercote <nnethercote@mozilla.com>
Wed, 19 Oct 2011 16:19:06 -0700
changeset 79638 56ec5e954858b976485d9f4de4a070708470c364
parent 79637 477f20a61cbaab6d9070a19fc1a7857c2e5b2f8b
child 79639 68dc332516426779a00b6d47f183646b83b0eba3
push id506
push userclegnitto@mozilla.com
push dateWed, 09 Nov 2011 02:03:18 +0000
treeherdermozilla-aurora@63587fc7bb93 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersluke
bugs685783
milestone10.0a1
Bug 685783 - Take advantage of slop bytes in js::Vector. r=luke.
js/public/Vector.h
--- a/js/public/Vector.h
+++ b/js/public/Vector.h
@@ -555,29 +555,36 @@ Vector<T,N,AP>::~Vector()
  */
 template <class T, size_t N, class AP>
 STATIC_POSTCONDITION(!return || newCap >= curLength + lengthInc)
 inline bool
 Vector<T,N,AP>::calculateNewCapacity(size_t curLength, size_t lengthInc,
                                      size_t &newCap)
 {
     size_t newMinCap = curLength + lengthInc;
+    size_t newMinSize = newMinCap * sizeof(T);
 
     /*
      * Check for overflow in the above addition, below CEILING_LOG2, and later
      * multiplication by sizeof(T).
      */
     if (newMinCap < curLength ||
         newMinCap & tl::MulOverflowMask<2 * sizeof(T)>::result) {
         this->reportAllocOverflow();
         return false;
     }
 
-    /* Round up to next power of 2. */
-    newCap = RoundUpPow2(newMinCap);
+    /*
+     * It's best to request 2^N sized chunks because they are unlikely to be
+     * rounded up by the allocator.  (Asking for a 2^N number of elements isn't
+     * as good, because if sizeof(T) is not a power-of-two that would result in
+     * a non-2^N request size, which can cause waste.)
+     */
+    size_t newSize = RoundUpPow2(newMinSize);
+    newCap = newSize / sizeof(T);
 
     /*
      * Do not allow a buffer large enough that the expression ((char *)end() -
      * (char *)begin()) overflows ptrdiff_t. See Bug 510319.
      */
     if (newCap & tl::UnsafeRangeSizeMask<T>::result) {
         this->reportAllocOverflow();
         return false;