Skip to content

Commit 5f84f3c

Browse files
committed
opt: Skip branches with worse weight
Once we exceed the weight of the current best selection, we can always shift as adding more inputs can never yield a better solution.
1 parent d68bc74 commit 5f84f3c

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

src/wallet/coinselection.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,9 @@ util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& utxo_pool, c
413413
// max_weight exceeded: SHIFT
414414
max_tx_weight_exceeded = true;
415415
should_shift = true;
416+
} else if (curr_weight > best_selection_weight) {
417+
// Worse weight than best solution. More UTXOs only increase weight: SHIFT
418+
should_shift = true;
416419
} else if (curr_amount >= total_target) {
417420
// Success, adding more weight cannot be better: SHIFT
418421
should_shift = true;

src/wallet/test/coinselector_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,7 @@ BOOST_AUTO_TEST_CASE(coin_grinder_tests)
12311231
add_coin(4 * COIN, 3, expected_result);
12321232
BOOST_CHECK(EquivalentResult(expected_result, *res));
12331233
// Demonstrate how following improvements reduce iteration count and catch any regressions in the future.
1234-
size_t expected_attempts = 2041;
1234+
size_t expected_attempts = 525;
12351235
BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == expected_attempts, strprintf("Expected %i attempts, but got %i", expected_attempts, res->GetSelectionsEvaluated()));
12361236
}
12371237

0 commit comments

Comments
 (0)