aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>2014-01-09 16:45:45 +0000
committerGravatar commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>2014-01-09 16:45:45 +0000
commitff007e8ff358d9253de22291ebfd5fe7980512ed (patch)
treedb842fe62afea5f9b24b77d78d5facfd4eb288ae /src
parent96ab95f45800eda0c5a101cd65046b00a22bcc81 (diff)
Allocate SkPictureFlat::fScratch lazily.
Cuts out half to two thirds of allocations of this object. Since we're already lazily allocating fWriteBuffer, we don't have to add any other code complexity. BUG=http://skbug.com/1980 R=mtklein@google.com, tomhudson@google.com Author: tomhudson@chromium.org Review URL: https://codereview.chromium.org/131483005 git-svn-id: http://skia.googlecode.com/svn/trunk@12992 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src')
-rw-r--r--src/core/SkPictureFlat.h27
1 files changed, 17 insertions, 10 deletions
diff --git a/src/core/SkPictureFlat.h b/src/core/SkPictureFlat.h
index 220ae11c0e..57daa5e786 100644
--- a/src/core/SkPictureFlat.h
+++ b/src/core/SkPictureFlat.h
@@ -376,10 +376,10 @@ class SkFlatDictionary {
public:
explicit SkFlatDictionary(SkFlatController* controller)
: fController(SkRef(controller))
- , fScratchSize(kScratchSizeGuess)
- , fScratch(AllocScratch(fScratchSize))
+ , fScratchSize(0)
+ , fScratch(NULL)
, fWriteBuffer(kWriteBufferGrowthBytes)
- , fWriteBufferReady(false) {
+ , fReady(false) {
this->reset();
}
@@ -515,18 +515,24 @@ private:
}
// We have to delay fWriteBuffer's initialization until its first use; fController might not
- // be fully set up by the time we get it in the constructor.
- void lazyWriteBufferInit() {
- if (fWriteBufferReady) {
+ // be fully set up by the time we get it in the constructor. We also delay allocating fScratch
+ // to avoid unnecessary heap allocations, since we're paying the price of the conditional
+ // anyway.
+ void lazyInit() {
+ if (fReady) {
return;
}
+
+ fScratchSize = kScratchSizeGuess;
+ fScratch = AllocScratch(fScratchSize);
+
// Without a bitmap heap, we'll flatten bitmaps into paints. That's never what you want.
SkASSERT(fController->getBitmapHeap() != NULL);
fWriteBuffer.setBitmapHeap(fController->getBitmapHeap());
fWriteBuffer.setTypefaceRecorder(fController->getTypefaceSet());
fWriteBuffer.setNamedFactoryRecorder(fController->getNamedFactorySet());
fWriteBuffer.setFlags(fController->getWriteBufferFlags());
- fWriteBufferReady = true;
+ fReady = true;
}
// As findAndReturnFlat, but returns a mutable pointer for internal use.
@@ -546,7 +552,7 @@ private:
// This reference is valid only until the next call to resetScratch() or detachScratch().
const SkFlatData& resetScratch(const T& element, int index) {
- this->lazyWriteBufferInit();
+ this->lazyInit();
// Flatten element into fWriteBuffer (using fScratch as storage).
fWriteBuffer.reset(fScratch->data(), fScratchSize);
@@ -577,6 +583,7 @@ private:
// Allocate a new SkFlatData exactly big enough to hold our current scratch.
// We use the controller for this allocation to extend the allocation's lifetime and allow
// the controller to do whatever memory management it wants.
+ SkASSERT(fScratch != NULL);
const size_t paddedSize = SizeWithPadding(fScratch->flatSize());
SkFlatData* detached = (SkFlatData*)fController->allocThrow(paddedSize);
@@ -596,9 +603,9 @@ private:
// All SkFlatData* stored in fIndexedData and fHash are owned by the controller.
SkAutoTUnref<SkFlatController> fController;
size_t fScratchSize; // How many bytes fScratch has allocated for data itself.
- SkFlatData* fScratch; // Owned, must be freed with sk_free.
+ SkFlatData* fScratch; // Owned, lazily allocated, must be freed with sk_free.
SkOrderedWriteBuffer fWriteBuffer;
- bool fWriteBufferReady;
+ bool fReady;
// We map between SkFlatData and a 1-based integer index.
int fNextIndex;