Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

graph: Memoize Batch.indirect_weight #5276

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions graph/src/components/store/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,12 @@ pub struct Batch {
pub offchain_to_remove: DataSources,
pub error: Option<StoreError>,
pub is_non_fatal_errors_active: bool,
/// Memoize the indirect weight of the batch. We need the `CacheWeight`
/// of the batch a lot in the write queue to determine if a batch should
/// be written. Recalculating it every time, which has to happen while
/// the writer holds a lock, conflicts with appending to the batch and
/// causes batches to be finished prematurely.
indirect_weight: usize,
}

impl Batch {
Expand Down Expand Up @@ -670,7 +676,7 @@ impl Batch {
let offchain_to_remove = DataSources::new(block_ptr.cheap_clone(), offchain_to_remove);
let first_block = block_ptr.number;
let block_times = vec![(block, block_time)];
Ok(Self {
let mut batch = Self {
block_ptr,
first_block,
block_times,
Expand All @@ -681,7 +687,10 @@ impl Batch {
offchain_to_remove,
error: None,
is_non_fatal_errors_active,
})
indirect_weight: 0,
};
batch.weigh();
Ok(batch)
}

fn append_inner(&mut self, mut batch: Batch) -> Result<(), StoreError> {
Expand Down Expand Up @@ -712,6 +721,7 @@ impl Batch {
if let Err(e) = &res {
self.error = Some(e.clone());
}
self.weigh();
res
}

Expand Down Expand Up @@ -772,11 +782,15 @@ impl Batch {
pub fn groups<'a>(&'a self) -> impl Iterator<Item = &'a RowGroup> {
self.mods.groups.iter()
}

fn weigh(&mut self) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got an english lesson with this one.

self.indirect_weight = self.mods.indirect_weight();
}
}

impl CacheWeight for Batch {
fn indirect_weight(&self) -> usize {
self.mods.indirect_weight()
self.indirect_weight
}
}

Expand Down
Loading