Bug 1277896 - add Vector::initLengthUninitialized (r=waldo)
authorLuke Wagner <luke@mozilla.com>
Fri, 10 Jun 2016 13:28:04 -0500
changeset 301557 03765412b2a8f37753041a4cea5e498325803924
parent 301556 0ac2613adaeefd45f9cde2d4d16746484a1be5e5
child 301558 5dc26d9549244df2f1de9a648ba79ff1fe793b65
push id19672
push usercbook@mozilla.com
push dateSun, 12 Jun 2016 09:33:05 +0000
treeherderfx-team@016e0f47e8ad [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerswaldo
bugs1277896
milestone50.0a1
Bug 1277896 - add Vector::initLengthUninitialized (r=waldo) MozReview-Commit-ID: He6Kzr0giLU
mfbt/Vector.h
--- a/mfbt/Vector.h
+++ b/mfbt/Vector.h
@@ -526,22 +526,31 @@ public:
   /* mutators */
 
   /**
    * Reverse the order of the elements in the vector in place.
    */
   void reverse();
 
   /**
-   * Given that the vector is empty and has no inline storage, grow to
-   * |capacity|.
+   * Given that the vector is empty, grow the internal capacity to |aRequest|,
+   * keeping the length 0.
    */
   MOZ_MUST_USE bool initCapacity(size_t aRequest);
 
   /**
+   * Given that the vector is empty, grow the internal capacity and length to
+   * |aRequest| leaving the elements' memory completely uninitialized (with all
+   * the associated hazards and caveats). This avoids the usual allocation-size
+   * rounding that happens in resize and overhead of initialization for elements
+   * that are about to be overwritten.
+   */
+  MOZ_MUST_USE bool initLengthUninitialized(size_t aRequest);
+
+  /**
    * If reserve(aRequest) succeeds and |aRequest >= length()|, then appending
    * |aRequest - length()| elements, in any sequence of append/appendAll calls,
    * is guaranteed to succeed.
    *
    * A request to reserve an amount less than the current length does not affect
    * reserved space.
    */
   MOZ_MUST_USE bool reserve(size_t aRequest);
@@ -966,16 +975,27 @@ Vector<T, N, AP>::initCapacity(size_t aR
 #ifdef DEBUG
   mReserved = aRequest;
 #endif
   return true;
 }
 
 template<typename T, size_t N, class AP>
 inline bool
+Vector<T, N, AP>::initLengthUninitialized(size_t aRequest)
+{
+  if (!initCapacity(aRequest)) {
+    return false;
+  }
+  infallibleGrowByUninitialized(aRequest);
+  return true;
+}
+
+template<typename T, size_t N, class AP>
+inline bool
 Vector<T, N, AP>::maybeCheckSimulatedOOM(size_t aRequestedSize)
 {
   if (aRequestedSize <= N) {
     return true;
   }
 
 #ifdef DEBUG
   if (aRequestedSize <= mReserved) {