Skip to content

Commit

Permalink
Move to a constant for "bucket one" in the scoring buckets
Browse files Browse the repository at this point in the history
Scoring buckets are stored as fixed point ints, with a 5-bit
fractional part (i.e. a value of 1.0 is stored as "32"). Now that
we also have 32 buckets, this leads to the codebase having many
references to 32 which could reasonably be confused for each other.

Thus, we add a constant here for the value 1.0 in our fixed-point
scheme.
  • Loading branch information
TheBlueMatt committed Sep 15, 2023
1 parent f7f524f commit 9437642
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lightning/src/routing/scoring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1684,6 +1684,10 @@ mod bucketed_history {
buckets: [u16; 32],
}

/// Buckets are stored in fixed point numbers with a 5 bit fractional part. Thus, the value
/// "one" is 32, or this constant.
pub const BUCKET_FIXED_POINT_ONE: u16 = 32;

impl HistoricalBucketRangeTracker {
pub(super) fn new() -> Self { Self { buckets: [0; 32] } }
pub(super) fn track_datapoint(&mut self, liquidity_offset_msat: u64, capacity_msat: u64) {
Expand Down Expand Up @@ -1714,7 +1718,7 @@ mod bucketed_history {
*e = ((*e as u32) * 2047 / 2048) as u16;
}
let bucket = pos_to_bucket(pos);
self.buckets[bucket] = self.buckets[bucket].saturating_add(32);
self.buckets[bucket] = self.buckets[bucket].saturating_add(BUCKET_FIXED_POINT_ONE);
}
}
/// Decay all buckets by the given number of half-lives. Used to more aggressively remove old
Expand Down Expand Up @@ -1768,7 +1772,8 @@ mod bucketed_history {

// If the total valid points is smaller than 1.0 (i.e. 32 in our fixed-point scheme),
// treat it as if we were fully decayed.
if total_valid_points_tracked.checked_shr(required_decays).unwrap_or(0) < 32*32 {
const FULLY_DECAYED: u16 = BUCKET_FIXED_POINT_ONE * BUCKET_FIXED_POINT_ONE;
if total_valid_points_tracked.checked_shr(required_decays).unwrap_or(0) < FULLY_DECAYED.into() {
return None;
}

Expand Down Expand Up @@ -1806,7 +1811,7 @@ mod bucketed_history {
let mut highest_max_bucket_with_points = 0; // The highest max-bucket with any data
let mut total_max_points = 0; // Total points in max-buckets to consider
for (max_idx, max_bucket) in self.max_liquidity_offset_history.buckets.iter().enumerate() {
if *max_bucket >= 32 {
if *max_bucket >= BUCKET_FIXED_POINT_ONE {
highest_max_bucket_with_points = cmp::max(highest_max_bucket_with_points, max_idx);
}
total_max_points += *max_bucket as u64;
Expand Down

0 comments on commit 9437642

Please sign in to comment.