Skip to content

Commit 1edd2ba

Browse files
committed
opt: Cut if last addition was minimal weight
In situations where we have UTXO groups of various weight, we can CUT rather than SHIFT when we exceeded the max_weight or the best selection’s weight while the last step was equal to the minimum weight in the lookahead.
1 parent 5248e2a commit 1edd2ba

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

src/wallet/coinselection.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -327,15 +327,20 @@ util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& utxo_pool, c
327327
std::sort(utxo_pool.begin(), utxo_pool.end(), descending_effval_weight);
328328
// The sum of UTXO amounts after this UTXO index, e.g. lookahead[5] = Σ(UTXO[6+].amount)
329329
std::vector<CAmount> lookahead(utxo_pool.size());
330+
// The minimum UTXO weight among the remaining UTXOs after this UTXO index, e.g. min_tail_weight[5] = min(UTXO[6+].weight)
331+
std::vector<int> min_tail_weight(utxo_pool.size());
330332

331-
// Calculate lookahead values, and check that there are sufficient funds
333+
// Calculate lookahead values, min_tail_weights, and check that there are sufficient funds
332334
CAmount total_available = 0;
335+
int min_group_weight = std::numeric_limits<int>::max();
333336
for (size_t i = 0; i < utxo_pool.size(); ++i) {
334337
size_t index = utxo_pool.size() - 1 - i; // Loop over every element in reverse order
335338
lookahead[index] = total_available;
339+
min_tail_weight[index] = min_group_weight;
336340
// UTXOs with non-positive effective value must have been filtered
337341
Assume(utxo_pool[index].GetSelectionAmount() > 0);
338342
total_available += utxo_pool[index].GetSelectionAmount();
343+
min_group_weight = std::min(min_group_weight, utxo_pool[index].m_weight);
339344
}
340345

341346
const CAmount total_target = selection_target + change_target;
@@ -426,16 +431,26 @@ util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& utxo_pool, c
426431
++curr_try;
427432

428433
// EVALUATE current selection: check for solutions and see whether we can CUT or SHIFT before EXPLORING further
429-
if (curr_amount + lookahead[curr_selection.back()] < total_target) {
434+
auto curr_tail = curr_selection.back();
435+
if (curr_amount + lookahead[curr_tail] < total_target) {
430436
// Insufficient funds with lookahead: CUT
431437
should_cut = true;
432438
} else if (curr_weight > max_weight) {
433-
// max_weight exceeded: SHIFT
439+
// max_weight exceeded: CUT if last selected group had minimal weight, else SHIFT
434440
max_tx_weight_exceeded = true;
435-
should_shift = 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+
}
436446
} else if (curr_weight > best_selection_weight) {
437-
// Worse weight than best solution. More UTXOs only increase weight: SHIFT
438-
should_shift = true;
447+
// Worse weight than best solution. More UTXOs only increase weight:
448+
// CUT if last selected group had minimal weight, else SHIFT
449+
if (utxo_pool[curr_tail].m_weight <= min_tail_weight[curr_tail]) {
450+
should_cut = true;
451+
} else {
452+
should_shift = true;
453+
}
439454
} else if (curr_amount >= total_target) {
440455
// Success, adding more weight cannot be better: SHIFT
441456
should_shift = true;

src/wallet/test/coinselector_tests.cpp

Lines changed: 2 additions & 2 deletions
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 = 281;
1234+
size_t expected_attempts = 218;
12351235
BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == expected_attempts, strprintf("Expected %i attempts, but got %i", expected_attempts, res->GetSelectionsEvaluated()));
12361236
}
12371237

@@ -1271,7 +1271,7 @@ BOOST_AUTO_TEST_CASE(coin_grinder_tests)
12711271
BOOST_CHECK(EquivalentResult(expected_result, *res));
12721272
// Demonstrate how following improvements reduce iteration count and catch any regressions in the future.
12731273
// If this takes more attempts, the implementation has regressed
1274-
size_t expected_attempts = 43;
1274+
size_t expected_attempts = 42;
12751275
BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == expected_attempts, strprintf("Expected %i attempts, but got %i", expected_attempts, res->GetSelectionsEvaluated()));
12761276
}
12771277

0 commit comments

Comments
 (0)