This is an automated email from the ASF dual-hosted git repository.

zhztheplayer pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gluten.git


The following commit(s) were added to refs/heads/main by this push:
     new 04e447122d [GLUTEN-13627][VL] Skip spill when shrinking satisfies the 
reclaim target (#13628)
04e447122d is described below

commit 04e447122d70721c82530d68e17b05a5910c872f
Author: Wechar Yu <[email protected]>
AuthorDate: Fri Nov 27 01:38:57 2026 +0800

    [GLUTEN-13627][VL] Skip spill when shrinking satisfies the reclaim target 
(#13628)
---
 cpp/velox/compute/WholeStageResultIterator.cc |  9 +++++++
 cpp/velox/tests/MemoryManagerTest.cc          | 35 +++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)

diff --git a/cpp/velox/compute/WholeStageResultIterator.cc 
b/cpp/velox/compute/WholeStageResultIterator.cc
index 8eae65d443..7a07ed4690 100644
--- a/cpp/velox/compute/WholeStageResultIterator.cc
+++ b/cpp/velox/compute/WholeStageResultIterator.cc
@@ -336,8 +336,17 @@ int64_t WholeStageResultIterator::spillFixedSize(int64_t 
size) {
   std::string poolName{pool->root()->name() + "/" + pool->name()};
   std::string logPrefix{"Spill[" + poolName + "]: "};
   int64_t shrunken = memoryManager_->shrink(size);
+  if (shrunken >= size) {
+    LOG(INFO) << fmt::format(
+        "{} Reclaimed {} by shrinking for a target of {}; skipping spill.",
+        logPrefix,
+        velox::succinctBytes(shrunken),
+        velox::succinctBytes(size));
+    return shrunken;
+  }
   if (spillStrategy_ == "auto") {
     int64_t remaining = size - shrunken;
+    VELOX_CHECK_GT(remaining, 0);
     LOG(INFO) << fmt::format("{} trying to request spill for {}.", logPrefix, 
velox::succinctBytes(remaining));
     auto mm = memoryManager_->getMemoryManager();
     uint64_t spilledOut = mm->arbitrator()->shrinkCapacity(remaining); // this 
conducts spill
diff --git a/cpp/velox/tests/MemoryManagerTest.cc 
b/cpp/velox/tests/MemoryManagerTest.cc
index 1367e2e048..ee88643a7c 100644
--- a/cpp/velox/tests/MemoryManagerTest.cc
+++ b/cpp/velox/tests/MemoryManagerTest.cc
@@ -16,9 +16,11 @@
  */
 
 #include "compute/VeloxBackend.h"
+#include "compute/WholeStageResultIterator.h"
 #include "config/VeloxConfig.h"
 #include "memory/VeloxMemoryManager.h"
 #include "velox/common/base/tests/GTestUtils.h"
+#include "velox/common/testutil/TempDirectoryPath.h"
 
 namespace gluten {
 
@@ -330,6 +332,39 @@ void assertCapacitiesMatch(MockSparkTaskMemoryManager& 
tmm, std::vector<std::uni
 }
 } // namespace
 
+TEST_F(MemoryManagerTest, spillOnlyWhenShrinkIsInsufficient) {
+  auto pool = vmm_->getLeafMemoryPool();
+  std::vector<void*> buffers{nullptr};
+  pool->setReclaimer(std::make_unique<MockMemoryReclaimer>(buffers, kMB));
+
+  auto spillDir = common::testutil::TempDirectoryPath::create();
+  auto plan = std::make_shared<core::ValuesNode>("values", 
std::vector<RowVectorPtr>{});
+  auto conf = std::make_shared<config::ConfigBase>(
+      std::unordered_map<std::string, std::string>{{kSessionTimezone, "UTC"}, 
{kSpillStrategy, "auto"}});
+  WholeStageResultIterator iterator(vmm_.get(), plan, {}, {}, {}, nullptr, 
nullptr, {}, spillDir->getPath(), conf, {});
+
+  // Request less than, exactly, and more than the available free capacity.
+  for (int64_t adjustment : {-1, 0, 1}) {
+    SCOPED_TRACE(adjustment);
+    vmm_->shrink(0);
+    buffers[0] = pool->allocate(kMB);
+    auto cleanup = folly::makeGuard([&]() {
+      if (buffers[0] != nullptr) {
+        pool->free(buffers[0], kMB);
+        buffers[0] = nullptr;
+      }
+    });
+
+    const int64_t freeBytes = vmm_->getAggregateMemoryPool()->freeBytes();
+    ASSERT_GT(freeBytes, 1);
+
+    const int64_t requestedBytes = freeBytes + adjustment;
+    EXPECT_GE(iterator.spillFixedSize(requestedBytes), requestedBytes);
+    // Trigger spill only when reqeusted bytes is more than the free capacity.
+    EXPECT_EQ(buffers[0] == nullptr, adjustment > 0);
+  }
+}
+
 class MultiMemoryManagerTest : public ::testing::Test {
  protected:
   static void SetUpTestCase() {


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to