From aa4e2c56b561918aa51c66a86a3b54381bd2425e Mon Sep 17 00:00:00 2001 From: Doris Wu Date: Sat, 15 Nov 2025 10:23:27 +0800 Subject: [PATCH] buffer update opt: Skip releaseFreeSlots when no frees are pending (#9437) --- filament/src/details/BufferAllocator.cpp | 17 +++++++++++++++-- filament/src/details/BufferAllocator.h | 3 ++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/filament/src/details/BufferAllocator.cpp b/filament/src/details/BufferAllocator.cpp index 2e60dfdcc0..e7692b8ad0 100644 --- a/filament/src/details/BufferAllocator.cpp +++ b/filament/src/details/BufferAllocator.cpp @@ -142,7 +142,11 @@ void BufferAllocator::retire(AllocationId id) { InternalSlotNode* targetNode = getNodeById(id); assert_invariant(targetNode != nullptr); - targetNode->slot.isAllocated = false; + Slot& slot = targetNode->slot; + slot.isAllocated = false; + if (slot.gpuUseCount == 0) { + mHasPendingFrees = true; + } } void BufferAllocator::acquireGpu(AllocationId id) { @@ -157,10 +161,18 @@ void BufferAllocator::releaseGpu(AllocationId id) { assert_invariant(targetNode != nullptr); assert_invariant(targetNode->slot.gpuUseCount > 0); - targetNode->slot.gpuUseCount--; + Slot& slot = targetNode->slot; + slot.gpuUseCount--; + if (slot.gpuUseCount == 0 && !slot.isAllocated) { + mHasPendingFrees = true; + } } void BufferAllocator::releaseFreeSlots() { + if (!mHasPendingFrees) { + return; + } + auto curr = mSlotPool.begin(); while (curr != mSlotPool.end()) { if (!curr->slot.isFree()) { @@ -199,6 +211,7 @@ void BufferAllocator::releaseFreeSlots() { curr = next; } + mHasPendingFrees = false; } BufferAllocator::allocation_size_t BufferAllocator::getTotalSize() const noexcept { diff --git a/filament/src/details/BufferAllocator.h b/filament/src/details/BufferAllocator.h index f42c30588d..0cfdc568f9 100644 --- a/filament/src/details/BufferAllocator.h +++ b/filament/src/details/BufferAllocator.h @@ -113,8 +113,9 @@ private: [[nodiscard]] InternalSlotNode* getNodeById(AllocationId id) const noexcept; + bool mHasPendingFrees = false; allocation_size_t mTotalSize; - const allocation_size_t mSlotSize; // Size of a single slot in bytes. + const allocation_size_t mSlotSize; // Size of a single slot in bytes std::list mSlotPool; // All slots, including both allocated and freed std::multimap mFreeList; std::unordered_map mOffsetMap;