Skip to content

Store tracked struct ids as ThinVec on Revisions #892

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

Merged
merged 7 commits into from
Jun 3, 2025
Merged
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
12 changes: 4 additions & 8 deletions src/function/diff_outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,12 @@ where
));
}

if old_outputs.is_empty() {
return;
}

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

for (ingredient_index, key_index) in old_outputs {
// SAFETY: key_index acquired from valid output
Expand Down
21 changes: 12 additions & 9 deletions src/tracked_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::ops::Index;
use std::{fmt, mem};

use crossbeam_queue::SegQueue;
use thin_vec::ThinVec;
use tracked_field::FieldIngredientImpl;

use crate::cycle::CycleHeads;
Expand Down Expand Up @@ -220,12 +221,18 @@ impl Clone for IdentityMap {
table: self.table.clone(),
}
}
fn clone_from(&mut self, source: &Self) {
self.table.clone_from(&source.table);
}
}

impl IdentityMap {
pub(crate) fn clone_from_slice(&mut self, source: &[(Identity, Id)]) {
self.table.clear();
self.table.reserve(source.len(), |(k, _)| k.hash);

for (key, id) in source {
self.insert(*key, *id);
}
}

pub(crate) fn insert(&mut self, key: Identity, id: Id) -> Option<Id> {
let entry = self.table.find_mut(key.hash, |&(k, _)| k == key);
match entry {
Expand All @@ -248,16 +255,12 @@ impl IdentityMap {
self.table.is_empty()
}

pub(crate) fn retain(&mut self, mut f: impl FnMut(&Identity, &mut Id) -> bool) {
self.table.retain(|(k, v)| f(k, v));
}

pub(crate) fn clear(&mut self) {
self.table.clear()
}

pub(crate) fn shrink_to_fit(&mut self) {
self.table.shrink_to_fit(|(k, _)| k.hash);
pub(crate) fn into_thin_vec(self) -> ThinVec<(Identity, Id)> {
self.table.into_iter().collect()
}
}

Expand Down
17 changes: 9 additions & 8 deletions src/zalsa_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::panic::UnwindSafe;
use std::ptr::{self, NonNull};

use rustc_hash::FxHashMap;
use thin_vec::ThinVec;
use tracing::debug;

use crate::accumulator::accumulated_map::{AccumulatedMap, AtomicInputAccumulatedValues};
Expand Down Expand Up @@ -371,7 +372,7 @@ impl QueryRevisionsExtra {
Some(Box::new(QueryRevisionsExtraInner {
accumulated,
cycle_heads,
tracked_struct_ids,
tracked_struct_ids: tracked_struct_ids.into_thin_vec(),
iteration,
}))
};
Expand Down Expand Up @@ -401,7 +402,7 @@ struct QueryRevisionsExtraInner {
/// 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.
tracked_struct_ids: IdentityMap,
tracked_struct_ids: ThinVec<(Identity, Id)>,

/// This result was computed based on provisional values from
/// these cycle heads. The "cycle head" is the query responsible
Expand All @@ -423,7 +424,7 @@ const _: [(); std::mem::size_of::<QueryRevisions>()] = [(); std::mem::size_of::<
#[cfg(not(feature = "shuttle"))]
#[cfg(target_pointer_width = "64")]
const _: [(); std::mem::size_of::<QueryRevisionsExtraInner>()] =
[(); std::mem::size_of::<[usize; 10]>()];
[(); std::mem::size_of::<[usize; 7]>()];

impl QueryRevisions {
pub(crate) fn fixpoint_initial(query: DatabaseKeyIndex) -> Self {
Expand Down Expand Up @@ -498,16 +499,16 @@ impl QueryRevisions {
}

/// Returns a reference to the `IdentityMap` for this query, or `None` if the map is empty.
pub fn tracked_struct_ids(&self) -> Option<&IdentityMap> {
pub fn tracked_struct_ids(&self) -> Option<&[(Identity, Id)]> {
self.extra
.0
.as_ref()
.map(|extra| &extra.tracked_struct_ids)
.map(|extra| &*extra.tracked_struct_ids)
.filter(|tracked_struct_ids| !tracked_struct_ids.is_empty())
}

/// Returns a mutable reference to the `IdentityMap` for this query, or `None` if the map is empty.
pub fn tracked_struct_ids_mut(&mut self) -> Option<&mut IdentityMap> {
pub fn tracked_struct_ids_mut(&mut self) -> Option<&mut ThinVec<(Identity, Id)>> {
self.extra
.0
.as_mut()
Expand Down Expand Up @@ -859,15 +860,15 @@ pub(crate) struct ActiveQueryGuard<'me> {

impl ActiveQueryGuard<'_> {
/// Initialize the tracked struct ids with the values from the prior execution.
pub(crate) fn seed_tracked_struct_ids(&self, tracked_struct_ids: &IdentityMap) {
pub(crate) fn seed_tracked_struct_ids(&self, tracked_struct_ids: &[(Identity, Id)]) {
self.local_state.with_query_stack_mut(|stack| {
#[cfg(debug_assertions)]
assert_eq!(stack.len(), self.push_len);
let frame = stack.last_mut().unwrap();
assert!(frame.tracked_struct_ids().is_empty());
frame
.tracked_struct_ids_mut()
.clone_from(tracked_struct_ids);
.clone_from_slice(tracked_struct_ids);
})
}

Expand Down