Bug 1385159 - Use array on the stack to prevent buffer allocation in DrawTargetSkia::DrawGlyphs. draft
authorcku <cku@mozilla.com>
Fri, 28 Jul 2017 15:43:28 +0800
changeset 618278 164df33433a818dcdd4f75217139082bb5b72fc4
parent 618186 6d1b50a370b4adffbb1ee73b9f51707c90d6a2b1
child 618389 2b88cfeaaf3217a0ccf2b56e426043bfaedcbd52
child 618457 b1271636e806bbc671a465fe738a8f031c4925c0
push id71284
push userbmo:cku@mozilla.com
push dateMon, 31 Jul 2017 06:19:12 +0000
bugs1385159
milestone56.0a1
Bug 1385159 - Use array on the stack to prevent buffer allocation in DrawTargetSkia::DrawGlyphs. Originally, I want to use AutoArray to replace std::vector in DrawTargetSkia::DrawGlyphs. But then I realize that we can not use nsTArray.h in moz2D. So I decide to use a c array instead. MozReview-Commit-ID: 2YzN3DUXzTi
gfx/2d/DrawTargetSkia.cpp
--- a/gfx/2d/DrawTargetSkia.cpp
+++ b/gfx/2d/DrawTargetSkia.cpp
@@ -1438,28 +1438,39 @@ DrawTargetSkia::DrawGlyphs(ScaledFont* a
   }
 #endif
   default:
     break;
   }
 
   paint.mPaint.setSubpixelText(useSubpixelText);
 
-  std::vector<uint16_t> indices;
-  std::vector<SkPoint> offsets;
-  indices.resize(aBuffer.mNumGlyphs);
-  offsets.resize(aBuffer.mNumGlyphs);
+  const uint32_t heapSize = 64;
+  uint16_t indicesOnStack[heapSize];
+  SkPoint offsetsOnStack[heapSize];
+  std::vector<uint16_t> indicesOnHeap;
+  std::vector<SkPoint> offsetsOnHeap;
+  uint16_t* indices = indicesOnStack;
+  SkPoint* offsets = offsetsOnStack;
+  if (aBuffer.mNumGlyphs > heapSize) {
+    // Heap allocation/ deallocation is slow, use it only if we need a
+    // bigger(>heapSize) buffer.
+    indicesOnHeap.resize(aBuffer.mNumGlyphs);
+    offsetsOnHeap.resize(aBuffer.mNumGlyphs);
+    indices = (uint16_t*)&indicesOnHeap.front();
+    offsets = (SkPoint*)&offsetsOnHeap.front();
+  }
 
   for (unsigned int i = 0; i < aBuffer.mNumGlyphs; i++) {
     indices[i] = aBuffer.mGlyphs[i].mIndex;
     offsets[i].fX = SkFloatToScalar(aBuffer.mGlyphs[i].mPosition.x);
     offsets[i].fY = SkFloatToScalar(aBuffer.mGlyphs[i].mPosition.y);
   }
 
-  mCanvas->drawPosText(&indices.front(), aBuffer.mNumGlyphs*2, &offsets.front(), paint.mPaint);
+  mCanvas->drawPosText(indices, aBuffer.mNumGlyphs*2, offsets, paint.mPaint);
 }
 
 void
 DrawTargetSkia::FillGlyphs(ScaledFont* aFont,
                            const GlyphBuffer& aBuffer,
                            const Pattern& aPattern,
                            const DrawOptions& aOptions,
                            const GlyphRenderingOptions* aRenderingOptions)