Skip to content

Commit 79c2eac

Browse files
authored
Store tracked struct ids as ThinVec on Revisions (#892)
* Store tracked struct ids as Vec on Revisions * Use reserve * Discard changes to tests/parallel/cycle_a_t1_b_t2_fallback.rs * Try boxed slice * Use thinvec * Ensure old-outputs are dropped if new revisions has no tracked struct ids * Discard changes to tests/parallel/cycle_a_t1_b_t2_fallback.rs
1 parent 14318b7 commit 79c2eac

File tree

3 files changed

+25
-25
lines changed

3 files changed

+25
-25
lines changed

src/function/diff_outputs.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,12 @@ where
4646
));
4747
}
4848

49-
if old_outputs.is_empty() {
50-
return;
51-
}
52-
49+
// Remove the outputs that are no longer present in the current revision
50+
// to prevent that the next revision is seeded with an id mapping that no longer exists.
5351
if let Some(tracked_struct_ids) = revisions.tracked_struct_ids_mut() {
54-
// Remove the outputs that are no longer present in the current revision
55-
// to prevent that the next revision is seeded with an id mapping that no longer exists.
5652
tracked_struct_ids
57-
.retain(|k, value| !old_outputs.contains(&(k.ingredient_index(), value.index())));
58-
}
53+
.retain(|(k, value)| !old_outputs.contains(&(k.ingredient_index(), value.index())));
54+
};
5955

6056
for (ingredient_index, key_index) in old_outputs {
6157
// SAFETY: key_index acquired from valid output

src/tracked_struct.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::ops::Index;
77
use std::{fmt, mem};
88

99
use crossbeam_queue::SegQueue;
10+
use thin_vec::ThinVec;
1011
use tracked_field::FieldIngredientImpl;
1112

1213
use crate::cycle::CycleHeads;
@@ -220,12 +221,18 @@ impl Clone for IdentityMap {
220221
table: self.table.clone(),
221222
}
222223
}
223-
fn clone_from(&mut self, source: &Self) {
224-
self.table.clone_from(&source.table);
225-
}
226224
}
227225

228226
impl IdentityMap {
227+
pub(crate) fn clone_from_slice(&mut self, source: &[(Identity, Id)]) {
228+
self.table.clear();
229+
self.table.reserve(source.len(), |(k, _)| k.hash);
230+
231+
for (key, id) in source {
232+
self.insert(*key, *id);
233+
}
234+
}
235+
229236
pub(crate) fn insert(&mut self, key: Identity, id: Id) -> Option<Id> {
230237
let entry = self.table.find_mut(key.hash, |&(k, _)| k == key);
231238
match entry {
@@ -248,16 +255,12 @@ impl IdentityMap {
248255
self.table.is_empty()
249256
}
250257

251-
pub(crate) fn retain(&mut self, mut f: impl FnMut(&Identity, &mut Id) -> bool) {
252-
self.table.retain(|(k, v)| f(k, v));
253-
}
254-
255258
pub(crate) fn clear(&mut self) {
256259
self.table.clear()
257260
}
258261

259-
pub(crate) fn shrink_to_fit(&mut self) {
260-
self.table.shrink_to_fit(|(k, _)| k.hash);
262+
pub(crate) fn into_thin_vec(self) -> ThinVec<(Identity, Id)> {
263+
self.table.into_iter().collect()
261264
}
262265
}
263266

src/zalsa_local.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::panic::UnwindSafe;
33
use std::ptr::{self, NonNull};
44

55
use rustc_hash::FxHashMap;
6+
use thin_vec::ThinVec;
67
use tracing::debug;
78

89
use crate::accumulator::accumulated_map::{AccumulatedMap, AtomicInputAccumulatedValues};
@@ -371,7 +372,7 @@ impl QueryRevisionsExtra {
371372
Some(Box::new(QueryRevisionsExtraInner {
372373
accumulated,
373374
cycle_heads,
374-
tracked_struct_ids,
375+
tracked_struct_ids: tracked_struct_ids.into_thin_vec(),
375376
iteration,
376377
}))
377378
};
@@ -401,7 +402,7 @@ struct QueryRevisionsExtraInner {
401402
/// previous revision. To handle this, `diff_outputs` compares
402403
/// the structs from the old/new revision and retains
403404
/// only entries that appeared in the new revision.
404-
tracked_struct_ids: IdentityMap,
405+
tracked_struct_ids: ThinVec<(Identity, Id)>,
405406

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

428429
impl QueryRevisions {
429430
pub(crate) fn fixpoint_initial(query: DatabaseKeyIndex) -> Self {
@@ -498,16 +499,16 @@ impl QueryRevisions {
498499
}
499500

500501
/// Returns a reference to the `IdentityMap` for this query, or `None` if the map is empty.
501-
pub fn tracked_struct_ids(&self) -> Option<&IdentityMap> {
502+
pub fn tracked_struct_ids(&self) -> Option<&[(Identity, Id)]> {
502503
self.extra
503504
.0
504505
.as_ref()
505-
.map(|extra| &extra.tracked_struct_ids)
506+
.map(|extra| &*extra.tracked_struct_ids)
506507
.filter(|tracked_struct_ids| !tracked_struct_ids.is_empty())
507508
}
508509

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

860861
impl ActiveQueryGuard<'_> {
861862
/// Initialize the tracked struct ids with the values from the prior execution.
862-
pub(crate) fn seed_tracked_struct_ids(&self, tracked_struct_ids: &IdentityMap) {
863+
pub(crate) fn seed_tracked_struct_ids(&self, tracked_struct_ids: &[(Identity, Id)]) {
863864
self.local_state.with_query_stack_mut(|stack| {
864865
#[cfg(debug_assertions)]
865866
assert_eq!(stack.len(), self.push_len);
866867
let frame = stack.last_mut().unwrap();
867868
assert!(frame.tracked_struct_ids().is_empty());
868869
frame
869870
.tracked_struct_ids_mut()
870-
.clone_from(tracked_struct_ids);
871+
.clone_from_slice(tracked_struct_ids);
871872
})
872873
}
873874

0 commit comments

Comments
 (0)