From c274f8f942bf756fc78ab01992aecb855231f9e3 Mon Sep 17 00:00:00 2001 From: Florin Malita Date: Wed, 18 Apr 2018 16:57:32 -0400 Subject: Prevent unnecessary/unbounded growth of SkTDynamicHash capacity SkTDynamicHash doesn't immediately recycle slots for removed entries, but instead just marks them as deleted. The only way to reclaim deleted slots currently is when an exponential grow/resize is triggered. A consequence of this is that the capacity/allocated storage can grow indefinitely when the hash is long-lived and churning -- even if the number of active entries is small/stable. To prevent this, I propose we only grow the capacity when the number of active slots constitutes a significant portion. Otherwise (when most slots are deleted), we trigger a "purge" (resize to the same capacity) to clear the tombstones. Bug: chromium:832482 Change-Id: Iefdcd7439f7d62ac021e176b71007d207c8bc876 Reviewed-on: https://skia-review.googlesource.com/122082 Reviewed-by: Mike Klein Commit-Queue: Florin Malita --- src/core/SkTDynamicHash.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'src/core') diff --git a/src/core/SkTDynamicHash.h b/src/core/SkTDynamicHash.h index b144d18d48..77d020fa39 100644 --- a/src/core/SkTDynamicHash.h +++ b/src/core/SkTDynamicHash.h @@ -242,7 +242,16 @@ private: void maybeGrow() { if (100 * (fCount + fDeleted + 1) > fCapacity * kGrowPercent) { - this->resize(fCapacity > 0 ? fCapacity * 2 : 4); + auto newCapacity = fCapacity > 0 ? fCapacity : 4; + + // Only grow the storage when most non-empty entries are + // in active use. Otherwise, just purge the tombstones. + if (fCount > fDeleted) { + newCapacity *= 2; + } + SkASSERT(newCapacity > fCount + 1); + + this->resize(newCapacity); } } -- cgit v1.2.3