Move to a constant for "bucket one" in the scoring buckets 2023-04-expose-success-prob
authorMatt Corallo <git@bluematt.me>
Thu, 7 Sep 2023 22:34:30 +0000 (22:34 +0000)
committerMatt Corallo <git@bluematt.me>
Fri, 15 Sep 2023 17:27:31 +0000 (17:27 +0000)
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.

lightning/src/routing/scoring.rs

index b13f5531ec77f970e7fa4f9be330d93386af0142..5fdbf9ae3a9c8eff91ba0948ec86d8f9f3f7bdec 100644 (file)
@@ -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) {
@@ -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
@@ -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;
                        }
 
@@ -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;