Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit bfcf8c4

Browse files
herbderbySkia Commit-Bot
authored andcommitted
simplify next allocation size
Since the fibonacci sequence is always multiplied by fFirstHeapAllocationSize, change the names ofr fFib0 and fFib1 to fNextHeapAlloc, and fYetNextHeapAlloc, and assign both to be fFirstHeapAllocationSize. This will create the same sequence of heap allocation sizes. Now fFirstHeapAllocationSize is only used in reset(). This will simplify CL/307348 which move reset to a different class. Change-Id: I54da1980239645acd36992476915137dd68ab9da Reviewed-on: https://skia-review.googlesource.com/c/skia/+/307349 Reviewed-by: Mike Klein <mtklein@google.com> Commit-Queue: Herb Derby <herb@google.com>
1 parent 10f019c commit bfcf8c4

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

src/core/SkArenaAlloc.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ static uint32_t first_allocated_block(uint32_t blockSize, uint32_t firstHeapAllo
1717
}
1818

1919
SkArenaAlloc::SkArenaAlloc(char* block, size_t size, size_t firstHeapAllocation)
20-
: fDtorCursor {block}
21-
, fCursor {block}
22-
, fEnd {block + ToU32(size)}
23-
, fFirstBlock {block}
24-
, fFirstSize {ToU32(size)}
25-
, fFirstHeapAllocationSize {first_allocated_block(ToU32(size), ToU32(firstHeapAllocation))}
20+
: fDtorCursor{block}
21+
, fCursor{block}
22+
, fEnd{block + ToU32(size)}
23+
, fFirstBlock{block}
24+
, fFirstSize{ToU32(size)}
25+
, fFirstHeapAllocationSize{first_allocated_block(ToU32(size), ToU32(firstHeapAllocation))}
26+
, fNextHeapAlloc{fFirstHeapAllocationSize}
27+
, fYetNextHeapAlloc{fNextHeapAlloc}
2628
{
2729
if (size < sizeof(Footer)) {
2830
fEnd = fCursor = fDtorCursor = nullptr;
@@ -110,13 +112,14 @@ void SkArenaAlloc::ensureSpace(uint32_t size, uint32_t alignment) {
110112
objSizeAndOverhead += alignmentOverhead;
111113
}
112114

113-
uint32_t minAllocationSize;
114-
if (fFirstHeapAllocationSize <= maxSize / fFib0) {
115-
minAllocationSize = fFirstHeapAllocationSize * fFib0;
116-
fFib0 += fFib1;
117-
std::swap(fFib0, fFib1);
115+
uint32_t minAllocationSize = fNextHeapAlloc;
116+
117+
// Calculate the next heap alloc that won't overflow.
118+
if (fYetNextHeapAlloc <= maxSize - fNextHeapAlloc) {
119+
fNextHeapAlloc += fYetNextHeapAlloc;
120+
std::swap(fNextHeapAlloc, fYetNextHeapAlloc);
118121
} else {
119-
minAllocationSize = maxSize;
122+
fNextHeapAlloc = maxSize;
120123
}
121124
uint32_t allocationSize = std::max(objSizeAndOverhead, minAllocationSize);
122125

src/core/SkArenaAlloc.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,21 @@ class SkArenaAlloc {
223223
const uint32_t fFirstSize;
224224
const uint32_t fFirstHeapAllocationSize;
225225

226-
// Use the Fibonacci sequence as the growth factor for block size. The size of the block
227-
// allocated is fFib0 * fFirstHeapAllocationSize. Using 2 ^ n * fFirstHeapAllocationSize
228-
// had too much slop for Android.
229-
uint32_t fFib0 {1}, fFib1 {1};
226+
// We found allocating strictly doubling amounts of memory from the heap left too
227+
// much unused slop, particularly on Android. Instead we'll follow a Fibonacci-like
228+
// progression that's simple to implement and grows with roughly a 1.6 exponent:
229+
//
230+
// To start,
231+
// fNextHeapAlloc = fYetNextHeapAlloc = 1*fFirstHeapAllocationSize;
232+
//
233+
// And then when we do allocate, follow a Fibonacci f(n+2) = f(n+1) + f(n) rule:
234+
// void* block = malloc(fNextHeapAlloc);
235+
// std::swap(fNextHeapAlloc, fYetNextHeapAlloc)
236+
// fYetNextHeapAlloc += fNextHeapAlloc;
237+
//
238+
// That makes the nth allocation fib(n) * fFirstHeapAllocationSize bytes.
239+
uint32_t fNextHeapAlloc, // How many bytes minimum will we allocate next from the heap?
240+
fYetNextHeapAlloc; // And then how many the next allocation after that?
230241
};
231242

232243
// Helper for defining allocators with inline/reserved storage.

0 commit comments

Comments
 (0)