Skip to content

Commit ff3585b

Browse files
committed
Use thinvec
1 parent efae1f0 commit ff3585b

File tree

4 files changed

+21
-22
lines changed

4 files changed

+21
-22
lines changed

src/function.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,13 @@ where
191191
&'db self,
192192
zalsa: &'db Zalsa,
193193
id: Id,
194-
memo: memo::Memo<C::Output<'db>>,
194+
mut memo: memo::Memo<C::Output<'db>>,
195195
memo_ingredient_index: MemoIngredientIndex,
196196
) -> &'db memo::Memo<C::Output<'db>> {
197+
if let Some(tracked_struct_ids) = memo.revisions.tracked_struct_ids_mut() {
198+
tracked_struct_ids.shrink_to_fit();
199+
}
200+
197201
// We convert to a `NonNull` here as soon as possible because we are going to alias
198202
// into the `Box`, which is a `noalias` type.
199203
// FIXME: Use `Box::into_non_null` once stable

src/function/diff_outputs.rs

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

49-
if old_outputs.is_empty() {
49+
let Some(tracked_struct_ids) = revisions.tracked_struct_ids_mut() else {
5050
return;
51-
}
51+
};
5252

5353
// Remove the outputs that are no longer present in the current revision
5454
// to prevent that the next revision is seeded with an id mapping that no longer exists.
55-
revisions.retain_tracked_struct_ids(|(k, value)| {
56-
!old_outputs.contains(&(k.ingredient_index(), value.index()))
57-
});
55+
tracked_struct_ids
56+
.retain(|(k, value)| !old_outputs.contains(&(k.ingredient_index(), value.index())));
5857

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

src/tracked_struct.rs

Lines changed: 2 additions & 1 deletion
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;
@@ -258,7 +259,7 @@ impl IdentityMap {
258259
self.table.clear()
259260
}
260261

261-
pub(crate) fn into_boxed_slice(self) -> Box<[(Identity, Id)]> {
262+
pub(crate) fn into_thin_vec(self) -> ThinVec<(Identity, Id)> {
262263
self.table.into_iter().collect()
263264
}
264265
}

src/zalsa_local.rs

Lines changed: 10 additions & 15 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: tracked_struct_ids.into_boxed_slice(),
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: Box<[(Identity, Id)]>,
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; 8]>()];
427+
[(); std::mem::size_of::<[usize; 7]>()];
427428

428429
impl QueryRevisions {
429430
pub(crate) fn fixpoint_initial(query: DatabaseKeyIndex) -> Self {
@@ -507,18 +508,12 @@ impl QueryRevisions {
507508
}
508509

509510
/// Returns a mutable reference to the `IdentityMap` for this query, or `None` if the map is empty.
510-
pub fn retain_tracked_struct_ids<F>(&mut self, f: F)
511-
where
512-
F: FnMut(&(Identity, Id)) -> bool,
513-
{
514-
let Some(extra) = self.extra.0.as_mut() else {
515-
return;
516-
};
517-
518-
let tracked_struct_ids = std::mem::take(&mut extra.tracked_struct_ids);
519-
let mut tracked_struct_ids = tracked_struct_ids.into_vec();
520-
tracked_struct_ids.retain(f);
521-
extra.tracked_struct_ids = tracked_struct_ids.into_boxed_slice()
511+
pub fn tracked_struct_ids_mut(&mut self) -> Option<&mut ThinVec<(Identity, Id)>> {
512+
self.extra
513+
.0
514+
.as_mut()
515+
.map(|extra| &mut extra.tracked_struct_ids)
516+
.filter(|tracked_struct_ids| !tracked_struct_ids.is_empty())
522517
}
523518
}
524519

0 commit comments

Comments
 (0)