Replace pooling mechanism with GrMemoryPool.
This change is a wash for tests that could fit inside the previous hard-coded pool (512 nodes) and appears to be a 5% improvement for sksl_large. Larger programs would hypothetically show an even more significant improvement. When SK_SUPPORT_GPU is disabled, we disable pooling entirely and fall back to the system allocator. This is necessary because SkSL can exist without Ganesh (such as in the wasm+CanvasKit build). Nanobench: http://screen/4xJEzdGducRxGeq Change-Id: I71dc702a84ab5c163673e35ec651003d7d45dacd Reviewed-on: https://skia-review.googlesource.com/c/skia/+/330219 Commit-Queue: John Stiles <johnstiles@google.com> Reviewed-by: Brian Osman <brianosman@google.com> Auto-Submit: John Stiles <johnstiles@google.com>
This commit is contained in:
parent
018f5f6dcc
commit
67e1cf4b1d
2
BUILD.gn
2
BUILD.gn
@ -560,6 +560,8 @@ if (skia_compile_processors || skia_compile_sksl_tests) {
|
||||
"src/core/SkMath.cpp",
|
||||
"src/core/SkSemaphore.cpp",
|
||||
"src/core/SkThreadID.cpp",
|
||||
"src/gpu/GrBlockAllocator.cpp",
|
||||
"src/gpu/GrMemoryPool.cpp",
|
||||
"src/ports/SkMemory_malloc.cpp",
|
||||
"src/sksl/SkSLMain.cpp",
|
||||
]
|
||||
|
@ -36,6 +36,7 @@ skia_sksl_sources = [
|
||||
"$_src/sksl/SkSLLexer.cpp",
|
||||
"$_src/sksl/SkSLLexer.h",
|
||||
"$_src/sksl/SkSLMemoryLayout.h",
|
||||
"$_src/sksl/SkSLMemoryPool.h",
|
||||
"$_src/sksl/SkSLParser.cpp",
|
||||
"$_src/sksl/SkSLParser.h",
|
||||
"$_src/sksl/SkSLPool.cpp",
|
||||
|
43
src/sksl/SkSLMemoryPool.h
Normal file
43
src/sksl/SkSLMemoryPool.h
Normal file
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 2016 Google Inc.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license that can be
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#ifndef SKSL_MEMORYPOOL
|
||||
#define SKSL_MEMORYPOOL
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "include/core/SkTypes.h"
|
||||
|
||||
#if SK_SUPPORT_GPU
|
||||
|
||||
#include "src/gpu/GrMemoryPool.h"
|
||||
|
||||
namespace SkSL {
|
||||
using MemoryPool = ::GrMemoryPool;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
// When Ganesh is disabled, GrMemoryPool is not linked in. We include a minimal class which mimics
|
||||
// the GrMemoryPool interface but simply redirects to the system allocator.
|
||||
namespace SkSL {
|
||||
|
||||
class MemoryPool {
|
||||
public:
|
||||
static std::unique_ptr<MemoryPool> Make(size_t, size_t) {
|
||||
return std::make_unique<MemoryPool>();
|
||||
}
|
||||
void reportLeaks() const {}
|
||||
bool isEmpty() const { return true; }
|
||||
void* allocate(size_t size) { return ::operator new(size); }
|
||||
void release(void* p) { ::operator delete(p); }
|
||||
};
|
||||
|
||||
} // namespace SkSL
|
||||
|
||||
#endif // SK_SUPPORT_GPU
|
||||
#endif // SKSL_MEMORYPOOL
|
@ -10,132 +10,11 @@
|
||||
#include <bitset>
|
||||
|
||||
#include "include/private/SkMutex.h"
|
||||
#include "src/sksl/ir/SkSLIRNode.h"
|
||||
|
||||
#define VLOG(...) // printf(__VA_ARGS__)
|
||||
|
||||
namespace SkSL {
|
||||
|
||||
namespace {
|
||||
|
||||
template <int kNodeSize, int kNumNodes>
|
||||
class Subpool {
|
||||
public:
|
||||
Subpool() {
|
||||
// Initializes each node in the pool as a free node. The free nodes form a singly-linked
|
||||
// list, each pointing to the next free node in sequence.
|
||||
for (int index = 0; index < kNumNodes - 1; ++index) {
|
||||
fNodes[index].fFreeListNext = &fNodes[index + 1];
|
||||
}
|
||||
fNodes[kNumNodes - 1].fFreeListNext = nullptr;
|
||||
}
|
||||
|
||||
void* poolBegin() {
|
||||
return &fNodes[0];
|
||||
}
|
||||
|
||||
void* poolEnd() {
|
||||
return &fNodes[kNumNodes];
|
||||
}
|
||||
|
||||
void* alloc() {
|
||||
// Does the pool contain a free node?
|
||||
if (!fFreeListHead) {
|
||||
return nullptr;
|
||||
}
|
||||
// Yes. Take a node from the freelist.
|
||||
auto* node = fFreeListHead;
|
||||
fFreeListHead = node->fFreeListNext;
|
||||
return node->fBuffer;
|
||||
}
|
||||
|
||||
void free(void* node_v) {
|
||||
SkASSERT(this->isValidNodePtrInPool(node_v));
|
||||
|
||||
// Push a node back onto the freelist.
|
||||
auto* node = static_cast<Subpool::Node*>(node_v);
|
||||
node->fFreeListNext = fFreeListHead;
|
||||
fFreeListHead = node;
|
||||
}
|
||||
|
||||
bool isValidNodePtrInPool(void* node_v) {
|
||||
// Verify that the pointer exists in our subpool at all.
|
||||
if (node_v < this->poolBegin()) {
|
||||
return false;
|
||||
}
|
||||
if (node_v >= this->poolEnd()) {
|
||||
return false;
|
||||
}
|
||||
// Verify that the pointer points to the start of a node, not the middle.
|
||||
intptr_t offsetInPool = (intptr_t)node_v - (intptr_t)this->poolBegin();
|
||||
return (offsetInPool % kNodeSize) == 0;
|
||||
}
|
||||
|
||||
void checkForLeaks() {
|
||||
#ifdef SK_DEBUG
|
||||
// Walk the free list and mark each node. We should encounter every item in the pool.
|
||||
std::bitset<kNumNodes> freed;
|
||||
for (Node* node = fFreeListHead; node; node = node->fFreeListNext) {
|
||||
ptrdiff_t nodeIndex = this->nodeIndex(node);
|
||||
freed[nodeIndex] = true;
|
||||
}
|
||||
// Look for any bit left unset above, and report it as a leak.
|
||||
bool foundLeaks = false;
|
||||
for (int index = 0; index < kNumNodes; ++index) {
|
||||
if (!freed[index]) {
|
||||
SkDebugf("Node %d leaked: ", index);
|
||||
IRNode* leak = reinterpret_cast<IRNode*>(fNodes[index].fBuffer);
|
||||
SkDebugf("%s\n", leak->description().c_str());
|
||||
foundLeaks = true;
|
||||
}
|
||||
}
|
||||
if (foundLeaks) {
|
||||
SkDEBUGFAIL("leaking SkSL pool nodes; if they are later freed, this will "
|
||||
"likely be fatal");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// Accessors.
|
||||
constexpr int nodeCount() { return kNumNodes; }
|
||||
|
||||
int nodeIndex(void* node_v) {
|
||||
SkASSERT(this->isValidNodePtrInPool(node_v));
|
||||
|
||||
auto* node = static_cast<Subpool::Node*>(node_v);
|
||||
return SkToInt(node - fNodes);
|
||||
}
|
||||
|
||||
private:
|
||||
struct Node {
|
||||
union {
|
||||
uint8_t fBuffer[kNodeSize];
|
||||
Node* fFreeListNext;
|
||||
};
|
||||
};
|
||||
|
||||
// This holds the first free node in the pool. It will be null when the pool is exhausted.
|
||||
Node* fFreeListHead = fNodes;
|
||||
|
||||
// Our pooled data lives here.
|
||||
Node fNodes[kNumNodes];
|
||||
};
|
||||
|
||||
static constexpr int kSmallNodeSize = 120;
|
||||
static constexpr int kNumSmallNodes = 480;
|
||||
using SmallSubpool = Subpool<kSmallNodeSize, kNumSmallNodes>;
|
||||
|
||||
static constexpr int kLargeNodeSize = 240;
|
||||
static constexpr int kNumLargeNodes = 20;
|
||||
using LargeSubpool = Subpool<kLargeNodeSize, kNumLargeNodes>;
|
||||
|
||||
} // namespace
|
||||
|
||||
struct PoolData {
|
||||
SmallSubpool fSmall;
|
||||
LargeSubpool fLarge;
|
||||
};
|
||||
|
||||
#if defined(SK_BUILD_FOR_IOS) && \
|
||||
(!defined(__IPHONE_9_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_9_0)
|
||||
|
||||
@ -153,24 +32,24 @@ static pthread_key_t get_pthread_key() {
|
||||
return sKey;
|
||||
}
|
||||
|
||||
static PoolData* get_thread_local_pool_data() {
|
||||
static PoolData* get_thread_local_memory_pool() {
|
||||
return static_cast<PoolData*>(pthread_getspecific(get_pthread_key()));
|
||||
}
|
||||
|
||||
static void set_thread_local_pool_data(PoolData* poolData) {
|
||||
static void set_thread_local_memory_pool(PoolData* poolData) {
|
||||
pthread_setspecific(get_pthread_key(), poolData);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
static thread_local PoolData* sPoolData = nullptr;
|
||||
static thread_local MemoryPool* sMemPool = nullptr;
|
||||
|
||||
static PoolData* get_thread_local_pool_data() {
|
||||
return sPoolData;
|
||||
static MemoryPool* get_thread_local_memory_pool() {
|
||||
return sMemPool;
|
||||
}
|
||||
|
||||
static void set_thread_local_pool_data(PoolData* poolData) {
|
||||
sPoolData = poolData;
|
||||
static void set_thread_local_memory_pool(MemoryPool* memPool) {
|
||||
sMemPool = memPool;
|
||||
}
|
||||
|
||||
#endif
|
||||
@ -182,16 +61,15 @@ static SkMutex& recycled_pool_mutex() {
|
||||
}
|
||||
|
||||
Pool::~Pool() {
|
||||
if (get_thread_local_pool_data() == fData) {
|
||||
if (get_thread_local_memory_pool() == fMemPool.get()) {
|
||||
SkDEBUGFAIL("SkSL pool is being destroyed while it is still attached to the thread");
|
||||
set_thread_local_pool_data(nullptr);
|
||||
set_thread_local_memory_pool(nullptr);
|
||||
}
|
||||
|
||||
fData->fSmall.checkForLeaks();
|
||||
fData->fLarge.checkForLeaks();
|
||||
fMemPool->reportLeaks();
|
||||
SkASSERT(fMemPool->isEmpty());
|
||||
|
||||
VLOG("DELETE Pool:0x%016llX\n", (uint64_t)fData);
|
||||
delete fData;
|
||||
VLOG("DELETE Pool:0x%016llX\n", (uint64_t)fMemPool.get());
|
||||
}
|
||||
|
||||
std::unique_ptr<Pool> Pool::Create() {
|
||||
@ -200,19 +78,19 @@ std::unique_ptr<Pool> Pool::Create() {
|
||||
if (sRecycledPool) {
|
||||
pool = std::unique_ptr<Pool>(sRecycledPool);
|
||||
sRecycledPool = nullptr;
|
||||
VLOG("REUSE Pool:0x%016llX\n", (uint64_t)pool->fData);
|
||||
VLOG("REUSE Pool:0x%016llX\n", (uint64_t)pool->fMemPool.get());
|
||||
} else {
|
||||
pool = std::unique_ptr<Pool>(new Pool);
|
||||
pool->fData = new PoolData;
|
||||
VLOG("CREATE Pool:0x%016llX\n", (uint64_t)pool->fData);
|
||||
pool->fMemPool = MemoryPool::Make(/*preallocSize=*/65536, /*minAllocSize=*/32768);
|
||||
VLOG("CREATE Pool:0x%016llX\n", (uint64_t)pool->fMemPool.get());
|
||||
}
|
||||
return pool;
|
||||
}
|
||||
|
||||
void Pool::Recycle(std::unique_ptr<Pool> pool) {
|
||||
if (pool) {
|
||||
pool->fData->fSmall.checkForLeaks();
|
||||
pool->fData->fLarge.checkForLeaks();
|
||||
pool->fMemPool->reportLeaks();
|
||||
SkASSERT(pool->fMemPool->isEmpty());
|
||||
}
|
||||
|
||||
SkAutoMutexExclusive lock(recycled_pool_mutex());
|
||||
@ -220,77 +98,49 @@ void Pool::Recycle(std::unique_ptr<Pool> pool) {
|
||||
delete sRecycledPool;
|
||||
}
|
||||
|
||||
VLOG("STASH Pool:0x%016llX\n", pool ? (uint64_t)pool->fData : 0ull);
|
||||
VLOG("STASH Pool:0x%016llX\n", pool ? (uint64_t)pool->fMemPool.get() : 0ull);
|
||||
sRecycledPool = pool.release();
|
||||
}
|
||||
|
||||
void Pool::attachToThread() {
|
||||
VLOG("ATTACH Pool:0x%016llX\n", (uint64_t)fData);
|
||||
SkASSERT(get_thread_local_pool_data() == nullptr);
|
||||
set_thread_local_pool_data(fData);
|
||||
VLOG("ATTACH Pool:0x%016llX\n", (uint64_t)fMemPool.get());
|
||||
SkASSERT(get_thread_local_memory_pool() == nullptr);
|
||||
set_thread_local_memory_pool(fMemPool.get());
|
||||
}
|
||||
|
||||
void Pool::detachFromThread() {
|
||||
VLOG("DETACH Pool:0x%016llX\n", (uint64_t)get_thread_local_pool_data());
|
||||
SkASSERT(get_thread_local_pool_data() != nullptr);
|
||||
set_thread_local_pool_data(nullptr);
|
||||
VLOG("DETACH Pool:0x%016llX\n", (uint64_t)get_thread_local_memory_pool());
|
||||
SkASSERT(get_thread_local_memory_pool() != nullptr);
|
||||
set_thread_local_memory_pool(nullptr);
|
||||
}
|
||||
|
||||
void* Pool::AllocIRNode(size_t size) {
|
||||
// Is a pool attached?
|
||||
PoolData* poolData = get_thread_local_pool_data();
|
||||
if (poolData) {
|
||||
if (size <= kSmallNodeSize) {
|
||||
// The node will fit in the small pool.
|
||||
auto* node = poolData->fSmall.alloc();
|
||||
if (node) {
|
||||
VLOG("ALLOC Pool:0x%016llX Index:S%03d 0x%016llX\n",
|
||||
(uint64_t)poolData, poolData->fSmall.nodeIndex(node), (uint64_t)node);
|
||||
return node;
|
||||
}
|
||||
} else if (size <= kLargeNodeSize) {
|
||||
// Try to allocate a large node.
|
||||
auto* node = poolData->fLarge.alloc();
|
||||
if (node) {
|
||||
VLOG("ALLOC Pool:0x%016llX Index:L%03d 0x%016llX\n",
|
||||
(uint64_t)poolData, poolData->fLarge.nodeIndex(node), (uint64_t)node);
|
||||
return node;
|
||||
}
|
||||
}
|
||||
MemoryPool* memPool = get_thread_local_memory_pool();
|
||||
if (memPool) {
|
||||
void* node = memPool->allocate(size);
|
||||
VLOG("ALLOC Pool:0x%016llX 0x%016llX\n", (uint64_t)memPool, (uint64_t)node);
|
||||
return node;
|
||||
}
|
||||
|
||||
// The pool can't be used for this allocation. Allocate nodes using the system allocator.
|
||||
void* ptr = ::operator new(size);
|
||||
VLOG("ALLOC Pool:0x%016llX Index:____ malloc 0x%016llX\n",
|
||||
(uint64_t)poolData, (uint64_t)ptr);
|
||||
return ptr;
|
||||
// There's no pool attached. Allocate nodes using the system allocator.
|
||||
void* node = ::operator new(size);
|
||||
VLOG("ALLOC Pool:__________________ 0x%016llX\n", (uint64_t)node);
|
||||
return node;
|
||||
}
|
||||
|
||||
void Pool::FreeIRNode(void* node) {
|
||||
// Is a pool attached?
|
||||
PoolData* poolData = get_thread_local_pool_data();
|
||||
if (poolData) {
|
||||
// Did this node come from either of our pools?
|
||||
if (node >= poolData->fSmall.poolBegin()) {
|
||||
if (node < poolData->fSmall.poolEnd()) {
|
||||
poolData->fSmall.free(node);
|
||||
VLOG("FREE Pool:0x%016llX Index:S%03d 0x%016llX\n",
|
||||
(uint64_t)poolData, poolData->fSmall.nodeIndex(node), (uint64_t)node);
|
||||
return;
|
||||
} else if (node < poolData->fLarge.poolEnd()) {
|
||||
poolData->fLarge.free(node);
|
||||
VLOG("FREE Pool:0x%016llX Index:L%03d 0x%016llX\n",
|
||||
(uint64_t)poolData, poolData->fLarge.nodeIndex(node), (uint64_t)node);
|
||||
return;
|
||||
}
|
||||
}
|
||||
MemoryPool* memPool = get_thread_local_memory_pool();
|
||||
if (memPool) {
|
||||
VLOG("FREE Pool:0x%016llX 0x%016llX\n", (uint64_t)memPool, (uint64_t)node);
|
||||
memPool->release(node);
|
||||
return;
|
||||
}
|
||||
|
||||
// We couldn't associate this node with our pool. Free it using the system allocator.
|
||||
VLOG("FREE Pool:0x%016llX Index:____ free 0x%016llX\n",
|
||||
(uint64_t)poolData, (uint64_t)node);
|
||||
// There's no pool attached. Free it using the system allocator.
|
||||
VLOG("FREE Pool:__________________ 0x%016llX\n", (uint64_t)node);
|
||||
::operator delete(node);
|
||||
}
|
||||
|
||||
|
||||
} // namespace SkSL
|
||||
|
@ -10,10 +10,16 @@
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "src/sksl/SkSLMemoryPool.h"
|
||||
|
||||
namespace SkSL {
|
||||
|
||||
class IRNode;
|
||||
struct PoolData;
|
||||
/**
|
||||
* Efficiently allocates memory for IRNodes in an SkSL program. Optimized for allocate/release
|
||||
* performance over memory efficiency.
|
||||
*
|
||||
* All allocated IRNodes must be released back to the pool before it can be destroyed or recycled.
|
||||
*/
|
||||
|
||||
class Pool {
|
||||
public:
|
||||
@ -53,7 +59,7 @@ private:
|
||||
void checkForLeaks();
|
||||
|
||||
Pool() = default; // use Create to make a pool
|
||||
PoolData* fData = nullptr;
|
||||
std::unique_ptr<SkSL::MemoryPool> fMemPool;
|
||||
};
|
||||
|
||||
} // namespace SkSL
|
||||
|
@ -65,7 +65,7 @@ public:
|
||||
// purposes
|
||||
int fOffset;
|
||||
|
||||
// Override operator new and delete to allow us to control allocation behavior.
|
||||
// Override operator new and delete to allow us to use a memory pool.
|
||||
static void* operator new(const size_t size) {
|
||||
return Pool::AllocIRNode(size);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user