Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply previous UTXO shortcut optimization #67

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ A basic performance comparison between this Rust BnB implementation and [Bitcoin

|implementation|pool size|ns/iter|
|-------------:|---------|-------|
| Rust BnB| 1,000|897,810|
| Rust BnB| 1,000|598,370|
| C++ Core BnB| 1,000|816,374|

Note: The measurements where recorded using rustc 1.75. Expect worse performance with MSRV.
Expand Down
39 changes: 32 additions & 7 deletions src/branch_and_bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,18 +273,23 @@ pub fn select_coins_bnb<Utxo: WeightedUtxo>(
// * Add next node to the inclusion branch.
else {
let (eff_value, utxo_waste, _) = w_utxos[index];
current_waste = current_waste.checked_add(utxo_waste)?;

index_selection.push(index);

// unchecked add is used here for performance. Since the sum of all utxo values
// did not overflow, then any positive subset of the sum will not overflow.
value = value.unchecked_add(eff_value);

// unchecked sub is used her for performance.
// The bounds for available_value are at most the sum of utxos
// and at least zero.
available_value = available_value.unchecked_sub(eff_value);

if index_selection.is_empty()
|| index - 1 == *index_selection.last().unwrap()
|| w_utxos[index].0 != w_utxos[index - 1].0
{
index_selection.push(index);
current_waste = current_waste.checked_add(utxo_waste)?;

// unchecked add is used here for performance. Since the sum of all utxo values
// did not overflow, then any positive subset of the sum will not overflow.
value = value.unchecked_add(eff_value);
}
}

// no overflow is possible since the iteration count is bounded.
Expand Down Expand Up @@ -651,6 +656,26 @@ mod tests {
assert_coin_select_params(&params, Some(&["10 cBTC", "6 cBTC", "2 cBTC"]));
}

#[test]
fn select_coins_bnb_early_bail_optimization() {
let mut utxos = vec!["7 cBTC", "7 cBTC", "7 cBTC", "7 cBTC", "2 cBTC"];
for _i in 0..50_000 {
utxos.push("5 cBTC");
}
let params = ParamsStr {
target: "30 cBTC",
cost_of_change: "5000 sats",
fee_rate: "0",
lt_fee_rate: "0",
weighted_utxos: utxos,
};

assert_coin_select_params(
&params,
Some(&["7 cBTC", "7 cBTC", "7 cBTC", "7 cBTC", "2 cBTC"]),
);
}

#[test]
fn select_coins_bnb_exhaust() {
// Recreate make_hard from bitcoin core test suit.
Expand Down
Loading