| 270 | } |
| 271 | |
| 272 | void WindowBuild::ensureInputFits(const RowVectorPtr& input) { |
| 273 | if (spillConfig_ == nullptr) { |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | const int64_t numRows = data_->numRows(); |
| 278 | if (numRows == 0) { |
| 279 | // 'data_' is empty. Nothing to spill. |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | auto [freeRows, outOfLineFreeBytes] = data_->freeSpace(); |
| 284 | const auto outOfLineBytes = |
| 285 | data_->stringAllocator().retainedSize() - outOfLineFreeBytes; |
| 286 | const int64_t flatInputBytes = input->usedSize(); |
| 287 | |
| 288 | // Test-only spill path. |
| 289 | if (numRows > 0 && testingTriggerSpill()) { |
| 290 | sortSpill(); |
| 291 | return; |
| 292 | } |
| 293 | |
| 294 | // If current memory usage exceeds spilling threshold, trigger spilling. |
| 295 | const auto currentMemoryUsage = pool_->currentBytes(); |
| 296 | if (spillMemoryThreshold_ != 0 && |
| 297 | currentMemoryUsage > spillMemoryThreshold_) { |
| 298 | spill(); |
| 299 | return; |
| 300 | } |
| 301 | |
| 302 | const auto minReservationBytes = |
| 303 | currentMemoryUsage * spillConfig_->minSpillableReservationPct / 100; |
| 304 | const auto availableReservationBytes = pool_->availableReservation(); |
| 305 | const int64_t estimatedIncrementalBytes = |
| 306 | data_->sizeIncrement(input->size(), outOfLineBytes ? flatInputBytes : 0); |
| 307 | |
| 308 | if (availableReservationBytes > minReservationBytes) { |
| 309 | // If we have enough free rows for input rows and enough variable length |
| 310 | // free space for the vector's flat size, no need for spilling. |
| 311 | if (freeRows > input->size() && |
| 312 | (outOfLineBytes == 0 || outOfLineFreeBytes >= flatInputBytes)) { |
| 313 | return; |
| 314 | } |
| 315 | |
| 316 | // If the current available reservation in memory pool is 2X the |
| 317 | // estimatedIncrementalBytes, no need to spill. |
| 318 | if (availableReservationBytes > 2 * estimatedIncrementalBytes) { |
| 319 | return; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | // Try reserving targetIncrementBytes more in memory pool, if succeed, no |
| 324 | // need to spill. |
| 325 | const auto targetIncrementBytes = std::max<int64_t>( |
| 326 | estimatedIncrementalBytes * 2, |
| 327 | currentMemoryUsage * spillConfig_->spillableReservationGrowthPct / 100); |
| 328 | { |
| 329 | memory::ReclaimableSectionGuard guard(nonReclaimableSection_); |
nothing calls this directly
no test coverage detected