From 4cf465f419b247e859c085740fdd931428807574 Mon Sep 17 00:00:00 2001 From: nym21 Date: Tue, 15 Apr 2025 11:18:31 +0200 Subject: [PATCH] computer: unwrap --- Cargo.lock | 4 +- crates/brk_computer/src/storage/vecs/base.rs | 27 ++- .../brk_computer/src/storage/vecs/blocks.rs | 2 +- .../src/storage/vecs/grouped/builder.rs | 126 +++++----- .../brk_computer/src/storage/vecs/indexes.rs | 86 +++---- .../src/storage/vecs/marketprice.rs | 219 +++++------------- .../src/storage/vecs/transactions.rs | 58 ++--- crates/brk_indexer/src/vecs/base.rs | 8 +- crates/brk_parser/README.md | 8 +- crates/brk_vec/src/traits/dynamic.rs | 10 +- crates/brk_vec/src/traits/stored_index.rs | 8 +- 11 files changed, 215 insertions(+), 341 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7d446a987..279e430ff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1549,9 +1549,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "libc" -version = "0.2.171" +version = "0.2.172" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c19937216e9d3aa9956d9bb8dfc0b0c8beb6058fc4f7a4dc4d850edf86a237d6" +checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa" [[package]] name = "linux-raw-sys" diff --git a/crates/brk_computer/src/storage/vecs/base.rs b/crates/brk_computer/src/storage/vecs/base.rs index 41cf7bc2f..849956dc5 100644 --- a/crates/brk_computer/src/storage/vecs/base.rs +++ b/crates/brk_computer/src/storage/vecs/base.rs @@ -9,8 +9,7 @@ use std::{ use brk_core::CheckedSub; use brk_exit::Exit; use brk_vec::{ - Compressed, DynamicVec, Error, GenericVec, Result, StoredIndex, StoredType, StoredVec, Value, - Version, + Compressed, DynamicVec, Error, GenericVec, Result, StoredIndex, StoredType, StoredVec, Version, }; use log::info; @@ -120,8 +119,12 @@ where &mut self.inner } - pub fn cached_get(&mut self, index: I) -> Result>> { - self.inner.cached_get(index) + pub fn unwrap_cached_get(&mut self, index: I) -> Option { + self.inner.unwrap_cached_get(index) + } + #[inline] + pub fn double_unwrap_cached_get(&mut self, index: I) -> T { + self.inner.double_unwrap_cached_get(index) } pub fn collect_inclusive_range(&self, from: I, to: I) -> Result> { @@ -196,7 +199,7 @@ where .map_or_else(T::default, |v| v.into_inner()), ); other.iter_from(index, |(v, i, ..)| { - if self.cached_get(i).unwrap().is_none_or(|old_v| *old_v > v) { + if self.unwrap_cached_get(i).is_none_or(|old_v| old_v > v) { self.forced_push_at(i, v, exit) } else { Ok(()) @@ -224,7 +227,7 @@ where let index = max_from.min(T::from(self.len())); first_indexes.iter_from(index, |(value, first_index, ..)| { let first_index = (first_index).to_usize()?; - let last_index = (last_indexes.cached_get(value)?.unwrap()).to_usize()?; + let last_index = (last_indexes.double_unwrap_cached_get(value)).to_usize()?; (first_index..last_index) .try_for_each(|index| self.forced_push_at(I::from(index), value, exit)) })?; @@ -344,8 +347,8 @@ where let index = max_from.min(I::from(self.len())); first_indexes.iter_from(index, |(i, first_index, ..)| { - let last_index = last_indexes.cached_get(i)?.unwrap().into_inner(); - let range = first_index.to_usize().unwrap()..=last_index.to_usize().unwrap(); + let last_index = last_indexes.double_unwrap_cached_get(i); + let range = first_index.unwrap_to_usize()..=last_index.unwrap_to_usize(); let count = if let Some(filter) = filter.as_mut() { range.into_iter().filter(|i| filter(T2::from(*i))).count() } else { @@ -377,7 +380,7 @@ where self_to_other.iter_from(index, |(i, other, ..)| { self.forced_push_at( i, - T::from(other_to_self.cached_get(other)?.unwrap().into_inner() == i), + T::from(other_to_self.double_unwrap_cached_get(other) == i), exit, ) })?; @@ -403,11 +406,11 @@ where let index = max_from.min(I::from(self.len())); first_indexes.iter_from(index, |(i, first_index, ..)| { - let last_index = last_indexes.cached_get(i)?.unwrap().into_inner(); - let range = first_index.to_usize().unwrap()..=last_index.to_usize().unwrap(); + let last_index = last_indexes.double_unwrap_cached_get(i); + let range = first_index.unwrap_to_usize()..=last_index.unwrap_to_usize(); let mut sum = T::from(0_usize); range.into_iter().for_each(|i| { - sum = sum.clone() + source.cached_get_(i).unwrap().unwrap().into_inner(); + sum = sum.clone() + source.double_unwrap_cached_get(T2::from(i)); }); self.forced_push_at(i, sum, exit) })?; diff --git a/crates/brk_computer/src/storage/vecs/blocks.rs b/crates/brk_computer/src/storage/vecs/blocks.rs index 7a428a6b8..fa11cc578 100644 --- a/crates/brk_computer/src/storage/vecs/blocks.rs +++ b/crates/brk_computer/src/storage/vecs/blocks.rs @@ -97,7 +97,7 @@ impl Vecs { indexer.mut_vecs().height_to_timestamp.mut_vec(), |(height, timestamp, _, height_to_timestamp)| { let interval = height.decremented().map_or(Timestamp::ZERO, |prev_h| { - let prev_timestamp = *height_to_timestamp.cached_get(prev_h).unwrap().unwrap(); + let prev_timestamp = height_to_timestamp.double_unwrap_cached_get(prev_h); timestamp .checked_sub(prev_timestamp) .unwrap_or(Timestamp::ZERO) diff --git a/crates/brk_computer/src/storage/vecs/grouped/builder.rs b/crates/brk_computer/src/storage/vecs/grouped/builder.rs index ea3fbc7ec..a411dcb76 100644 --- a/crates/brk_computer/src/storage/vecs/grouped/builder.rs +++ b/crates/brk_computer/src/storage/vecs/grouped/builder.rs @@ -16,18 +16,18 @@ where I: StoredIndex, T: ComputedType, { - pub first: Option>, - pub average: Option>, - pub sum: Option>, - pub max: Option>, - pub _90p: Option>, - pub _75p: Option>, - pub median: Option>, - pub _25p: Option>, - pub _10p: Option>, - pub min: Option>, - pub last: Option>, - pub total: Option>, + first: Option>, + average: Option>, + sum: Option>, + max: Option>, + _90p: Option>, + _75p: Option>, + median: Option>, + _25p: Option>, + _10p: Option>, + min: Option>, + last: Option>, + total: Option>, } impl ComputedVecBuilder @@ -128,14 +128,12 @@ where source.iter_from(index, |(i, v, ..)| { let prev = i - .to_usize() - .unwrap() + .unwrap_to_usize() .checked_sub(1) .map_or(T::from(0_usize), |prev_i| { total_vec - .cached_get(I::from(prev_i)) - .unwrap() - .map_or(T::from(0_usize), |v| v.into_inner()) + .unwrap_cached_get(I::from(prev_i)) + .unwrap_or(T::from(0_usize)) }); let value = v.clone() + prev; total_vec.forced_push_at(i, value, exit)?; @@ -164,21 +162,15 @@ where let index = self.starting_index(max_from); first_indexes.iter_from(index, |(i, first_index, first_indexes)| { - let last_index = last_indexes.cached_get(i)?.unwrap().into_inner(); + let last_index = last_indexes.double_unwrap_cached_get(i); if let Some(first) = self.first.as_mut() { - let v = source.cached_get(first_index)?.unwrap().into_inner(); + let v = source.double_unwrap_cached_get(first_index); first.forced_push_at(index, v, exit)?; } if let Some(last) = self.last.as_mut() { - let v = source - .cached_get(last_index) - .inspect_err(|_| { - dbg!(last.path(), last_index); - })? - .unwrap() - .into_inner(); + let v = source.double_unwrap_cached_get(last_index); last.forced_push_at(index, v, exit)?; } @@ -269,17 +261,12 @@ where } if let Some(total_vec) = self.total.as_mut() { - let prev = i.to_usize().unwrap().checked_sub(1).map_or( - T::from(0_usize), - |prev_i| { - total_vec - .cached_get(I::from(prev_i)) - .unwrap() - .unwrap() - .to_owned() - .into_inner() - }, - ); + let prev = i + .unwrap_to_usize() + .checked_sub(1) + .map_or(T::from(0_usize), |prev_i| { + total_vec.double_unwrap_cached_get(I::from(prev_i)) + }); total_vec.forced_push_at(i, prev + sum, exit)?; } } @@ -320,17 +307,14 @@ where let index = self.starting_index(max_from); first_indexes.iter_from(index, |(i, first_index, ..)| { - let last_index = *last_indexes.cached_get(i).unwrap().unwrap(); + let last_index = last_indexes.double_unwrap_cached_get(i); if let Some(first) = self.first.as_mut() { let v = source .first .as_mut() .unwrap() - .cached_get(first_index) - .unwrap() - .unwrap() - .into_inner(); + .double_unwrap_cached_get(first_index); first.forced_push_at(index, v, exit)?; } @@ -339,10 +323,7 @@ where .last .as_mut() .unwrap() - .cached_get(last_index) - .unwrap() - .unwrap() - .into_inner(); + .double_unwrap_cached_get(last_index); last.forced_push_at(index, v, exit)?; } @@ -405,16 +386,12 @@ where } if let Some(total_vec) = self.total.as_mut() { - let prev = i.to_usize().unwrap().checked_sub(1).map_or( - T::from(0_usize), - |prev_i| { - total_vec - .cached_get(I::from(prev_i)) - .unwrap() - .unwrap() - .into_inner() - }, - ); + let prev = i + .unwrap_to_usize() + .checked_sub(1) + .map_or(T::from(0_usize), |prev_i| { + total_vec.double_unwrap_cached_get(I::from(prev_i)) + }); total_vec.forced_push_at(i, prev + sum, exit)?; } } @@ -457,6 +434,43 @@ where )) } + pub fn unwrap_first(&mut self) -> &mut ComputedVec { + self.first.as_mut().unwrap() + } + pub fn unwrap_average(&mut self) -> &mut ComputedVec { + self.average.as_mut().unwrap() + } + pub fn unwrap_sum(&mut self) -> &mut ComputedVec { + self.sum.as_mut().unwrap() + } + pub fn unwrap_max(&mut self) -> &mut ComputedVec { + self.max.as_mut().unwrap() + } + pub fn unwrap_90p(&mut self) -> &mut ComputedVec { + self._90p.as_mut().unwrap() + } + pub fn unwrap_75p(&mut self) -> &mut ComputedVec { + self._75p.as_mut().unwrap() + } + pub fn unwrap_median(&mut self) -> &mut ComputedVec { + self.median.as_mut().unwrap() + } + pub fn unwrap_25p(&mut self) -> &mut ComputedVec { + self._25p.as_mut().unwrap() + } + pub fn unwrap_10p(&mut self) -> &mut ComputedVec { + self._10p.as_mut().unwrap() + } + pub fn unwrap_min(&mut self) -> &mut ComputedVec { + self.min.as_mut().unwrap() + } + pub fn unwrap_last(&mut self) -> &mut ComputedVec { + self.last.as_mut().unwrap() + } + pub fn unwrap_total(&mut self) -> &mut ComputedVec { + self.total.as_mut().unwrap() + } + pub fn any_vecs(&self) -> Vec<&dyn brk_vec::AnyStoredVec> { let mut v: Vec<&dyn brk_vec::AnyStoredVec> = vec![]; diff --git a/crates/brk_computer/src/storage/vecs/indexes.rs b/crates/brk_computer/src/storage/vecs/indexes.rs index 1f420899b..e78074eee 100644 --- a/crates/brk_computer/src/storage/vecs/indexes.rs +++ b/crates/brk_computer/src/storage/vecs/indexes.rs @@ -343,12 +343,8 @@ impl Vecs { |(h, d, s, ..)| { let d = h .decremented() - .and_then(|h| s.cached_get(h).ok()) - .flatten() - .map_or(d, |prev_d| { - let prev_d = *prev_d; - if prev_d > d { prev_d } else { d } - }); + .and_then(|h| s.unwrap_cached_get(h)) + .map_or(d, |prev_d| prev_d.max(d)); (h, d) }, exit, @@ -365,8 +361,8 @@ impl Vecs { let starting_dateindex = self .height_to_dateindex - .cached_get(decremented_starting_height)? - .map_or_else(Default::default, |v| v.into_inner()); + .unwrap_cached_get(decremented_starting_height) + .unwrap_or_default(); self.height_to_dateindex.compute_transform( starting_indexes.height, @@ -377,8 +373,7 @@ impl Vecs { let starting_dateindex = if let Some(dateindex) = self .height_to_dateindex - .cached_get(decremented_starting_height)? - .map(|v| v.into_inner()) + .unwrap_cached_get(decremented_starting_height) { starting_dateindex.min(dateindex) } else { @@ -450,8 +445,8 @@ impl Vecs { let starting_weekindex = self .dateindex_to_weekindex - .cached_get(starting_dateindex)? - .map_or_else(Default::default, |v| v.into_inner()); + .unwrap_cached_get(starting_dateindex) + .unwrap_or_default(); self.dateindex_to_weekindex.compute_transform( starting_dateindex, @@ -485,12 +480,7 @@ impl Vecs { self.weekindex_to_timestamp.compute_transform( starting_weekindex, self.weekindex_to_first_dateindex.mut_vec(), - |(i, d, ..)| { - ( - i, - *self.dateindex_to_timestamp.cached_get(d).unwrap().unwrap(), - ) - }, + |(i, d, ..)| (i, self.dateindex_to_timestamp.double_unwrap_cached_get(d)), exit, )?; @@ -498,8 +488,8 @@ impl Vecs { let starting_monthindex = self .dateindex_to_monthindex - .cached_get(starting_dateindex)? - .map_or_else(Default::default, |v| v.into_inner()); + .unwrap_cached_get(starting_dateindex) + .unwrap_or_default(); self.dateindex_to_monthindex.compute_transform( starting_dateindex, @@ -535,12 +525,7 @@ impl Vecs { self.monthindex_to_timestamp.compute_transform( starting_monthindex, self.monthindex_to_first_dateindex.mut_vec(), - |(i, d, ..)| { - ( - i, - *self.dateindex_to_timestamp.cached_get(d).unwrap().unwrap(), - ) - }, + |(i, d, ..)| (i, self.dateindex_to_timestamp.double_unwrap_cached_get(d)), exit, )?; @@ -548,8 +533,8 @@ impl Vecs { let starting_quarterindex = self .monthindex_to_quarterindex - .cached_get(starting_monthindex)? - .map_or_else(Default::default, |v| v.into_inner()); + .unwrap_cached_get(starting_monthindex) + .unwrap_or_default(); self.monthindex_to_quarterindex.compute_transform( starting_monthindex, @@ -585,12 +570,7 @@ impl Vecs { self.quarterindex_to_timestamp.compute_transform( starting_quarterindex, self.quarterindex_to_first_monthindex.mut_vec(), - |(i, m, ..)| { - ( - i, - *self.monthindex_to_timestamp.cached_get(m).unwrap().unwrap(), - ) - }, + |(i, m, ..)| (i, self.monthindex_to_timestamp.double_unwrap_cached_get(m)), exit, )?; @@ -598,8 +578,8 @@ impl Vecs { let starting_yearindex = self .monthindex_to_yearindex - .cached_get(starting_monthindex)? - .map_or_else(Default::default, |v| v.into_inner()); + .unwrap_cached_get(starting_monthindex) + .unwrap_or_default(); self.monthindex_to_yearindex.compute_transform( starting_monthindex, @@ -635,12 +615,7 @@ impl Vecs { self.yearindex_to_timestamp.compute_transform( starting_yearindex, self.yearindex_to_first_monthindex.mut_vec(), - |(i, m, ..)| { - ( - i, - *self.monthindex_to_timestamp.cached_get(m).unwrap().unwrap(), - ) - }, + |(i, m, ..)| (i, self.monthindex_to_timestamp.double_unwrap_cached_get(m)), exit, )?; @@ -648,8 +623,8 @@ impl Vecs { let starting_decadeindex = self .yearindex_to_decadeindex - .cached_get(starting_yearindex)? - .map_or_else(Default::default, |v| v.into_inner()); + .unwrap_cached_get(starting_yearindex) + .unwrap_or_default(); self.yearindex_to_decadeindex.compute_transform( starting_yearindex, @@ -683,12 +658,7 @@ impl Vecs { self.decadeindex_to_timestamp.compute_transform( starting_decadeindex, self.decadeindex_to_first_yearindex.mut_vec(), - |(i, y, ..)| { - ( - i, - *self.yearindex_to_timestamp.cached_get(y).unwrap().unwrap(), - ) - }, + |(i, y, ..)| (i, self.yearindex_to_timestamp.double_unwrap_cached_get(y)), exit, )?; @@ -696,8 +666,8 @@ impl Vecs { let starting_difficultyepoch = self .height_to_difficultyepoch - .cached_get(decremented_starting_height)? - .map_or_else(Default::default, |v| v.into_inner()); + .unwrap_cached_get(decremented_starting_height) + .unwrap_or_default(); self.height_to_difficultyepoch.compute_transform( starting_indexes.height, @@ -734,11 +704,7 @@ impl Vecs { |(i, h, ..)| { ( i, - *indexer_vecs - .height_to_timestamp - .cached_get(h) - .unwrap() - .unwrap(), + indexer_vecs.height_to_timestamp.double_unwrap_cached_get(h), ) }, exit, @@ -748,8 +714,8 @@ impl Vecs { let starting_halvingepoch = self .height_to_halvingepoch - .cached_get(decremented_starting_height)? - .map_or_else(Default::default, |v| v.into_inner()); + .unwrap_cached_get(decremented_starting_height) + .unwrap_or_default(); self.height_to_halvingepoch.compute_transform( starting_indexes.height, @@ -786,7 +752,7 @@ impl Vecs { // |(i, h, ..)| { // ( // i, - // *indexer_vecs.height_to_timestamp.cached_get(h).unwrap().unwrap(), + // *indexer_vecs.height_to_timestamp.unwraped_cached_get(h).unwrap().unwrap(), // ) // }, // exit, diff --git a/crates/brk_computer/src/storage/vecs/marketprice.rs b/crates/brk_computer/src/storage/vecs/marketprice.rs index bfa85346f..341cd99de 100644 --- a/crates/brk_computer/src/storage/vecs/marketprice.rs +++ b/crates/brk_computer/src/storage/vecs/marketprice.rs @@ -242,9 +242,8 @@ impl Vecs { .get_height( h, t, - h.decremented().map(|prev_h| { - *height_to_timestamp.cached_get(prev_h).unwrap().unwrap() - }), + h.decremented() + .map(|prev_h| height_to_timestamp.double_unwrap_cached_get(prev_h)), ) .unwrap(); (h, ohlc) @@ -454,43 +453,26 @@ impl Vecs { self.weekindex_to_ohlc.compute_transform( starting_indexes.weekindex, - self.timeindexes_to_close - .weekindex - .last - .as_mut() - .unwrap() - .mut_vec(), + self.timeindexes_to_close.weekindex.unwrap_last().mut_vec(), |(i, close, ..)| { ( i, OHLCDollars { - open: *self + open: self .timeindexes_to_open .weekindex - .first - .as_mut() - .unwrap() - .cached_get(i) - .unwrap() - .unwrap(), - high: *self + .unwrap_first() + .double_unwrap_cached_get(i), + high: self .timeindexes_to_high .weekindex - .max - .as_mut() - .unwrap() - .cached_get(i) - .unwrap() - .unwrap(), - low: *self + .unwrap_max() + .double_unwrap_cached_get(i), + low: self .timeindexes_to_low .weekindex - .min - .as_mut() - .unwrap() - .cached_get(i) - .unwrap() - .unwrap(), + .unwrap_min() + .double_unwrap_cached_get(i), close, }, ) @@ -502,41 +484,27 @@ impl Vecs { starting_indexes.difficultyepoch, self.chainindexes_to_close .difficultyepoch - .last - .as_mut() - .unwrap() + .unwrap_last() .mut_vec(), |(i, close, ..)| { ( i, OHLCDollars { - open: *self + open: self .chainindexes_to_open .difficultyepoch - .first - .as_mut() - .unwrap() - .cached_get(i) - .unwrap() - .unwrap(), - high: *self + .unwrap_first() + .double_unwrap_cached_get(i), + high: self .chainindexes_to_high .difficultyepoch - .max - .as_mut() - .unwrap() - .cached_get(i) - .unwrap() - .unwrap(), - low: *self + .unwrap_max() + .double_unwrap_cached_get(i), + low: self .chainindexes_to_low .difficultyepoch - .min - .as_mut() - .unwrap() - .cached_get(i) - .unwrap() - .unwrap(), + .unwrap_min() + .double_unwrap_cached_get(i), close, }, ) @@ -546,43 +514,26 @@ impl Vecs { self.monthindex_to_ohlc.compute_transform( starting_indexes.monthindex, - self.timeindexes_to_close - .monthindex - .last - .as_mut() - .unwrap() - .mut_vec(), + self.timeindexes_to_close.monthindex.unwrap_last().mut_vec(), |(i, close, ..)| { ( i, OHLCDollars { - open: *self + open: self .timeindexes_to_open .monthindex - .first - .as_mut() - .unwrap() - .cached_get(i) - .unwrap() - .unwrap(), - high: *self + .unwrap_first() + .double_unwrap_cached_get(i), + high: self .timeindexes_to_high .monthindex - .max - .as_mut() - .unwrap() - .cached_get(i) - .unwrap() - .unwrap(), - low: *self + .unwrap_max() + .double_unwrap_cached_get(i), + low: self .timeindexes_to_low .monthindex - .min - .as_mut() - .unwrap() - .cached_get(i) - .unwrap() - .unwrap(), + .unwrap_min() + .double_unwrap_cached_get(i), close, }, ) @@ -594,41 +545,27 @@ impl Vecs { starting_indexes.quarterindex, self.timeindexes_to_close .quarterindex - .last - .as_mut() - .unwrap() + .unwrap_last() .mut_vec(), |(i, close, ..)| { ( i, OHLCDollars { - open: *self + open: self .timeindexes_to_open .quarterindex - .first - .as_mut() - .unwrap() - .cached_get(i) - .unwrap() - .unwrap(), - high: *self + .unwrap_first() + .double_unwrap_cached_get(i), + high: self .timeindexes_to_high .quarterindex - .max - .as_mut() - .unwrap() - .cached_get(i) - .unwrap() - .unwrap(), - low: *self + .unwrap_max() + .double_unwrap_cached_get(i), + low: self .timeindexes_to_low .quarterindex - .min - .as_mut() - .unwrap() - .cached_get(i) - .unwrap() - .unwrap(), + .unwrap_min() + .double_unwrap_cached_get(i), close, }, ) @@ -638,43 +575,26 @@ impl Vecs { self.yearindex_to_ohlc.compute_transform( starting_indexes.yearindex, - self.timeindexes_to_close - .yearindex - .last - .as_mut() - .unwrap() - .mut_vec(), + self.timeindexes_to_close.yearindex.unwrap_last().mut_vec(), |(i, close, ..)| { ( i, OHLCDollars { - open: *self + open: self .timeindexes_to_open .yearindex - .first - .as_mut() - .unwrap() - .cached_get(i) - .unwrap() - .unwrap(), - high: *self + .unwrap_first() + .double_unwrap_cached_get(i), + high: self .timeindexes_to_high .yearindex - .max - .as_mut() - .unwrap() - .cached_get(i) - .unwrap() - .unwrap(), - low: *self + .unwrap_max() + .double_unwrap_cached_get(i), + low: self .timeindexes_to_low .yearindex - .min - .as_mut() - .unwrap() - .cached_get(i) - .unwrap() - .unwrap(), + .unwrap_min() + .double_unwrap_cached_get(i), close, }, ) @@ -689,42 +609,27 @@ impl Vecs { starting_indexes.decadeindex, self.timeindexes_to_close .decadeindex - .last - .as_mut() - .as_mut() - .unwrap() + .unwrap_last() .mut_vec(), |(i, close, ..)| { ( i, OHLCDollars { - open: *self + open: self .timeindexes_to_open .decadeindex - .first - .as_mut() - .unwrap() - .cached_get(i) - .unwrap() - .unwrap(), - high: *self + .unwrap_first() + .double_unwrap_cached_get(i), + high: self .timeindexes_to_high .decadeindex - .max - .as_mut() - .unwrap() - .cached_get(i) - .unwrap() - .unwrap(), - low: *self + .unwrap_max() + .double_unwrap_cached_get(i), + low: self .timeindexes_to_low .decadeindex - .min - .as_mut() - .unwrap() - .cached_get(i) - .unwrap() - .unwrap(), + .unwrap_min() + .double_unwrap_cached_get(i), close, }, ) diff --git a/crates/brk_computer/src/storage/vecs/transactions.rs b/crates/brk_computer/src/storage/vecs/transactions.rs index 1150cb23e..927f3ad06 100644 --- a/crates/brk_computer/src/storage/vecs/transactions.rs +++ b/crates/brk_computer/src/storage/vecs/transactions.rs @@ -295,10 +295,7 @@ impl Vecs { |txindex| { let v = indexer_vecs .txindex_to_txversion - .cached_get(txindex) - .unwrap() - .unwrap() - .into_inner(); + .double_unwrap_cached_get(txindex); v == txversion }, exit, @@ -326,10 +323,7 @@ impl Vecs { let total_size = indexer_vecs .txindex_to_total_size .mut_vec() - .cached_get(txindex) - .unwrap() - .unwrap() - .into_inner(); + .double_unwrap_cached_get(txindex); // This is the exact definition of a weight unit, as defined by BIP-141 (quote above). let wu = base_size * 3 + total_size; @@ -357,12 +351,12 @@ impl Vecs { |(txinindex, txoutindex, slf, other)| { let value = if txoutindex == Txoutindex::COINBASE { Sats::ZERO - } else if let Ok(Some(value)) = indexer_vecs + } else if let Some(value) = indexer_vecs .txoutindex_to_value .mut_vec() - .cached_get(txoutindex) + .unwrap_cached_get(txoutindex) { - *value + value } else { dbg!(txinindex, txoutindex, slf.len(), other.len()); panic!() @@ -429,11 +423,8 @@ impl Vecs { if input_value.is_zero() { (txindex, input_value) } else { - let output_value = txindex_to_output_value - .cached_get(txindex) - .unwrap() - .unwrap() - .into_inner(); + let output_value = + txindex_to_output_value.double_unwrap_cached_get(txindex); (txindex, input_value.checked_sub(output_value).unwrap()) } }, @@ -455,10 +446,7 @@ impl Vecs { let vsize = self .txindex_to_vsize .mut_vec() - .cached_get(txindex) - .unwrap() - .unwrap() - .into_inner(); + .double_unwrap_cached_get(txindex); (txindex, Feerate::from((fee, vsize))) }, @@ -496,29 +484,18 @@ impl Vecs { |(height, txindex, ..)| { let first_txoutindex = indexer_vecs .txindex_to_first_txoutindex - .cached_get(txindex) - .unwrap() - .unwrap() - .into_inner() - .to_usize() - .unwrap(); + .double_unwrap_cached_get(txindex) + .unwrap_to_usize(); let last_txoutindex = indexes .txindex_to_last_txoutindex .mut_vec() - .cached_get(txindex) - .unwrap() - .unwrap() - .into_inner() - .to_usize() - .unwrap(); + .double_unwrap_cached_get(txindex) + .unwrap_to_usize(); let mut sats = Sats::ZERO; (first_txoutindex..=last_txoutindex).for_each(|txoutindex| { sats += indexer_vecs .txoutindex_to_value - .cached_get_(txoutindex) - .unwrap() - .unwrap() - .into_inner(); + .double_unwrap_cached_get(Txoutindex::from(txoutindex)); }); (height, sats) }, @@ -540,14 +517,9 @@ impl Vecs { let fees = self .indexes_to_fee .height - .sum - .as_mut() - .unwrap() + .unwrap_sum() .mut_vec() - .cached_get(height) - .unwrap() - .unwrap() - .into_inner(); + .double_unwrap_cached_get(height); (height, subsidy.checked_sub(fees).unwrap()) }, exit, diff --git a/crates/brk_indexer/src/vecs/base.rs b/crates/brk_indexer/src/vecs/base.rs index c2fb62e58..6106ac10a 100644 --- a/crates/brk_indexer/src/vecs/base.rs +++ b/crates/brk_indexer/src/vecs/base.rs @@ -46,12 +46,12 @@ where self.inner.get(index) } #[inline] - pub fn cached_get(&mut self, index: I) -> Result>> { - self.inner.cached_get(index) + pub fn unwrap_cached_get(&mut self, index: I) -> Option { + self.inner.unwrap_cached_get(index) } #[inline] - pub fn cached_get_(&mut self, index: usize) -> Result>> { - self.inner.cached_get_(index) + pub fn double_unwrap_cached_get(&mut self, index: I) -> T { + self.inner.double_unwrap_cached_get(index) } pub fn iter_from(&mut self, index: I, f: F) -> Result<()> diff --git a/crates/brk_parser/README.md b/crates/brk_parser/README.md index 2f57c80a4..cdef6be44 100644 --- a/crates/brk_parser/README.md +++ b/crates/brk_parser/README.md @@ -41,16 +41,16 @@ The element returned by the iterator is a tuple which includes the: - Block: `Block` (from `bitcoin-rust`) - Block's Hash: `BlockHash` (also from `bitcoin-rust`) -## Example - -`src/main.rs` - ## Requirements Even though it reads *blkXXXXX.dat* files, it **needs** `bitcoind` to run with the RPC server to filter out block forks. Peak memory should be around 500MB. +## Disclaimer + +A state is saved in `{bitcoindir}/blocks/blk_index_to_blk_recap.json` to allow for faster starts (see benchmark below) but doesn't yet support locking. Thus it is recommended to run one instance of `brk_parser` at a time. + ## Comparaison | | [brk_parser](https://crates.io/crates/brk_parser) | [bitcoin-explorer (deprecated)](https://crates.io/crates/bitcoin-explorer) | [blocks_iterator](https://crates.io/crates/blocks_iterator) | diff --git a/crates/brk_vec/src/traits/dynamic.rs b/crates/brk_vec/src/traits/dynamic.rs index 0f35e4de3..d18282c54 100644 --- a/crates/brk_vec/src/traits/dynamic.rs +++ b/crates/brk_vec/src/traits/dynamic.rs @@ -17,7 +17,15 @@ pub trait DynamicVec: Send + Sync { } #[inline] fn cached_get(&mut self, index: Self::I) -> Result>> { - self.get_(index.to_usize()?) + self.cached_get_(index.to_usize()?) + } + #[inline] + fn unwrap_cached_get(&mut self, index: Self::I) -> Option { + self.cached_get(index).unwrap().map(Value::into_inner) + } + #[inline] + fn double_unwrap_cached_get(&mut self, index: Self::I) -> Self::T { + self.unwrap_cached_get(index).unwrap() } #[inline] fn get_(&self, index: usize) -> Result>> { diff --git a/crates/brk_vec/src/traits/stored_index.rs b/crates/brk_vec/src/traits/stored_index.rs index a083b452f..a01b52d79 100644 --- a/crates/brk_vec/src/traits/stored_index.rs +++ b/crates/brk_vec/src/traits/stored_index.rs @@ -18,6 +18,7 @@ where + Send + Sync, { + fn unwrap_to_usize(self) -> usize; fn to_usize(self) -> Result; fn to_string<'a>() -> &'a str; } @@ -37,7 +38,12 @@ where + Send + Sync, { - #[inline(always)] + #[inline] + fn unwrap_to_usize(self) -> usize { + self.to_usize().unwrap() + } + + #[inline] fn to_usize(self) -> Result { self.try_into().map_err(|_| Error::FailedKeyTryIntoUsize) }