Skip to content

Commit

Permalink
opt: Skip checking max_weight separately
Browse files Browse the repository at this point in the history
Initialize `best_selection_weight` as `max_weight` allows us to skip the
separate `max_weight` check on every loop.
  • Loading branch information
murchandamus committed Jan 8, 2024
1 parent d26b54f commit 040817e
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions src/wallet/coinselection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& utxo_pool, c
// CoinGrinder tracks selection via the indices of the currently selected UTXOs
std::vector<size_t> best_selection;
CAmount best_selection_amount = MAX_MONEY;
int best_selection_weight = std::numeric_limits<int>::max();
int best_selection_weight = max_weight; // Tie is fine, because we prefer lower selection amount
bool max_tx_weight_exceeded = false;

std::vector<size_t> curr_selection;
Expand Down Expand Up @@ -318,15 +318,9 @@ util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& utxo_pool, c
if (curr_amount + lookahead[curr_selection.back()] < selection_target + change_target) {
// Insufficient funds with lookahead: CUT
should_cut = true;
} else if (curr_weight > max_weight) {
// max_weight exceeded: CUT if last selected group had minimal weight, else SHIFT
max_tx_weight_exceeded = true;
if (utxo_pool[curr_selection.back()].m_weight <= min_tail_weight[curr_selection.back()]) {
should_cut = true;
} else {
should_shift = true;
}
} else if (curr_weight > best_selection_weight) {
// best_selection_weight is initialized to max_weight
if (curr_weight > max_weight) max_tx_weight_exceeded = true;
// Worse weight than best solution. More UTXOs only increase weight:
// CUT if last selected group had minimal weight, else SHIFT
if (utxo_pool[curr_selection.back()].m_weight <= min_tail_weight[curr_selection.back()]) {
Expand Down

0 comments on commit 040817e

Please sign in to comment.