From 7fe89b4f7a0a9788086926ea47f102f5e036114a Mon Sep 17 00:00:00 2001 From: Andrii Rosa Date: Thu, 6 Jun 2024 08:17:21 -0700 Subject: [PATCH] Fix HashStringAllocator::clear (#10053) Summary: Pull Request resolved: https://github.com/facebookincubator/velox/pull/10053 Decrement memory counters as in HashStringAllocator::freeToPool Reviewed By: xiaoxmeng, mbasmanova Differential Revision: D58171132 fbshipit-source-id: f87d411022b9b5cd9d996b4d999a7ab6c485171b --- velox/common/memory/HashStringAllocator.cpp | 5 ++++- velox/common/memory/tests/HashStringAllocatorTest.cpp | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/velox/common/memory/HashStringAllocator.cpp b/velox/common/memory/HashStringAllocator.cpp index e8b8b028499b..f18df41b13f1 100644 --- a/velox/common/memory/HashStringAllocator.cpp +++ b/velox/common/memory/HashStringAllocator.cpp @@ -90,7 +90,10 @@ void HashStringAllocator::clear() { freeBytes_ = 0; std::fill(std::begin(freeNonEmpty_), std::end(freeNonEmpty_), 0); for (auto& pair : allocationsFromPool_) { - pool()->free(pair.first, pair.second); + const auto size = pair.second; + pool()->free(pair.first, size); + sizeFromPool_ -= size; + cumulativeBytes_ -= size; } allocationsFromPool_.clear(); for (auto i = 0; i < kNumFreeLists; ++i) { diff --git a/velox/common/memory/tests/HashStringAllocatorTest.cpp b/velox/common/memory/tests/HashStringAllocatorTest.cpp index 7ac868f9027e..e8541f24d77d 100644 --- a/velox/common/memory/tests/HashStringAllocatorTest.cpp +++ b/velox/common/memory/tests/HashStringAllocatorTest.cpp @@ -670,5 +670,13 @@ TEST_F(HashStringAllocatorTest, storeStringFast) { allocator_->checkConsistency(); } +TEST_F(HashStringAllocatorTest, clear) { + allocator_->allocate(HashStringAllocator::kMinAlloc); + allocator_->allocate(HashStringAllocator::kMaxAlloc + 1); + EXPECT_GT(allocator_->retainedSize(), 0); + allocator_->clear(); + EXPECT_EQ(allocator_->retainedSize(), 0); +} + } // namespace } // namespace facebook::velox