diff --git a/velox/exec/RowContainer.cpp b/velox/exec/RowContainer.cpp index 8566f5fb1212..ed99701ce9d6 100644 --- a/velox/exec/RowContainer.cpp +++ b/velox/exec/RowContainer.cpp @@ -336,7 +336,7 @@ char* RowContainer::initializeRow(char* row, bool reuse) { } void RowContainer::eraseRows(folly::Range rows) { - freeRowsExtraMemory(rows, false); + freeRowsExtraMemory(rows, /*freeNextRowVector=*/true); for (auto* row : rows) { VELOX_CHECK(!bits::isBitSet(row, freeFlagOffset_), "Double free of row"); bits::setBit(row, freeFlagOffset_); @@ -457,31 +457,11 @@ void RowContainer::freeAggregates(folly::Range rows) { } } -void RowContainer::freeNextRowVectors(folly::Range rows, bool clear) { +void RowContainer::freeNextRowVectors(folly::Range rows) { if (!nextOffset_ || !hasDuplicateRows_) { return; } - if (clear) { - for (auto row : rows) { - auto vector = getNextRowVector(row); - if (vector) { - // Clear all rows, we can clear the nextOffset_ slots and delete the - // next-row-vector. - for (auto& next : *vector) { - getNextRowVector(next) = nullptr; - } - // Because of 'parallelJoinBuild', the memory for the next row vector - // may not be allocated from the RowContainer to which the row belongs, - // hence we need to release memory through the vector's allocator. - auto allocator = vector->get_allocator().allocator(); - std::destroy_at(vector); - allocator->free(HashStringAllocator::headerOf(vector)); - } - } - return; - } - for (auto row : rows) { auto vector = getNextRowVector(row); if (vector) { @@ -500,10 +480,14 @@ void RowContainer::freeNextRowVectors(folly::Range rows, bool clear) { } } -void RowContainer::freeRowsExtraMemory(folly::Range rows, bool clear) { +void RowContainer::freeRowsExtraMemory( + folly::Range rows, + bool freeNextRowVector) { freeVariableWidthFields(rows); freeAggregates(rows); - freeNextRowVectors(rows, clear); + if (freeNextRowVector) { + freeNextRowVectors(rows); + } numRows_ -= rows.size(); } @@ -959,7 +943,9 @@ void RowContainer::clear() { std::vector rows(kBatch); RowContainerIterator iter; while (auto numRows = listRows(&iter, kBatch, rows.data())) { - freeRowsExtraMemory(folly::Range(rows.data(), numRows), true); + freeRowsExtraMemory( + folly::Range(rows.data(), numRows), + /*freeNextRowVector=*/false); } } hasDuplicateRows_ = false; diff --git a/velox/exec/RowContainer.h b/velox/exec/RowContainer.h index 39ae2aa568c4..7dc2303d582e 100644 --- a/velox/exec/RowContainer.h +++ b/velox/exec/RowContainer.h @@ -1368,9 +1368,9 @@ class RowContainer { void freeAggregates(folly::Range rows); // Free next row vectors associated with the 'rows'. - void freeNextRowVectors(folly::Range rows, bool clear); + void freeNextRowVectors(folly::Range rows); - void freeRowsExtraMemory(folly::Range rows, bool clear); + void freeRowsExtraMemory(folly::Range rows, bool freeNextRowVector); // Updates the specific column's columnHasNulls_ flag, if 'hasNulls' is true. // columnHasNulls_ flag is false by default.