443663 {
443664 static constexpr idx_t OPTIMIZATION_ITERATIONS_MULTIPLIER = 5;
443665
443666
443667 optional_idx state_index;
443668 vector<reference<const TemporaryMemoryState>> states;
443669 vector<idx_t> res;
443670 vector<double> der;
443671 idx_t sum_of_initial_res = 0;
443672
443676 sum_of_initial_res += initial_reservation;
443678 state_index = i;
443679 } else if (initial_reservation >= state.get().GetRemainingSize()) {
443680 continue;
443681 }
443682 states.emplace_back(state);
443683 res.push_back(initial_reservation);
443684 der.push_back(NumericLimits<double>::Maximum());
443685 i++;
443686 }
443687 const idx_t n = i;
443688
443690 return res[state_index.GetIndex()];
443691 }
443692 const auto free_memory =
memory_limit - sum_of_initial_res;
443693
443694
443695 idx_t remaining_memory = free_memory;
443696 const idx_t optimization_iterations = OPTIMIZATION_ITERATIONS_MULTIPLIER * n;
443697 for (
idx_t opt_idx = 0; opt_idx < optimization_iterations; opt_idx++) {
443698 D_ASSERT(remaining_memory != 0);
443699 ComputeDerivatives(states, res, der, n);
443700
443701
443702 idx_t min_idx = 0;
443703 double min_der = NumericLimits<double>::Maximum();
443704 for (i = 0; i < n; i++) {
443705 auto &state = states[i].get();
443706 if (res[i] >= state.GetRemainingSize()) {
443707 continue;
443708 }
443709 if (der[i] < min_der) {
443710 min_idx = i;
443711 min_der = der[i];
443712 }
443713 }
443714 auto &min_state = states[min_idx].get();
443715
443716
443717 const auto iter_memory = ExactNumericCast<idx_t>(
443718 std::ceil(static_cast<double>(remaining_memory) / static_cast<double>(optimization_iterations - opt_idx)));
443719
443720
443721 const auto state_room = min_state.GetRemainingSize() - res[min_idx];
443722 const auto delta = MinValue(iter_memory, state_room);
443723 D_ASSERT(delta <= remaining_memory);
443724
443725
443726 res[min_idx] += delta;
443727 remaining_memory -= delta;
443728
443729
443730
443731 if (opt_idx == optimization_iterations - 1 && delta < iter_memory) {
443732 opt_idx--;
443733 }
443734 }
443735 D_ASSERT(remaining_memory == 0);
443736
443737
443738
443739
443740
443741
443742
443743
443744 vector<idx_t> idxs(n, 0);
443745 vector<idx_t> max_res(n, 0);
443746 for (i = 0; i < n; i++) {
443747 idxs[i] = i;
443748 max_res[i] = states[i].get().GetRemainingSize() - 1;
443749 }
443750 ComputeDerivatives(states, max_res, der, n);
443751 std::sort(idxs.begin(), idxs.end(), [&](
const idx_t &lhs,
const idx_t &rhs) { return der[lhs] < der[rhs]; });
443752
443753
443754 remaining_memory = free_memory;
443755 for (const auto idx : idxs) {
443756 auto &state = states[idx].get();
443757 D_ASSERT(res[idx] <= state.GetRemainingSize());
443759
443760 const auto state_remaining = initial_state_reservation + remaining_memory;
443762
443763 auto num_other_states = MinValue(MAXIMUM_REMAINING_STATE_RESERVATIONS,
num_connections);
443766 auto state_reservation = MinValue(res[idx], upper_bound);
443767
443768 state_reservation = MaxValue(state_reservation, initial_state_reservation);
443769
443770 if (idx == state_index.GetIndex()) {
443771 return state_reservation;
443772 }
443773
443774 const auto delta = state_reservation - initial_state_reservation;
443775 remaining_memory -= delta;
443776 }
443777
443778
443779 throw InternalException("Did not find state_index in ComputeOptimalReservation");
443780}
static constexpr idx_t MINIMUM_REMAINING_STATE_RESERVATIONS
Or, we leave room for between this many minimum reservations (if it is less than MAXIMUM_FREE_MEMORY_...
Definition duckdb.cpp:103061
static idx_t ComputeInitialReservation(const TemporaryMemoryState &temporary_memory_state)
Compute initial reservation for use in ComputeReservation.
Definition duckdb.cpp:443622
bool RefersToSameObject(const reference< T > &a, const reference< T > &b)
Returns whether or not two reference wrappers refer to the same object.
Definition duckdb.hpp:2191