Skip to content

Commit

Permalink
Interleave bucket calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBlueMatt committed Dec 12, 2023
1 parent a4b487b commit 78ea786
Showing 1 changed file with 42 additions and 12 deletions.
54 changes: 42 additions & 12 deletions lightning/src/routing/scoring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1904,34 +1904,64 @@ mod bucketed_history {

for (min_idx, min_bucket) in min_liquidity_offset_history_buckets.iter().enumerate().skip(1) {
let min_bucket_start_pos = BUCKET_START_POS[min_idx];
let max_max_idx = 31 - min_idx;
if payment_pos < min_bucket_start_pos {
for (max_idx, max_bucket) in max_liquidity_offset_history_buckets.iter().enumerate().take(32 - min_idx) {
let max_bucket_end_pos = BUCKET_START_POS[32 - max_idx] - 1;
if payment_pos >= max_bucket_end_pos {
for (idx, chunk) in max_liquidity_offset_history_buckets.chunks(2).enumerate().take(16 - min_idx/2) {
let max_idx_a = idx * 2;
let max_idx_b = idx * 2 + 1;

let max_bucket_a = chunk[0];
let mut max_bucket_b = *chunk.get(1).unwrap_or(&0);

let max_bucket_end_pos_a = BUCKET_START_POS[32 - max_idx_a] - 1;
if payment_pos >= max_bucket_end_pos_a {
// Success probability 0, the payment amount may be above the max liquidity
break;
}
let max_bucket_end_pos_b = BUCKET_START_POS[32 - max_idx_b] - 1;
if max_idx_b > max_max_idx || payment_pos >= max_bucket_end_pos_b { max_bucket_b = 0 }

// Note that this multiply can only barely not overflow - two 16 bit ints plus
// 30 bits is 62 bits.
let bucket_prob_times_billion = ((*min_bucket as u32) * (*max_bucket as u32)) as u64
let bucket_prob_times_billion_a = ((*min_bucket as u32) * (max_bucket_a as u32)) as u64
* 1024 * 1024 * 1024 / total_valid_points_tracked;
let bucket_prob_times_billion_b = ((*min_bucket as u32) * (max_bucket_b as u32)) as u64
* 1024 * 1024 * 1024 / total_valid_points_tracked;
debug_assert!(bucket_prob_times_billion < u32::max_value() as u64);
cumulative_success_prob_times_billion += bucket_prob_times_billion;
debug_assert!(bucket_prob_times_billion_a < u32::max_value() as u64);
debug_assert!(bucket_prob_times_billion_b < u32::max_value() as u64);
cumulative_success_prob_times_billion += bucket_prob_times_billion_a;
cumulative_success_prob_times_billion += bucket_prob_times_billion_b;
}
} else {
for (max_idx, max_bucket) in max_liquidity_offset_history_buckets.iter().enumerate().take(32 - min_idx) {
let max_bucket_end_pos = BUCKET_START_POS[32 - max_idx] - 1;
if payment_pos >= max_bucket_end_pos {
for (idx, chunk) in max_liquidity_offset_history_buckets.chunks(2).enumerate().take(16 - min_idx/2) {
let max_idx_a = idx * 2;
let max_idx_b = idx * 2 + 1;

let max_bucket_a = chunk[0];
let mut max_bucket_b = *chunk.get(1).unwrap_or(&0);

let max_bucket_end_pos_a = BUCKET_START_POS[32 - max_idx_a] - 1;
if payment_pos >= max_bucket_end_pos_a {
// Success probability 0, the payment amount may be above the max liquidity
break;
}
let max_bucket_end_pos_b = BUCKET_START_POS[32 - max_idx_b] - 1;
if max_idx_b > max_max_idx || payment_pos >= max_bucket_end_pos_b { max_bucket_b = 0 }

// Note that this multiply can only barely not overflow - two 16 bit ints plus
// 30 bits is 62 bits.
let bucket_points = ((*min_bucket as u32) * (*max_bucket as u32)) as u64;
let bucket_points_a = ((*min_bucket as u32) * (max_bucket_a as u32)) as u64;
let bucket_points_b = ((*min_bucket as u32) * (max_bucket_b as u32)) as u64;
cumulative_success_prob_times_billion += success_probability_times_value_times_billion(
payment_pos as u64, min_bucket_start_pos as u64,
max_bucket_end_pos as u64, POSITION_TICKS as u64 - 1, params, true,
bucket_points, total_valid_points_tracked);
max_bucket_end_pos_a as u64, POSITION_TICKS as u64 - 1, params, true,
bucket_points_a, total_valid_points_tracked);
if payment_pos < max_bucket_end_pos_b {
cumulative_success_prob_times_billion += success_probability_times_value_times_billion(
payment_pos as u64, min_bucket_start_pos as u64,
max_bucket_end_pos_b as u64, POSITION_TICKS as u64 - 1, params, true,
bucket_points_b, total_valid_points_tracked);
}
}
}
}
Expand Down

0 comments on commit 78ea786

Please sign in to comment.