Skip to content

refactor: Shrink QueryRevisions by 3 usize by boxing IdentityMap #768

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion src/active_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ impl ActiveQuery {
.is_empty()
.not()
.then(|| Box::new(mem::take(accumulated)));
let tracked_struct_ids = mem::take(tracked_struct_ids);
let tracked_struct_ids = tracked_struct_ids
.is_empty()
.not()
.then(|| Box::new(mem::take(tracked_struct_ids)));
let accumulated_inputs = AtomicInputAccumulatedValues::new(accumulated_inputs);
let cycle_heads = mem::take(cycle_heads);
QueryRevisions {
Expand Down
18 changes: 11 additions & 7 deletions src/function/diff_outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,23 @@ where
) {
// Iterate over the outputs of the `old_memo` and put them into a hashset
let mut old_outputs: FxHashSet<_> = old_memo.revisions.origin.outputs().collect();

// Iterate over the outputs of the current query
// and remove elements from `old_outputs` when we find them
for new_output in revisions.origin.outputs() {
old_outputs.remove(&new_output);
}

if !old_outputs.is_empty() {
// Remove the outputs that are no longer present in the current revision
// to prevent that the next revision is seeded with a id mapping that no longer exists.
revisions.tracked_struct_ids.retain(|&k, &mut value| {
!old_outputs.contains(&DatabaseKeyIndex::new(k.ingredient_index(), value))
});
if let Some(tracked_struct_ids) = &mut revisions.tracked_struct_ids {
if !old_outputs.is_empty() {
// Remove the outputs that are no longer present in the current revision
// to prevent that the next revision is seeded with a id mapping that no longer exists.
tracked_struct_ids.retain(|&k, &mut value| {
!old_outputs.contains(&DatabaseKeyIndex::new(k.ingredient_index(), value))
});
}
if tracked_struct_ids.is_empty() {
revisions.tracked_struct_ids = None;
}
}

for old_output in old_outputs {
Expand Down
14 changes: 11 additions & 3 deletions src/function/execute.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
cycle::{CycleRecoveryStrategy, MAX_ITERATIONS},
zalsa::ZalsaDatabase,
zalsa_local::ActiveQueryGuard,
zalsa_local::{ActiveQueryGuard, QueryRevisions},
Database, Event, EventKind,
};

Expand Down Expand Up @@ -52,8 +52,16 @@ where
loop {
// If we already executed this query once, then use the tracked-struct ids from the
// previous execution as the starting point for the new one.
if let Some(old_memo) = opt_old_memo {
active_query.seed_tracked_struct_ids(&old_memo.revisions.tracked_struct_ids);
if let Some(Memo {
revisions:
QueryRevisions {
tracked_struct_ids: Some(tracked_struct_ids),
..
},
..
}) = opt_old_memo
{
active_query.seed_tracked_struct_ids(tracked_struct_ids);
}

// Query was not previously executed, or value is potentially
Expand Down
2 changes: 1 addition & 1 deletion src/function/memo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub(super) struct Memo<V> {
// Memo's are stored a lot, make sure their size is doesn't randomly increase.
// #[cfg(test)]
const _: [(); std::mem::size_of::<Memo<std::num::NonZeroUsize>>()] =
[(); std::mem::size_of::<[usize; 13]>()];
[(); std::mem::size_of::<[usize; 10]>()];

impl<V> Memo<V> {
pub(super) fn new(value: Option<V>, revision_now: Revision, revisions: QueryRevisions) -> Self {
Expand Down
6 changes: 5 additions & 1 deletion src/zalsa_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,11 @@ pub(crate) struct QueryRevisions {
/// previous revision. To handle this, `diff_outputs` compares
/// the structs from the old/new revision and retains
/// only entries that appeared in the new revision.
pub(super) tracked_struct_ids: IdentityMap,
///
/// Since not all queries produce a tracked struct, wrapping
/// `IdentityMap` in an `Option<Box<T>>` reduces the size of
/// `QueryRevisions` by 3 words (24 bytes on a 64-bit platform).
pub(super) tracked_struct_ids: Option<Box<IdentityMap>>,

pub(super) accumulated: Option<Box<AccumulatedMap>>,

Expand Down