Bug 1286871 - Only reserve std::vector when necessary. r=jgilbert draft
authorMorris Tseng <mtseng@mozilla.com>
Mon, 05 Sep 2016 14:18:52 +0800
changeset 409819 d4106c222be3bec02664d30a2a7c0c5c95bb19d7
parent 409737 dbe4b47941c7b3d6298a0ead5e40dd828096c808
child 530422 0af359f0d35ebf508981e157f37a6c728e8ba2df
push id28558
push userbmo:mtseng@mozilla.com
push dateMon, 05 Sep 2016 08:04:24 +0000
reviewersjgilbert
bugs1286871
milestone51.0a1
Bug 1286871 - Only reserve std::vector when necessary. r=jgilbert MozReview-Commit-ID: 6q6VizxQnqV
dom/canvas/WebGLFramebuffer.cpp
--- a/dom/canvas/WebGLFramebuffer.cpp
+++ b/dom/canvas/WebGLFramebuffer.cpp
@@ -1054,17 +1054,16 @@ WebGLFramebuffer::ValidateAndInitAttachm
                                                    errorText.BeginReading());
         return false;
     }
 
     // Cool! We've checked out ok. Just need to initialize.
 
     //////
     // Check if we need to initialize anything
-
     std::vector<WebGLFBAttachPoint*> tex3DToClear;
 
     const auto fnGatherIf3D = [&](WebGLFBAttachPoint& attach) {
         if (!attach.Texture())
             return false;
 
         const auto& info = attach.Texture()->ImageInfoAt(attach.ImageTarget(),
                                                          attach.MipLevel());
@@ -1073,42 +1072,50 @@ WebGLFramebuffer::ValidateAndInitAttachm
 
         tex3DToClear.push_back(&attach);
         return true;
     };
 
     //////
 
     uint32_t clearBits = 0;
-    std::vector<GLenum> drawBuffersForClear(1 + mMoreColorAttachments.Size(),
-                                            LOCAL_GL_NONE);
-
+    std::vector<GLenum> drawBuffersForClear;
     std::vector<WebGLFBAttachPoint*> attachmentsToClear;
-    attachmentsToClear.reserve(1 + mMoreColorAttachments.Size() + 3);
 
     const auto fnGatherColor = [&](WebGLFBAttachPoint& attach, uint32_t colorAttachNum) {
         if (!IsDrawBuffer(colorAttachNum) || !attach.HasUninitializedImageData())
             return;
 
         if (fnGatherIf3D(attach))
             return;
 
-        attachmentsToClear.push_back(&attach);
+        if (attachmentsToClear.empty()) {
+            attachmentsToClear.reserve(1 + mMoreColorAttachments.Size() + 3);
+        }
+
+        if (drawBuffersForClear.empty()) {
+            drawBuffersForClear.assign(1 + mMoreColorAttachments.Size(), LOCAL_GL_NONE);
+        }
 
         clearBits |= LOCAL_GL_COLOR_BUFFER_BIT;
+        attachmentsToClear.push_back(&attach);
         drawBuffersForClear[colorAttachNum] = LOCAL_GL_COLOR_ATTACHMENT0 + colorAttachNum;
     };
 
     const auto fnGatherOther = [&](WebGLFBAttachPoint& attach, GLenum attachClearBits) {
         if (!attach.HasUninitializedImageData())
             return;
 
         if (fnGatherIf3D(attach))
             return;
 
+        if (attachmentsToClear.empty()) {
+            attachmentsToClear.reserve(1 + mMoreColorAttachments.Size() + 3);
+        }
+
         attachmentsToClear.push_back(&attach);
 
         clearBits |= attachClearBits;
     };
 
     //////
 
     fnGatherColor(mColorAttachment0, 0);
@@ -1120,16 +1127,18 @@ WebGLFramebuffer::ValidateAndInitAttachm
     }
 
     fnGatherOther(mDepthAttachment, LOCAL_GL_DEPTH_BUFFER_BIT);
     fnGatherOther(mStencilAttachment, LOCAL_GL_STENCIL_BUFFER_BIT);
     fnGatherOther(mDepthStencilAttachment,
                   LOCAL_GL_DEPTH_BUFFER_BIT | LOCAL_GL_STENCIL_BUFFER_BIT);
 
     //////
+    if (!clearBits && tex3DToClear.empty())
+        return true;
 
     mContext->MakeContextCurrent();
 
     if (clearBits) {
         const auto fnDrawBuffers = [this](const std::vector<GLenum>& list) {
             this->mContext->gl->fDrawBuffers(list.size(), list.data());
         };