Optimize calculation of prioritization fees stats (#301)

* Optimize calculation of prioritization fees stats

* comment on arrays in response

* Fix the calculation of supp

* Fix fmt

---------

Co-authored-by: GroovieGermanikus <groovie@mango.markets>
This commit is contained in:
galactus 2024-01-25 16:06:38 +01:00 committed by GitHub
parent 7171b524f0
commit 7821d29975
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 19 deletions

View File

@ -10,10 +10,15 @@ pub struct TxAggregateStats {
#[derive(Clone, Serialize, Debug)]
pub struct PrioFeesStats {
// the arrays are same size and ordered monotonically
pub by_tx: Vec<u64>,
pub by_tx_percentiles: Vec<f32>,
// the arrays are same size and ordered monotonically
pub by_cu: Vec<u64>,
pub by_cu_percentiles: Vec<f32>,
// per block stats
pub tx_count: TxAggregateStats,
pub cu_consumed: TxAggregateStats,
}

View File

@ -1,6 +1,5 @@
use crate::rpc_data::FeePoint;
use itertools::Itertools;
use std::collections::HashMap;
use std::iter::zip;
/// `quantile` function is the same as the median if q=50, the same as the minimum if q=0 and the same as the maximum if q=100.
@ -39,25 +38,22 @@ pub fn calculate_supp_percentiles(
// get stats by CU
let cu_sum: u64 = prio_fees_in_block.iter().map(|x| x.1).sum();
let mut dist_fee_by_cu: HashMap<i32, u64> = HashMap::new();
let mut agg: u64 = 0;
let mut p = 0;
let mut agg: u64 = prio_fees_in_block[0].1;
let mut index = 0;
let p_step = 5;
for (prio, cu) in &prio_fees_in_block {
agg += cu;
// write p's as long as agg beats the aggregated cu
while agg >= (cu_sum as f64 * p as f64 / 100.0) as u64 && p <= 100 {
dist_fee_by_cu.insert(p, *prio);
assert_ne!(p_step, 0, "zero steps might cause infinite loop");
p += p_step;
}
}
let dist_fee_by_cu: Vec<FeePoint> = dist_fee_by_cu
.into_iter()
.sorted_by_key(|(p, _)| *p)
.map(|(p, fees)| FeePoint {
p: p as u32,
v: fees,
let dist_fee_by_cu = (0..=100)
.step_by(p_step)
.map(|p| {
if agg < (cu_sum * p) / 100 {
index += 1;
agg += prio_fees_in_block[index].1;
}
let (prio, _) = prio_fees_in_block[index];
FeePoint {
p: p as u32,
v: prio,
}
})
.collect_vec();