Skip to content

Commit

Permalink
Lazy load origin children
Browse files Browse the repository at this point in the history
  • Loading branch information
someone235 committed Aug 15, 2024
1 parent 5ebd9fe commit eeeb8e6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
9 changes: 9 additions & 0 deletions consensus/src/model/services/reachability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub trait ReachabilityService {
fn is_any_dag_ancestor_result(&self, list: &mut impl Iterator<Item = Hash>, queried: Hash) -> Result<bool>;
fn get_next_chain_ancestor(&self, descendant: Hash, ancestor: Hash) -> Hash;
fn get_chain_parent(&self, this: Hash) -> Hash;
fn has_reachability_data(&self, this: Hash) -> bool;
}

impl<T: ReachabilityStoreReader + ?Sized> ReachabilityService for T {
Expand Down Expand Up @@ -56,6 +57,10 @@ impl<T: ReachabilityStoreReader + ?Sized> ReachabilityService for T {
fn get_chain_parent(&self, this: Hash) -> Hash {
self.get_parent(this).unwrap()
}

fn has_reachability_data(&self, this: Hash) -> bool {
self.has(this).unwrap()
}
}

/// Multi-threaded reachability service imp
Expand Down Expand Up @@ -108,6 +113,10 @@ impl<T: ReachabilityStoreReader + ?Sized> ReachabilityService for MTReachability
fn get_chain_parent(&self, this: Hash) -> Hash {
self.store.read().get_parent(this).unwrap()
}

fn has_reachability_data(&self, this: Hash) -> bool {
self.store.read().has(this).unwrap()
}
}

impl<T: ReachabilityStoreReader + ?Sized> MTReachabilityService<T> {
Expand Down
24 changes: 10 additions & 14 deletions consensus/src/processes/parents_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ use crate::model::{
stores::{headers::HeaderStoreReader, reachability::ReachabilityStoreReader, relations::RelationsStoreReader},
};

use super::reachability::ReachabilityResultExtensions;

#[derive(Clone)]
pub struct ParentsManager<T: HeaderStoreReader, U: ReachabilityStoreReader, V: RelationsStoreReader> {
max_block_level: BlockLevel,
Expand Down Expand Up @@ -52,10 +50,7 @@ impl<T: HeaderStoreReader, U: ReachabilityStoreReader, V: RelationsStoreReader>
.expect("at least one of the parents is expected to be in the future of the pruning point");
direct_parent_headers.swap(0, first_parent_in_future_of_pruning_point);

let origin_children = self.relations_service.get_children(ORIGIN).unwrap().read().iter().copied().collect_vec();
let origin_children_headers =
origin_children.iter().copied().map(|parent| self.headers_store.get_header(parent).unwrap()).collect_vec();

let mut origin_children_headers = None;
let mut parents = Vec::with_capacity(self.max_block_level as usize);

for block_level in 0..self.max_block_level {
Expand Down Expand Up @@ -97,11 +92,7 @@ impl<T: HeaderStoreReader, U: ReachabilityStoreReader, V: RelationsStoreReader>
};

for (i, parent) in grandparents.into_iter().enumerate() {
let is_in_origin_children_future = self
.reachability_service
.is_any_dag_ancestor_result(&mut origin_children.iter().copied(), parent)
.unwrap_option()
.is_some_and(|r| r);
let has_reachability_data = self.reachability_service.has_reachability_data(parent);

// Reference blocks are the blocks that are used in reachability queries to check if
// a candidate is in the future of another candidate. In most cases this is just the
Expand All @@ -113,10 +104,15 @@ impl<T: HeaderStoreReader, U: ReachabilityStoreReader, V: RelationsStoreReader>
// the virtual genesis children in the pruning point anticone. So we can check which
// virtual genesis children have this block as parent and use those block as
// reference blocks.
let reference_blocks = if is_in_origin_children_future {
let reference_blocks = if has_reachability_data {
smallvec![parent]
} else {
let mut reference_blocks = SmallVec::with_capacity(origin_children.len());
let origin_children_headers = origin_children_headers.get_or_insert_with(|| {
let origin_children =
self.relations_service.get_children(ORIGIN).unwrap().read().iter().copied().collect_vec();
origin_children.iter().copied().map(|parent| self.headers_store.get_header(parent).unwrap()).collect_vec()
});
let mut reference_blocks = SmallVec::with_capacity(origin_children_headers.len());
for child_header in origin_children_headers.iter() {
if self.parents_at_level(child_header, block_level).contains(&parent) {
reference_blocks.push(child_header.hash);
Expand All @@ -133,7 +129,7 @@ impl<T: HeaderStoreReader, U: ReachabilityStoreReader, V: RelationsStoreReader>
continue;
}

if !is_in_origin_children_future {
if !has_reachability_data {
continue;
}

Expand Down

0 comments on commit eeeb8e6

Please sign in to comment.