diff --git a/crates/brk_computer/src/internal/from_height/computed/delta.rs b/crates/brk_computer/src/internal/from_height/computed/delta.rs index cf21e694c..96d336d8b 100644 --- a/crates/brk_computer/src/internal/from_height/computed/delta.rs +++ b/crates/brk_computer/src/internal/from_height/computed/delta.rs @@ -25,7 +25,7 @@ use crate::{ /// Pre-collect source data from the earliest needed offset. /// Returns (source_data, offset) for use in compute_delta_window. -pub(super) fn collect_source( +fn collect_source( source: &impl ReadableVec, skip: usize, earliest_starts: &impl ReadableVec, @@ -45,8 +45,7 @@ pub(super) fn compute_delta_window( rate_bps_h: &mut EagerVec>, max_from: Height, starts: &impl ReadableVec, - source_data: &[S], - offset: usize, + source: &impl ReadableVec, exit: &Exit, ) -> Result<()> where @@ -54,10 +53,18 @@ where C: NumericValue, B: NumericValue, { + let skip = change_h.len(); + let (mut source_data, mut offset) = collect_source(source, skip, starts); + change_h.compute_transform( max_from, starts, |(h, ago_h, ..)| { + if h.to_usize() < offset || ago_h.to_usize() < offset { + // Version reset cleared the vec — re-collect from scratch + source_data = source.collect(); + offset = 0; + } let current: f64 = source_data[h.to_usize() - offset].into(); let ago: f64 = source_data[ago_h.to_usize() - offset].into(); (h, C::from(current - ago)) @@ -69,6 +76,11 @@ where max_from, &*change_h, |(h, change, ..)| { + if h.to_usize() < offset { + // Version reset cleared the vec — re-collect from scratch + source_data = source.collect(); + offset = 0; + } let current_f: f64 = source_data[h.to_usize() - offset].into(); let change_f: f64 = change.into(); let ago = current_f - change_f; @@ -127,10 +139,6 @@ where source: &impl ReadableVec, exit: &Exit, ) -> Result<()> { - // Pre-collect once using the widest window (1y has earliest ago heights) - let skip = self.change.0._24h.height.len(); - let (source_data, offset) = collect_source(source, skip, windows._1y); - for ((change_w, rate_w), starts) in self .change .0 @@ -144,8 +152,7 @@ where &mut rate_w.bps.height, max_from, *starts, - &source_data, - offset, + source, exit, )?; } @@ -201,16 +208,12 @@ where source: &impl ReadableVec, exit: &Exit, ) -> Result<()> { - let skip = self.change_1m.height.len(); - let (source_data, offset) = collect_source(source, skip, height_1m_ago); - compute_delta_window( &mut self.change_1m.height, &mut self.rate_1m.bps.height, max_from, height_1m_ago, - &source_data, - offset, + source, exit, ) } @@ -294,10 +297,6 @@ where source: &impl ReadableVec, exit: &Exit, ) -> Result<()> { - // Pre-collect once using the widest window (1y has earliest ago heights) - let skip = self.change_24h.height.len(); - let (source_data, offset) = collect_source(source, skip, windows._1y); - let changes = [&mut self.change_24h, &mut self.change_1w, &mut self.change_1y]; let rates = [&mut self.rate_24h, &mut self.rate_1w, &mut self.rate_1y]; let starts = [windows._24h, windows._1w, windows._1y]; @@ -308,8 +307,7 @@ where &mut rate_w.bps.height, max_from, starts, - &source_data, - offset, + source, exit, )?; } diff --git a/crates/brk_computer/src/internal/from_height/computed/fiat_delta.rs b/crates/brk_computer/src/internal/from_height/computed/fiat_delta.rs index 433901596..8ff9e2923 100644 --- a/crates/brk_computer/src/internal/from_height/computed/fiat_delta.rs +++ b/crates/brk_computer/src/internal/from_height/computed/fiat_delta.rs @@ -15,7 +15,7 @@ use crate::{ }, }; -use super::delta::{collect_source, compute_delta_window}; +use super::delta::compute_delta_window; /// Fiat 1m-only delta: fiat change (cents + usd) + rate for the 1-month window. #[derive(Traversable)] @@ -64,16 +64,12 @@ where source: &impl ReadableVec, exit: &Exit, ) -> Result<()> { - let skip = self.change_1m.cents.height.len(); - let (source_data, offset) = collect_source(source, skip, height_1m_ago); - compute_delta_window( &mut self.change_1m.cents.height, &mut self.rate_1m.bps.height, max_from, height_1m_ago, - &source_data, - offset, + source, exit, ) } @@ -156,9 +152,6 @@ where source: &impl ReadableVec, exit: &Exit, ) -> Result<()> { - let skip = self.change_24h.cents.height.len(); - let (source_data, offset) = collect_source(source, skip, windows._1y); - let changes: [&mut FiatFromHeight; 3] = [&mut self.change_24h, &mut self.change_1w, &mut self.change_1y]; let rates = [&mut self.rate_24h, &mut self.rate_1w, &mut self.rate_1y]; @@ -170,8 +163,7 @@ where &mut rate_w.bps.height, max_from, starts, - &source_data, - offset, + source, exit, )?; } @@ -247,9 +239,6 @@ where source: &impl ReadableVec, exit: &Exit, ) -> Result<()> { - let skip = self.change_24h.cents.height.len(); - let (source_data, offset) = collect_source(source, skip, windows._1y); - let changes: [&mut FiatFromHeight; 4] = [ &mut self.change_24h, &mut self.change_1w, @@ -265,8 +254,7 @@ where &mut rate_w.bps.height, max_from, *starts, - &source_data, - offset, + source, exit, )?; }