1use std::any::TypeId;
2use std::borrow::Cow;
3use std::cell::UnsafeCell;
4use std::fmt;
5use std::hash::{BuildHasher, Hash};
6use std::marker::PhantomData;
7use std::num::NonZeroUsize;
8use std::path::{Path, PathBuf};
9use std::ptr::NonNull;
10
11use crossbeam_utils::CachePadded;
12use intrusive_collections::{LinkedList, LinkedListLink, UnsafeRef, intrusive_adapter};
13use rustc_hash::FxBuildHasher;
14use smallvec::SmallVec;
15
16use crate::durability::{AtomicDurability, Durability};
17use crate::function::VerifyResult;
18use crate::hash::{FxHashSet, FxIndexSet};
19use crate::id::{AsId, FromId};
20use crate::ingredient::Ingredient;
21use crate::plumbing::{self, Jar, ZalsaLocal};
22use crate::revision::AtomicRevision;
23use crate::sync::{Arc, Mutex, max_parallelism};
24use crate::table::Slot;
25use crate::table::memo::{MemoTable, MemoTableTypes, MemoTableWithTypesMut};
26use crate::zalsa::{IngredientIndex, JarKind, Zalsa};
27use crate::zalsa_local::QueryEdge;
28use crate::{DatabaseKeyIndex, Event, EventKind, Id, Revision};
29
30#[cfg(not(test))]
31const DEFAULT_REVISIONS: usize = 3;
32
33#[cfg(test)]
35const DEFAULT_REVISIONS: usize = 1;
36
37pub unsafe trait Configuration: Sized + 'static {
48 const LOCATION: crate::ingredient::Location;
49 const DEBUG_NAME: &'static str;
50
51 const PERSIST: bool;
53
54 const REVISIONS: NonZeroUsize = NonZeroUsize::new(DEFAULT_REVISIONS).unwrap();
56
57 type Fields<'db>: InternedData;
59
60 type Struct<'db>: Copy + FromId + AsId;
62
63 fn heap_size(_value: &Self::Fields<'_>) -> Option<usize> {
65 None
66 }
67
68 fn serialize<S>(value: &Self::Fields<'_>, serializer: S) -> Result<S::Ok, S::Error>
72 where
73 S: plumbing::serde::Serializer;
74
75 fn deserialize<'de, D>(deserializer: D) -> Result<Self::Fields<'static>, D::Error>
79 where
80 D: plumbing::serde::Deserializer<'de>;
81}
82
83pub trait InternedData: Sized + Eq + Hash + Clone + Sync + Send {}
84impl<T: Eq + Hash + Clone + Sync + Send> InternedData for T {}
85
86pub struct JarImpl<C: Configuration> {
87 phantom: PhantomData<C>,
88}
89
90pub struct IngredientImpl<C: Configuration> {
95 ingredient_index: IngredientIndex,
97
98 hasher: FxBuildHasher,
100
101 shift: u32,
103
104 shards: Box<[CachePadded<Mutex<IngredientShard>>]>,
106
107 revision_queue: RevisionQueue,
109
110 memo_table_types: Arc<MemoTableTypes>,
111
112 _marker: PhantomData<fn() -> C>,
113}
114
115struct IngredientShard {
116 key_map: hashbrown::HashTable<ValueKey>,
121
122 lru: LinkedList<LruEntryAdapter>,
124}
125
126impl Default for IngredientShard {
127 fn default() -> Self {
128 Self {
129 lru: LinkedList::default(),
130 key_map: hashbrown::HashTable::new(),
131 }
132 }
133}
134
135unsafe impl Send for IngredientShard {}
139
140unsafe impl<C: Configuration> Sync for Value<C> {}
147
148intrusive_adapter!(LruEntryAdapter = UnsafeRef<LruEntry>: LruEntry { link => LinkedListLink });
149
150pub struct Value<C>
152where
153 C: Configuration,
154{
155 shard: u16,
159
160 lru: LruEntry,
162
163 fields: UnsafeCell<C::Fields<'static>>,
168
169 memos: UnsafeCell<MemoTable>,
174
175 durability: AtomicDurability,
184}
185
186struct LruEntry {
188 link: LinkedListLink,
190
191 id: UnsafeCell<Id>,
206
207 last_interned_at: AtomicRevision,
210}
211
212impl LruEntry {
213 #[inline]
215 fn ptr_from_value<C: Configuration>(value: &Value<C>) -> *const Self {
216 let value = std::ptr::from_ref(value).cast::<u8>();
217
218 unsafe { value.add(std::mem::offset_of!(Value<C>, lru)).cast() }
222 }
223
224 #[inline]
231 unsafe fn value_from_ptr<'a, C: Configuration>(entry: *const Self) -> &'a Value<C> {
232 unsafe {
236 &*entry
237 .cast::<u8>()
238 .sub(std::mem::offset_of!(Value<C>, lru))
239 .cast::<Value<C>>()
240 }
241 }
242}
243
244fn is_reusable<C: Configuration>(durability: Durability) -> bool {
249 if C::REVISIONS == IMMORTAL {
251 return false;
252 }
253
254 durability == Durability::LOW
261}
262
263fn report_tracked_read_if_reusable<C: Configuration>(
265 zalsa_local: &ZalsaLocal,
266 index: DatabaseKeyIndex,
267 current_revision: Revision,
268 durability: Durability,
269) {
270 if is_reusable::<C>(durability) {
271 zalsa_local.report_tracked_read_simple(index, durability, current_revision);
272 } else {
273 zalsa_local.report_tracked_read_revision(current_revision);
277 }
278}
279
280struct ReusableSlot {
281 entry: *const LruEntry,
282 old_id: Id,
283 new_id: Id,
284}
285
286impl<C> Value<C>
287where
288 C: Configuration,
289{
290 #[cfg(feature = "salsa_unstable")]
292 pub fn fields<'db>(&'db self) -> &'db C::Fields<'db> {
293 unsafe { std::mem::transmute::<&C::Fields<'static>, &C::Fields<'db>>(&*self.fields.get()) }
299 }
300
301 #[cfg(all(not(feature = "shuttle"), feature = "salsa_unstable"))]
308 unsafe fn memory_usage(&self, memo_table_types: &MemoTableTypes) -> crate::database::SlotInfo {
309 let heap_size = C::heap_size(self.fields());
310 let memos = unsafe { &*self.memos.get() };
313 let memos = unsafe { memo_table_types.attach_memos(memos) };
315
316 crate::database::SlotInfo {
317 debug_name: C::DEBUG_NAME,
318 size_of_metadata: std::mem::size_of::<Self>()
319 - std::mem::size_of::<C::Fields<'static>>(),
320 size_of_fields: std::mem::size_of::<C::Fields<'static>>(),
321 heap_size_of_fields: heap_size,
322 memos: unsafe { memos.memory_usage() },
324 }
325 }
326}
327
328impl<C: Configuration> Default for JarImpl<C> {
329 fn default() -> Self {
330 Self {
331 phantom: PhantomData,
332 }
333 }
334}
335
336impl<C: Configuration> Jar for JarImpl<C> {
337 fn create_ingredients(
338 _zalsa: &mut Zalsa,
339 first_index: IngredientIndex,
340 ) -> Vec<Box<dyn Ingredient>> {
341 vec![Box::new(IngredientImpl::<C>::new(first_index)) as _]
342 }
343
344 fn id_struct_type_id() -> TypeId {
345 TypeId::of::<C::Struct<'static>>()
346 }
347}
348
349impl<C> IngredientImpl<C>
350where
351 C: Configuration,
352{
353 pub fn new(ingredient_index: IngredientIndex) -> Self {
354 let shards = new_shards();
355 let shift = usize::BITS - shards.len().trailing_zeros();
356
357 Self {
358 ingredient_index,
359 hasher: FxBuildHasher,
360 memo_table_types: Arc::new(MemoTableTypes::default()),
361 revision_queue: RevisionQueue::new(C::REVISIONS),
362 shift,
363 shards,
364 _marker: PhantomData,
365 }
366 }
367 #[inline]
371 fn shard(&self, hash: u64) -> usize {
372 ((hash as usize) << 7) >> self.shift
374 }
375
376 unsafe fn to_internal_data<'db>(&'db self, data: C::Fields<'db>) -> C::Fields<'static> {
381 unsafe { std::mem::transmute::<C::Fields<'db>, C::Fields<'static>>(data) }
383 }
384
385 fn from_internal_data<'db>(data: &'db C::Fields<'static>) -> &'db C::Fields<'db> {
386 unsafe { std::mem::transmute::<&'db C::Fields<'static>, &'db C::Fields<'db>>(data) }
389 }
390
391 pub fn intern<'db, Key>(
395 &'db self,
396 zalsa: &'db Zalsa,
397 zalsa_local: &'db ZalsaLocal,
398 key: Key,
399 ) -> C::Struct<'db>
400 where
401 Key: Hash,
402 C::Fields<'db>: HashEqLike<Key>,
403 Key: Lookup<C::Fields<'db>>,
404 {
405 FromId::from_id(self.intern_id(zalsa, zalsa_local, key))
406 }
407
408 pub fn intern_id<'db, Key>(
412 &'db self,
413 zalsa: &'db Zalsa,
414 zalsa_local: &'db ZalsaLocal,
415 key: Key,
416 ) -> crate::Id
417 where
418 Key: Hash,
419 C::Fields<'db>: HashEqLike<Key>,
424 Key: Lookup<C::Fields<'db>>,
425 {
426 let current_revision = zalsa.current_revision();
428 if C::REVISIONS != IMMORTAL {
429 self.revision_queue.record(current_revision);
430 }
431
432 let hash = self.hasher.hash_one(&key);
434
435 let shard_index = self.shard(hash);
436 let shard = unsafe { &mut *self.shards.get_unchecked(shard_index).lock() };
438
439 let eq = |value: &ValueKey| unsafe { Self::value_eq(value.value::<C>(), &key) };
441
442 if let Some(value) = shard.key_map.find(hash, eq) {
444 let value = unsafe { value.value::<C>() };
446
447 let id = unsafe { *value.lru.id.get() };
449 let mut durability = value.durability.load();
450 let index = self.database_key_index(id);
451
452 if value.lru.last_interned_at.load() < current_revision {
454 value.lru.last_interned_at.store(current_revision);
455
456 zalsa.event(&|| {
457 Event::new(EventKind::DidValidateInternedValue {
458 key: index,
459 revision: current_revision,
460 })
461 });
462
463 if is_reusable::<C>(durability) {
464 unsafe { shard.lru.cursor_mut_from_ptr(&value.lru).remove() };
469
470 unsafe {
473 shard
474 .lru
475 .push_front(UnsafeRef::from_raw(LruEntry::ptr_from_value(value)))
476 };
477 }
478 }
479
480 if !is_reusable::<C>(durability) {
482 if let Some(query_durability) =
483 zalsa_local.report_tracked_read_revision(current_revision)
484 && query_durability > durability
485 {
486 value.durability.store(query_durability);
487 }
488 return id;
489 }
490
491 if let Some((_, stamp)) = zalsa_local.active_query() {
492 durability = std::cmp::max(durability, stamp.durability);
494
495 if !is_reusable::<C>(durability) {
497 unsafe { shard.lru.cursor_mut_from_ptr(&value.lru).remove() };
500 }
501 value.durability.store(durability);
502 }
503
504 report_tracked_read_if_reusable::<C>(zalsa_local, index, current_revision, durability);
510
511 return id;
512 }
513
514 if !self.revision_queue.is_primed() {
516 return self.intern_id_cold(key, zalsa, zalsa_local, shard, shard_index, hash);
517 }
518
519 let Some((slot, value)) = (unsafe { self.find_reusable_slot(current_revision, shard) })
523 else {
524 return self.intern_id_cold(key, zalsa, zalsa_local, shard, shard_index, hash);
526 };
527
528 let (durability, last_interned_at) = zalsa_local
530 .active_query()
531 .map(|(_, stamp)| (stamp.durability, current_revision))
532 .unwrap_or((Durability::MAX, Revision::max()));
535
536 let new_fields = unsafe { self.to_internal_data(key.into_owned()) };
540
541 let old_hash = self.hasher.hash_one(unsafe { &*value.fields.get() });
543
544 let index = self.database_key_index(slot.new_id);
545
546 report_tracked_read_if_reusable::<C>(zalsa_local, index, current_revision, durability);
550
551 let hasher = |value: &ValueKey| unsafe { self.value_hash(value.value::<C>()) };
556 shard.key_map.reserve(1, hasher);
557
558 unsafe { self.clear_memos(zalsa, &mut *value.memos.get(), slot.old_id) };
563
564 unsafe { shard.lru.cursor_mut_from_ptr(&value.lru).remove() };
567
568 let value_key = ValueKey::new(value);
574 shard
575 .key_map
576 .find_entry(old_hash, |found_value| found_value.0 == value_key.0)
577 .unwrap_or_else(|_| panic!("interned value in LRU so must be in key_map"))
578 .remove();
579
580 let old_fields = unsafe { std::mem::replace(&mut *value.fields.get(), new_fields) };
585
586 unsafe { *value.lru.id.get() = slot.new_id };
588
589 shard.key_map.insert_unique(hash, value_key, hasher);
590
591 if is_reusable::<C>(durability) {
592 unsafe {
597 shard
598 .lru
599 .push_front(UnsafeRef::from_raw(LruEntry::ptr_from_value(value)))
600 };
601 }
602
603 value.durability.store(durability);
607 value.lru.last_interned_at.store(last_interned_at);
608
609 drop(old_fields);
610
611 zalsa.event(&|| {
612 Event::new(EventKind::DidReuseInternedValue {
613 key: index,
614 revision: current_revision,
615 })
616 });
617
618 slot.new_id
619 }
620
621 #[allow(clippy::too_many_arguments)]
625 fn intern_id_cold<'db, Key>(
626 &'db self,
627 key: Key,
628 zalsa: &Zalsa,
629 zalsa_local: &ZalsaLocal,
630 shard: &mut IngredientShard,
631 shard_index: usize,
632 hash: u64,
633 ) -> crate::Id
634 where
635 Key: Hash,
636 C::Fields<'db>: HashEqLike<Key>,
637 Key: Lookup<C::Fields<'db>>,
638 {
639 let current_revision = zalsa.current_revision();
640
641 let (durability, last_interned_at) = zalsa_local
643 .active_query()
644 .map(|(_, stamp)| (stamp.durability, current_revision))
645 .unwrap_or((Durability::MAX, Revision::max()));
648
649 let fields = key.into_owned();
652
653 let (id, value) = zalsa_local.allocate(zalsa, self.ingredient_index, |id| Value::<C> {
654 shard: shard_index as u16,
655 lru: LruEntry {
656 link: LinkedListLink::new(),
657 id: UnsafeCell::new(id),
658 last_interned_at: AtomicRevision::new(last_interned_at),
659 },
660 fields: UnsafeCell::new(unsafe { self.to_internal_data(fields) }),
662 memos: UnsafeCell::new(unsafe { MemoTable::new(self.memo_table_types()) }),
665 durability: AtomicDurability::new(durability),
666 });
667
668 unsafe { self.insert_value(shard, hash, value) };
672
673 let index = self.database_key_index(id);
674
675 report_tracked_read_if_reusable::<C>(zalsa_local, index, current_revision, durability);
684
685 zalsa.event(&|| {
686 Event::new(EventKind::DidInternValue {
687 key: index,
688 revision: current_revision,
689 })
690 });
691
692 id
693 }
694
695 unsafe fn insert_value(&self, shard: &mut IngredientShard, hash: u64, value: &Value<C>) {
702 unsafe fn inner(
711 shard: &mut IngredientShard,
712 hash: u64,
713 value_key: ValueKey,
714 entry: *const LruEntry,
715 reusable: bool,
716 hasher: &dyn Fn(&ValueKey) -> u64,
717 ) {
718 if reusable {
719 unsafe { shard.lru.push_front(UnsafeRef::from_raw(entry)) };
722 }
723
724 insert_unique_erased(shard, hash, value_key, hasher);
725
726 debug_assert_eq!(hash, hasher(&value_key));
727 }
728
729 let durability = value.durability.load();
730 let reusable = is_reusable::<C>(durability);
731 let value_key = ValueKey::new(value);
732
733 let hasher = |value: &ValueKey| unsafe { self.value_hash(value.value::<C>()) };
735
736 unsafe {
739 inner(
740 shard,
741 hash,
742 value_key,
743 LruEntry::ptr_from_value(value),
744 reusable,
745 &hasher,
746 )
747 };
748 }
749
750 unsafe fn find_reusable_slot(
762 &self,
763 current_revision: Revision,
764 shard: &mut IngredientShard,
765 ) -> Option<(ReusableSlot, &Value<C>)> {
766 unsafe fn inner(
773 revision_queue: &RevisionQueue,
774 current_revision: Revision,
775 shard: &mut IngredientShard,
776 ) -> Option<ReusableSlot> {
777 let mut cursor = shard.lru.back_mut();
778
779 while let Some(entry) = cursor.as_cursor().clone_pointer() {
780 let entry = UnsafeRef::into_raw(entry);
781
782 let entry_ref = unsafe { &*entry };
784 let last_interned_at = entry_ref.last_interned_at.load();
785
786 if !revision_queue.is_stale(last_interned_at) {
792 return None;
793 }
794
795 assert!(last_interned_at < current_revision);
797
798 let old_id = unsafe { *entry_ref.id.get() };
800
801 if let Some(new_id) = old_id.next_generation() {
805 return Some(ReusableSlot {
806 entry,
807 old_id,
808 new_id,
809 });
810 }
811
812 cursor.remove().unwrap();
814 cursor = shard.lru.back_mut();
815 }
816
817 None
818 }
819
820 let slot = unsafe { inner(&self.revision_queue, current_revision, shard) }?;
822
823 let value = unsafe { LruEntry::value_from_ptr::<C>(slot.entry) };
827
828 Some((slot, value))
829 }
830
831 pub(crate) unsafe fn clear_memos(&self, zalsa: &Zalsa, memo_table: &mut MemoTable, id: Id) {
838 unsafe fn inner(
846 zalsa: &Zalsa,
847 ingredient_index: IngredientIndex,
848 memo_table_types: &MemoTableTypes,
849 memo_table: &mut MemoTable,
850 id: Id,
851 ) {
852 let table = unsafe { memo_table_types.attach_memos_mut(memo_table) };
854
855 struct TableDropGuard<'a>(MemoTableWithTypesMut<'a>);
858
859 impl Drop for TableDropGuard<'_> {
860 fn drop(&mut self) {
861 unsafe { self.0.drop() };
864 }
865 }
866
867 let mut table_guard = TableDropGuard(table);
868
869 unsafe {
872 table_guard.0.take_memos(|memo_ingredient_index, memo| {
873 let ingredient_index =
874 zalsa.ingredient_index_for_memo(ingredient_index, memo_ingredient_index);
875
876 let executor = DatabaseKeyIndex::new(ingredient_index, id);
877
878 zalsa.event(&|| Event::new(EventKind::DidDiscard { key: executor }));
879
880 memo.remove_outputs(zalsa, executor);
881 })
882 };
883
884 std::mem::forget(table_guard);
885
886 memo_table.reset();
888 }
889
890 unsafe {
893 inner(
894 zalsa,
895 self.ingredient_index,
896 &self.memo_table_types,
897 memo_table,
898 id,
899 )
900 }
901 }
902
903 unsafe fn value_hash(&self, value: &Value<C>) -> u64 {
909 unsafe { self.hasher.hash_one(&*value.fields.get()) }
911 }
912
913 unsafe fn value_eq<'db, Key>(value: &'db Value<C>, key: &Key) -> bool
919 where
920 C::Fields<'db>: HashEqLike<Key>,
921 {
922 let fields = unsafe { &*value.fields.get() };
924
925 HashEqLike::eq(Self::from_internal_data(fields), key)
926 }
927
928 #[inline]
930 pub fn database_key_index(&self, id: Id) -> DatabaseKeyIndex {
931 DatabaseKeyIndex::new(self.ingredient_index, id)
932 }
933
934 #[inline]
936 pub fn data<'db>(&'db self, zalsa: &'db Zalsa, id: Id) -> &'db C::Fields<'db> {
937 let value = zalsa.table().get::<Value<C>>(id);
938 value.assert_validated(zalsa.current_revision());
939
940 debug_assert_eq!(
941 id,
942 unsafe { *value.lru.id.get() },
944 "Interned ID for `{}` refers to a reused slot",
945 C::DEBUG_NAME,
946 );
947
948 unsafe { Self::from_internal_data(&*value.fields.get()) }
952 }
953
954 #[inline]
958 pub fn fields<'db>(&'db self, zalsa: &'db Zalsa, s: C::Struct<'db>) -> &'db C::Fields<'db> {
959 self.data(zalsa, AsId::as_id(&s))
960 }
961
962 pub fn reset(&mut self, zalsa_mut: &mut Zalsa) {
963 _ = zalsa_mut;
964
965 for shard in self.shards.iter_mut() {
966 shard.get_mut().key_map.clear();
968 }
969 }
970
971 pub fn entries<'db>(&'db self, zalsa: &'db Zalsa) -> impl Iterator<Item = StructEntry<'db, C>> {
973 unsafe { self.entries_inner(true, zalsa) }
975 }
976
977 unsafe fn entries_inner<'db>(
985 &'db self,
986 should_lock: bool,
987 zalsa: &'db Zalsa,
988 ) -> impl Iterator<Item = StructEntry<'db, C>> {
989 zalsa.table().slots_of::<Value<C>>().map(move |(_, value)| {
991 let id = if should_lock {
992 let _shard =
993 unsafe { self.shards.get_unchecked(value.shard as usize) }.lock();
995
996 let last_interned_at = value.lru.last_interned_at.load();
1001 value
1002 .lru
1003 .last_interned_at
1004 .store(last_interned_at.max(zalsa.current_revision()));
1005 unsafe { *value.lru.id.get() }
1007 } else {
1008 unsafe { *value.lru.id.get() }
1011 };
1012
1013 StructEntry {
1014 value,
1015 key: self.database_key_index(id),
1016 }
1017 })
1018 }
1019}
1020
1021fn new_shards() -> Box<[CachePadded<Mutex<IngredientShard>>]> {
1026 let shard_count = (max_parallelism() * 4).next_power_of_two();
1027 (0..shard_count).map(|_| Default::default()).collect()
1028}
1029
1030#[repr(transparent)]
1032#[derive(Clone, Copy)]
1033struct ValueKey(NonNull<()>);
1034
1035impl ValueKey {
1036 fn new<C: Configuration>(value: &Value<C>) -> Self {
1037 Self(NonNull::from(value).cast())
1038 }
1039
1040 unsafe fn value<'db, C: Configuration>(&self) -> &'db Value<C> {
1044 unsafe { &*self.0.as_ptr().cast::<Value<C>>() }
1046 }
1047}
1048
1049unsafe impl Send for ValueKey {}
1052unsafe impl Sync for ValueKey {}
1054
1055fn insert_unique_erased(
1057 shard: &mut IngredientShard,
1058 hash: u64,
1059 value: ValueKey,
1060 hasher: &dyn Fn(&ValueKey) -> u64,
1061) {
1062 shard.key_map.insert_unique(hash, value, hasher);
1063}
1064
1065pub struct StructEntry<'db, C>
1067where
1068 C: Configuration,
1069{
1070 #[allow(dead_code)]
1071 value: &'db Value<C>,
1072 key: DatabaseKeyIndex,
1073}
1074
1075impl<'db, C> StructEntry<'db, C>
1076where
1077 C: Configuration,
1078{
1079 pub fn key(&self) -> DatabaseKeyIndex {
1081 self.key
1082 }
1083
1084 pub fn as_struct(&self) -> C::Struct<'_> {
1086 FromId::from_id(self.key.key_index())
1087 }
1088
1089 #[cfg(feature = "salsa_unstable")]
1090 pub fn value(&self) -> &'db Value<C> {
1091 self.value
1092 }
1093}
1094
1095impl<C> Ingredient for IngredientImpl<C>
1096where
1097 C: Configuration,
1098{
1099 fn location(&self) -> &'static crate::ingredient::Location {
1100 &C::LOCATION
1101 }
1102
1103 fn ingredient_index(&self) -> IngredientIndex {
1104 self.ingredient_index
1105 }
1106
1107 unsafe fn maybe_changed_after(
1108 &self,
1109 zalsa: &crate::zalsa::Zalsa,
1110 _db: crate::database::RawDatabase<'_>,
1111 input: Id,
1112 _revision: Revision,
1113 ) -> VerifyResult {
1114 let current_revision = zalsa.current_revision();
1116 if C::REVISIONS != IMMORTAL {
1117 self.revision_queue.record(current_revision);
1118 }
1119
1120 let value = zalsa.table().get::<Value<C>>(input);
1121
1122 let _shard = unsafe { self.shards.get_unchecked(value.shard as usize) }.lock();
1124
1125 let id = unsafe { *value.lru.id.get() };
1127
1128 if id.generation() > input.generation() {
1130 return VerifyResult::changed();
1131 }
1132
1133 value.lru.last_interned_at.store(current_revision);
1135
1136 zalsa.event(&|| {
1137 let index = self.database_key_index(input);
1138
1139 Event::new(EventKind::DidValidateInternedValue {
1140 key: index,
1141 revision: current_revision,
1142 })
1143 });
1144
1145 VerifyResult::unchanged()
1147 }
1148
1149 unsafe fn collect_minimum_serialized_edges(
1150 &self,
1151 _zalsa: &Zalsa,
1152 edge: QueryEdge,
1153 serialized_edges: &mut FxIndexSet<QueryEdge>,
1154 _visited_edges: &mut FxHashSet<QueryEdge>,
1155 ) {
1156 if C::PERSIST && C::REVISIONS != IMMORTAL {
1157 serialized_edges.insert(edge);
1163 }
1164
1165 }
1167
1168 fn flatten_cycle_head_dependencies(
1169 &self,
1170 _zalsa: &Zalsa,
1171 id: Id,
1172 flattened_input_outputs: &mut FxIndexSet<QueryEdge>,
1173 _seen: &mut FxHashSet<DatabaseKeyIndex>,
1174 ) {
1175 flattened_input_outputs.insert(QueryEdge::input(self.database_key_index(id)));
1176 }
1177
1178 fn debug_name(&self) -> &'static str {
1179 C::DEBUG_NAME
1180 }
1181
1182 fn jar_kind(&self) -> JarKind {
1183 JarKind::Struct
1184 }
1185
1186 fn memo_table_types(&self) -> &Arc<MemoTableTypes> {
1187 &self.memo_table_types
1188 }
1189
1190 fn memo_table_types_mut(&mut self) -> &mut Arc<MemoTableTypes> {
1191 &mut self.memo_table_types
1192 }
1193
1194 #[cfg(all(not(feature = "shuttle"), feature = "salsa_unstable"))]
1196 unsafe fn memory_usage(
1197 &self,
1198 db: &dyn crate::Database,
1199 ) -> Option<Vec<crate::database::SlotInfo>> {
1200 let entries = unsafe { self.entries_inner(false, db.zalsa()) };
1202
1203 let memory_usage = entries
1204 .map(|entry| unsafe { entry.value.memory_usage(&self.memo_table_types) })
1207 .collect();
1208
1209 Some(memory_usage)
1210 }
1211
1212 fn is_persistable(&self) -> bool {
1213 C::PERSIST
1214 }
1215
1216 fn should_serialize(&self, zalsa: &Zalsa) -> bool {
1217 C::PERSIST && zalsa.table().slots_of::<Value<C>>().next().is_some()
1218 }
1219
1220 #[cfg(feature = "persistence")]
1221 unsafe fn serialize<'db>(
1222 &'db self,
1223 zalsa: &'db Zalsa,
1224 f: &mut dyn FnMut(&dyn erased_serde::Serialize),
1225 ) {
1226 f(&persistence::SerializeIngredient {
1227 zalsa,
1228 _ingredient: self,
1229 })
1230 }
1231
1232 #[cfg(feature = "persistence")]
1233 fn deserialize(
1234 &mut self,
1235 zalsa: &mut Zalsa,
1236 deserializer: &mut dyn erased_serde::Deserializer,
1237 ) -> Result<(), erased_serde::Error> {
1238 let deserialize = persistence::DeserializeIngredient {
1239 zalsa,
1240 ingredient: self,
1241 };
1242
1243 serde::de::DeserializeSeed::deserialize(deserialize, deserializer)
1244 }
1245}
1246
1247impl<C> std::fmt::Debug for IngredientImpl<C>
1248where
1249 C: Configuration,
1250{
1251 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1252 f.debug_struct(std::any::type_name::<Self>())
1253 .field("index", &self.ingredient_index)
1254 .finish()
1255 }
1256}
1257
1258unsafe impl<C> Slot for Value<C>
1260where
1261 C: Configuration,
1262{
1263 #[inline(always)]
1264 unsafe fn memos(
1265 this: *const Self,
1266 current_revision: Revision,
1267 ) -> *const crate::table::memo::MemoTable {
1268 unsafe { (*this).assert_validated(current_revision) };
1270 unsafe { Self::memos_unchecked(this) }
1272 }
1273
1274 #[inline(always)]
1275 unsafe fn memos_unchecked(this: *const Self) -> *const crate::table::memo::MemoTable {
1276 unsafe { (*this).memos.get() }
1278 }
1279
1280 #[inline(always)]
1281 fn memos_mut(&mut self) -> &mut crate::table::memo::MemoTable {
1282 self.memos.get_mut()
1283 }
1284}
1285
1286impl<C> Value<C>
1287where
1288 C: Configuration,
1289{
1290 #[inline]
1292 fn assert_validated(&self, current_revision: Revision) {
1293 assert!(
1297 C::REVISIONS == IMMORTAL
1298 || self.lru.last_interned_at.load() >= current_revision
1299 || !is_reusable::<C>(self.durability.load()),
1300 "Data for reusable `{}` was not interned in the latest revision for its durability.",
1301 C::DEBUG_NAME,
1302 );
1303 }
1304}
1305
1306struct RevisionQueue {
1312 lock: Mutex<()>,
1313 revisions: SmallVec<[AtomicRevision; DEFAULT_REVISIONS]>,
1315}
1316
1317const IMMORTAL: NonZeroUsize = NonZeroUsize::MAX;
1319
1320impl RevisionQueue {
1321 fn new(capacity: NonZeroUsize) -> Self {
1322 let revisions = if capacity == IMMORTAL {
1323 SmallVec::new()
1324 } else {
1325 (0..capacity.get())
1326 .map(|_| AtomicRevision::start())
1327 .collect()
1328 };
1329
1330 Self {
1331 lock: Mutex::new(()),
1332 revisions,
1333 }
1334 }
1335
1336 #[inline]
1338 fn record(&self, revision: Revision) {
1339 debug_assert!(
1340 !self.revisions.is_empty(),
1341 "cannot record revisions when interned garbage collection is disabled"
1342 );
1343
1344 if self.revisions[0].load() >= revision {
1346 return;
1347 }
1348
1349 self.record_cold(revision);
1350 }
1351
1352 #[cold]
1353 fn record_cold(&self, revision: Revision) {
1354 let _lock = self.lock.lock();
1355
1356 if self.revisions[0].load() >= revision {
1358 return;
1359 }
1360
1361 for i in (1..self.revisions.len()).rev() {
1365 self.revisions[i].store(self.revisions[i - 1].load());
1366 }
1367
1368 self.revisions[0].store(revision);
1369 }
1370
1371 #[inline]
1373 fn is_stale(&self, revision: Revision) -> bool {
1374 let Some(oldest) = self.revisions.last() else {
1375 return false;
1376 };
1377 let oldest = oldest.load();
1378
1379 if oldest == Revision::start() {
1381 return false;
1382 }
1383
1384 revision < oldest
1385 }
1386
1387 #[inline]
1390 fn is_primed(&self) -> bool {
1391 self.revisions
1392 .last()
1393 .is_some_and(|oldest| oldest.load() > Revision::start())
1394 }
1395}
1396
1397pub trait HashEqLike<O> {
1399 fn eq(&self, data: &O) -> bool;
1400}
1401
1402pub trait Lookup<O> {
1417 fn into_owned(self) -> O;
1418}
1419
1420impl<T> Lookup<T> for T {
1421 fn into_owned(self) -> T {
1422 self
1423 }
1424}
1425
1426impl<T> HashEqLike<T> for T
1427where
1428 T: Hash + Eq,
1429{
1430 fn eq(&self, data: &T) -> bool {
1431 self == data
1432 }
1433}
1434
1435impl<T> HashEqLike<T> for &T
1436where
1437 T: Hash + Eq,
1438{
1439 fn eq(&self, data: &T) -> bool {
1440 **self == *data
1441 }
1442}
1443
1444impl<T> HashEqLike<&T> for T
1445where
1446 T: Hash + Eq,
1447{
1448 fn eq(&self, data: &&T) -> bool {
1449 *self == **data
1450 }
1451}
1452
1453impl<T> Lookup<T> for &T
1454where
1455 T: Clone,
1456{
1457 fn into_owned(self) -> T {
1458 Clone::clone(self)
1459 }
1460}
1461
1462impl<'a, T> HashEqLike<&'a T> for Box<T>
1463where
1464 T: ?Sized + Hash + Eq,
1465 Box<T>: From<&'a T>,
1466{
1467 fn eq(&self, data: &&T) -> bool {
1468 **self == **data
1469 }
1470}
1471
1472impl<'a, T> Lookup<Box<T>> for &'a T
1473where
1474 T: ?Sized + Hash + Eq,
1475 Box<T>: From<&'a T>,
1476{
1477 fn into_owned(self) -> Box<T> {
1478 Box::from(self)
1479 }
1480}
1481
1482impl<'a, T> HashEqLike<&'a T> for Arc<T>
1483where
1484 T: ?Sized + Hash + Eq,
1485 Arc<T>: From<&'a T>,
1486{
1487 fn eq(&self, data: &&T) -> bool {
1488 **self == **data
1489 }
1490}
1491
1492impl<'a, T> Lookup<Arc<T>> for &'a T
1493where
1494 T: ?Sized + Hash + Eq,
1495 Arc<T>: From<&'a T>,
1496{
1497 fn into_owned(self) -> Arc<T> {
1498 Arc::from(self)
1499 }
1500}
1501
1502#[cfg(feature = "triomphe")]
1503impl<'a, T> HashEqLike<&'a T> for triomphe::Arc<T>
1504where
1505 T: ?Sized + Hash + Eq,
1506 triomphe::Arc<T>: From<&'a T>,
1507{
1508 fn eq(&self, data: &&T) -> bool {
1509 **self == **data
1510 }
1511}
1512
1513#[cfg(feature = "triomphe")]
1514impl<'a, T> Lookup<triomphe::Arc<T>> for &'a T
1515where
1516 T: ?Sized + Hash + Eq,
1517 triomphe::Arc<T>: From<&'a T>,
1518{
1519 fn into_owned(self) -> triomphe::Arc<T> {
1520 triomphe::Arc::from(self)
1521 }
1522}
1523
1524impl Lookup<String> for &str {
1525 fn into_owned(self) -> String {
1526 self.to_owned()
1527 }
1528}
1529
1530#[cfg(feature = "compact_str")]
1531impl Lookup<compact_str::CompactString> for &str {
1532 fn into_owned(self) -> compact_str::CompactString {
1533 compact_str::CompactString::new(self)
1534 }
1535}
1536
1537#[cfg(feature = "compact_str")]
1538impl HashEqLike<&str> for compact_str::CompactString {
1539 fn eq(&self, data: &&str) -> bool {
1540 self == *data
1541 }
1542}
1543
1544#[cfg(feature = "compact_str")]
1545impl HashEqLike<Cow<'_, str>> for compact_str::CompactString {
1546 fn eq(&self, data: &Cow<'_, str>) -> bool {
1547 self.as_str() == data.as_ref()
1548 }
1549}
1550
1551#[cfg(feature = "compact_str")]
1552impl Lookup<compact_str::CompactString> for Cow<'_, str> {
1553 fn into_owned(self) -> compact_str::CompactString {
1554 compact_str::CompactString::new(Cow::into_owned(self))
1555 }
1556}
1557
1558impl HashEqLike<&str> for String {
1559 fn eq(&self, data: &&str) -> bool {
1560 self == *data
1561 }
1562}
1563
1564impl<A, T: Hash + Eq + PartialEq<A>> HashEqLike<&[A]> for Vec<T> {
1565 fn eq(&self, data: &&[A]) -> bool {
1566 self.len() == data.len() && data.iter().enumerate().all(|(i, a)| &self[i] == a)
1567 }
1568}
1569
1570impl<A: Hash + Eq + PartialEq<T> + Clone + Lookup<T>, T> Lookup<Vec<T>> for &[A] {
1571 fn into_owned(self) -> Vec<T> {
1572 self.iter().map(|a| Lookup::into_owned(a.clone())).collect()
1573 }
1574}
1575
1576impl<const N: usize, A, T: Hash + Eq + PartialEq<A>> HashEqLike<[A; N]> for Vec<T> {
1577 fn eq(&self, data: &[A; N]) -> bool {
1578 self.len() == data.len() && data.iter().enumerate().all(|(i, a)| &self[i] == a)
1579 }
1580}
1581
1582impl<const N: usize, A: Hash + Eq + PartialEq<T> + Clone + Lookup<T>, T> Lookup<Vec<T>> for [A; N] {
1583 fn into_owned(self) -> Vec<T> {
1584 self.into_iter()
1585 .map(|a| Lookup::into_owned(a.clone()))
1586 .collect()
1587 }
1588}
1589
1590impl HashEqLike<&Path> for PathBuf {
1591 fn eq(&self, data: &&Path) -> bool {
1592 self == data
1593 }
1594}
1595
1596impl Lookup<PathBuf> for &Path {
1597 fn into_owned(self) -> PathBuf {
1598 self.to_owned()
1599 }
1600}
1601
1602impl<T: Hash + Eq + Clone> HashEqLike<Cow<'_, T>> for T {
1603 fn eq(&self, data: &Cow<'_, T>) -> bool {
1604 self == data.as_ref()
1605 }
1606}
1607
1608impl<T: Clone> Lookup<T> for Cow<'_, T> {
1609 fn into_owned(self) -> T {
1610 Cow::into_owned(self)
1611 }
1612}
1613
1614impl HashEqLike<Cow<'_, str>> for String {
1615 fn eq(&self, data: &Cow<'_, str>) -> bool {
1616 self.as_str() == data.as_ref()
1617 }
1618}
1619
1620impl Lookup<String> for Cow<'_, str> {
1621 fn into_owned(self) -> String {
1622 Cow::into_owned(self)
1623 }
1624}
1625
1626impl HashEqLike<Cow<'_, Path>> for PathBuf {
1627 fn eq(&self, data: &Cow<'_, Path>) -> bool {
1628 self.as_path() == data.as_ref()
1629 }
1630}
1631
1632impl Lookup<PathBuf> for Cow<'_, Path> {
1633 fn into_owned(self) -> PathBuf {
1634 Cow::into_owned(self)
1635 }
1636}
1637
1638impl<T: Hash + Eq + Clone> HashEqLike<Cow<'_, [T]>> for Box<[T]> {
1639 fn eq(&self, data: &Cow<'_, [T]>) -> bool {
1640 self.as_ref() == data.as_ref()
1641 }
1642}
1643
1644impl<T: Clone> Lookup<Box<[T]>> for Cow<'_, [T]> {
1645 fn into_owned(self) -> Box<[T]> {
1646 Cow::into_owned(self).into_boxed_slice()
1647 }
1648}
1649
1650impl<T: Hash + Eq + Clone> HashEqLike<Cow<'_, [T]>> for Vec<T> {
1651 fn eq(&self, data: &Cow<'_, [T]>) -> bool {
1652 self.as_slice() == data.as_ref()
1653 }
1654}
1655
1656impl<T: Clone> Lookup<Vec<T>> for Cow<'_, [T]> {
1657 fn into_owned(self) -> Vec<T> {
1658 Cow::into_owned(self)
1659 }
1660}
1661
1662#[cfg(feature = "persistence")]
1663mod persistence {
1664 use std::cell::UnsafeCell;
1665 use std::fmt;
1666 use std::hash::BuildHasher;
1667
1668 use intrusive_collections::LinkedListLink;
1669 use serde::ser::{SerializeMap, SerializeStruct};
1670 use serde::{Deserialize, de};
1671
1672 use super::{AtomicDurability, AtomicRevision, Configuration, IngredientImpl, LruEntry, Value};
1673 use crate::plumbing::Ingredient;
1674 use crate::table::memo::MemoTable;
1675 use crate::zalsa::Zalsa;
1676 use crate::{Durability, Id, Revision};
1677
1678 pub struct SerializeIngredient<'db, C>
1679 where
1680 C: Configuration,
1681 {
1682 pub zalsa: &'db Zalsa,
1683 pub _ingredient: &'db IngredientImpl<C>,
1684 }
1685
1686 impl<C> serde::Serialize for SerializeIngredient<'_, C>
1687 where
1688 C: Configuration,
1689 {
1690 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
1691 where
1692 S: serde::Serializer,
1693 {
1694 let Self { zalsa, .. } = *self;
1695
1696 let count = zalsa.table().slots_of::<Value<C>>().count();
1699 let mut map = serializer.serialize_map(Some(count))?;
1700
1701 for (_, value) in zalsa.table().slots_of::<Value<C>>() {
1702 let id = unsafe { *value.lru.id.get() };
1705
1706 map.serialize_entry(&id.as_bits(), value)?;
1707 }
1708
1709 map.end()
1710 }
1711 }
1712
1713 impl<C> serde::Serialize for Value<C>
1714 where
1715 C: Configuration,
1716 {
1717 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
1718 where
1719 S: serde::Serializer,
1720 {
1721 let mut value = serializer.serialize_struct("Value,", 3)?;
1722
1723 let Value {
1724 fields,
1725 lru,
1726 durability,
1727 shard: _,
1728 memos: _,
1729 } = self;
1730 let LruEntry {
1731 link: _,
1732 id: _,
1733 last_interned_at,
1734 } = lru;
1735
1736 let fields = unsafe { &*fields.get() };
1739
1740 let durability = durability.load();
1741 let last_interned_at = last_interned_at.load();
1742
1743 value.serialize_field("durability", &durability)?;
1744 value.serialize_field("last_interned_at", &last_interned_at)?;
1745 value.serialize_field("fields", &SerializeFields::<C>(fields))?;
1746
1747 value.end()
1748 }
1749 }
1750
1751 struct SerializeFields<'db, C: Configuration>(&'db C::Fields<'static>);
1752
1753 impl<C> serde::Serialize for SerializeFields<'_, C>
1754 where
1755 C: Configuration,
1756 {
1757 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
1758 where
1759 S: serde::Serializer,
1760 {
1761 C::serialize(IngredientImpl::<C>::from_internal_data(self.0), serializer)
1762 }
1763 }
1764
1765 pub struct DeserializeIngredient<'db, C>
1766 where
1767 C: Configuration,
1768 {
1769 pub zalsa: &'db mut Zalsa,
1770 pub ingredient: &'db mut IngredientImpl<C>,
1771 }
1772
1773 impl<'de, C> de::DeserializeSeed<'de> for DeserializeIngredient<'_, C>
1774 where
1775 C: Configuration,
1776 {
1777 type Value = ();
1778
1779 fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
1780 where
1781 D: serde::Deserializer<'de>,
1782 {
1783 deserializer.deserialize_map(self)
1784 }
1785 }
1786
1787 impl<'de, C> de::Visitor<'de> for DeserializeIngredient<'_, C>
1788 where
1789 C: Configuration,
1790 {
1791 type Value = ();
1792
1793 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1794 formatter.write_str("a map")
1795 }
1796
1797 fn visit_map<M>(self, mut access: M) -> Result<Self::Value, M::Error>
1798 where
1799 M: de::MapAccess<'de>,
1800 {
1801 let DeserializeIngredient { zalsa, ingredient } = self;
1802
1803 while let Some((id, value)) = access.next_entry::<u64, DeserializeValue<C>>()? {
1804 let id = Id::from_bits(id);
1805 let (page_idx, _) = crate::table::split_id(id);
1806
1807 let hash = ingredient.hasher.hash_one(&value.fields.0);
1809 let shard_index = ingredient.shard(hash);
1810
1811 let shard = unsafe { &mut *ingredient.shards.get_unchecked(shard_index).lock() };
1813
1814 let value = Value::<C> {
1815 shard: shard_index as u16,
1816 lru: LruEntry {
1817 link: LinkedListLink::new(),
1818 id: UnsafeCell::new(id),
1819 last_interned_at: AtomicRevision::new(value.last_interned_at),
1820 },
1821 fields: UnsafeCell::new(value.fields.0),
1822 memos: UnsafeCell::new(unsafe {
1825 MemoTable::new(ingredient.memo_table_types())
1826 }),
1827 durability: AtomicDurability::new(value.durability),
1828 };
1829
1830 zalsa.table_mut().force_page::<Value<C>>(
1832 page_idx,
1833 ingredient.ingredient_index(),
1834 ingredient.memo_table_types(),
1835 );
1836
1837 let (allocated_id, value) = unsafe {
1841 zalsa
1842 .table()
1843 .page(page_idx)
1844 .allocate(page_idx, |_| value)
1845 .unwrap_or_else(|_| panic!("serialized an invalid `Id`: {id:?}"))
1846 };
1847
1848 assert_eq!(
1849 allocated_id.index(),
1850 id.index(),
1851 "values are serialized in allocation order"
1852 );
1853
1854 unsafe { ingredient.insert_value(shard, hash, value) };
1859 }
1860
1861 Ok(())
1862 }
1863 }
1864
1865 #[derive(Deserialize)]
1866 #[serde(rename = "Value")]
1867 pub struct DeserializeValue<C: Configuration> {
1868 durability: Durability,
1869 last_interned_at: Revision,
1870 #[serde(bound = "C: Configuration")]
1871 fields: DeserializeFields<C>,
1872 }
1873
1874 struct DeserializeFields<C: Configuration>(C::Fields<'static>);
1875
1876 impl<'de, C> serde::Deserialize<'de> for DeserializeFields<C>
1877 where
1878 C: Configuration,
1879 {
1880 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1881 where
1882 D: serde::Deserializer<'de>,
1883 {
1884 C::deserialize(deserializer)
1885 .map(DeserializeFields)
1886 .map_err(de::Error::custom)
1887 }
1888 }
1889}
1890
1891#[cfg(all(not(feature = "shuttle"), target_pointer_width = "64"))]
1892mod _static_assertions {
1893 use std::mem;
1894
1895 use super::LruEntry;
1896 use super::{Configuration, Value};
1897 use crate::{Id, plumbing};
1898
1899 const _: [(); mem::size_of::<LruEntry>()] = [(); mem::size_of::<[usize; 4]>()];
1900
1901 const _: [(); mem::size_of::<Value<DummyConfiguration>>()] = [(); mem::size_of::<[usize; 7]>()];
1902
1903 struct DummyConfiguration;
1904
1905 unsafe impl Configuration for DummyConfiguration {
1907 const LOCATION: crate::ingredient::Location =
1908 crate::ingredient::Location { file: "", line: 0 };
1909 const DEBUG_NAME: &'static str = "";
1910 const PERSIST: bool = false;
1911
1912 type Fields<'db> = [u8; 1];
1913 type Struct<'db> = Id;
1914
1915 fn serialize<S>(_: &Self::Fields<'_>, _: S) -> Result<S::Ok, S::Error>
1916 where
1917 S: plumbing::serde::Serializer,
1918 {
1919 unimplemented!()
1920 }
1921
1922 fn deserialize<'de, D>(_: D) -> Result<Self::Fields<'static>, D::Error>
1923 where
1924 D: plumbing::serde::Deserializer<'de>,
1925 {
1926 unimplemented!()
1927 }
1928 }
1929}
1930
1931#[cfg(test)]
1932mod tests {
1933 use super::*;
1934
1935 struct TestConfiguration;
1936
1937 unsafe impl Configuration for TestConfiguration {
1939 const LOCATION: crate::ingredient::Location = crate::ingredient::Location {
1940 file: file!(),
1941 line: line!(),
1942 };
1943 const DEBUG_NAME: &'static str = "TestConfiguration";
1944 const PERSIST: bool = false;
1945 const REVISIONS: NonZeroUsize = NonZeroUsize::new(2).unwrap();
1946
1947 type Fields<'db> = ();
1948 type Struct<'db> = Id;
1949
1950 fn serialize<S>(_value: &Self::Fields<'_>, _serializer: S) -> Result<S::Ok, S::Error>
1951 where
1952 S: plumbing::serde::Serializer,
1953 {
1954 unimplemented!()
1955 }
1956
1957 fn deserialize<'de, D>(_deserializer: D) -> Result<Self::Fields<'static>, D::Error>
1958 where
1959 D: plumbing::serde::Deserializer<'de>,
1960 {
1961 unimplemented!()
1962 }
1963 }
1964
1965 #[test]
1966 fn revision_queue_records_each_revision_once() {
1967 let queue = RevisionQueue::new(TestConfiguration::REVISIONS);
1968 let revision = Revision::start().next();
1969
1970 queue.record_cold(revision);
1972 queue.record_cold(revision);
1973
1974 assert_eq!(queue.revisions[0].load(), revision);
1975 assert_eq!(queue.revisions[1].load(), Revision::start());
1976 }
1977}