Skip to content

Commit b7672c7

Browse files
committed
opt: Skip checking max_weight separately
Initialize `best_selection_weight` as `max_weight` allows us to skip the separate `max_weight` check on every loop.
1 parent 1edd2ba commit b7672c7

File tree

1 file changed

+3
-9
lines changed

1 file changed

+3
-9
lines changed

src/wallet/coinselection.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& utxo_pool, c
359359

360360
// The weight of the currently selected input set, and the weight of the best selection
361361
int curr_weight = 0;
362-
int best_selection_weight = std::numeric_limits<int>::max();
362+
int best_selection_weight = max_weight; // Tie is fine, because we prefer lower selection amount
363363

364364
// Whether the input sets generated during this search have exceeded the maximum transaction weight at any point
365365
bool max_tx_weight_exceeded = false;
@@ -435,15 +435,9 @@ util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& utxo_pool, c
435435
if (curr_amount + lookahead[curr_tail] < total_target) {
436436
// Insufficient funds with lookahead: CUT
437437
should_cut = true;
438-
} else if (curr_weight > max_weight) {
439-
// max_weight exceeded: CUT if last selected group had minimal weight, else SHIFT
440-
max_tx_weight_exceeded = true;
441-
if (utxo_pool[curr_tail].m_weight <= min_tail_weight[curr_tail]) {
442-
should_cut = true;
443-
} else {
444-
should_shift = true;
445-
}
446438
} else if (curr_weight > best_selection_weight) {
439+
// best_selection_weight is initialized to max_weight
440+
if (curr_weight > max_weight) max_tx_weight_exceeded = true;
447441
// Worse weight than best solution. More UTXOs only increase weight:
448442
// CUT if last selected group had minimal weight, else SHIFT
449443
if (utxo_pool[curr_tail].m_weight <= min_tail_weight[curr_tail]) {

0 commit comments

Comments
 (0)