Skip to content

Commit

Permalink
opt: Skip checking max_weight separately
Browse files Browse the repository at this point in the history
Init best_selection_weight as max_weight allows us to skip the separate
max_weight check on every loop.
  • Loading branch information
murchandamus committed Sep 26, 2023
1 parent 6ba9c1c commit dc6fe3a
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions src/wallet/coinselection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& utxo_pool, c

std::vector<size_t> best_selection; // best selection UTXO indices
CAmount best_selection_amount = MAX_MONEY;
int best_selection_weight = INT_MAX;
int best_selection_weight = max_weight; // Tie is fine, because we prefer lower selection amount

std::vector<size_t> curr_selection; // selected UTXO indices
CAmount curr_amount = 0;
Expand Down Expand Up @@ -350,15 +350,9 @@ util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& utxo_pool, c
if (curr_amount + lookahead.at(utxo_pool_index) < selection_target + change_target) {
// Cannot succeed due to insufficient funds in lookahead: CUT (deselect latest, SHIFT)
next_op = operations::cut;
} 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.at(curr_selection.back()).m_weight <= min_tail_weight.at(curr_selection.back())) {
next_op = operations::cut;
} else {
next_op = operations::shift;
}
} 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.at(curr_selection.back()).m_weight <= min_tail_weight.at(curr_selection.back())) {
Expand All @@ -369,7 +363,7 @@ util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& utxo_pool, c
} else if (curr_amount >= selection_target + change_target) {
// Target exceeded: check if new best, then SHIFT (deselect latest, select next)
next_op = operations::shift;
if (curr_weight < best_selection_weight || (curr_weight == best_selection_weight && curr_amount < best_selection_amount)) {
if (curr_weight < best_selection_weight || curr_amount < best_selection_amount) {
best_selection = curr_selection;
best_selection_weight = curr_weight;
best_selection_amount = curr_amount;
Expand Down

0 comments on commit dc6fe3a

Please sign in to comment.