Skip to content

Commit 793dc41

Browse files
committed
Assert size for interned Value
1 parent 60026c0 commit 793dc41

File tree

1 file changed

+34
-26
lines changed

1 file changed

+34
-26
lines changed

src/interned.rs

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,19 @@ impl<C: Configuration> Default for IngredientShard<C> {
9292

9393
// SAFETY: `LinkedListLink` is `!Sync`, however, the linked list is only accessed through the
9494
// ingredient lock, and values are only ever linked to a single list on the ingredient.
95-
unsafe impl<C: Configuration> Sync for Value<C> {}
95+
unsafe impl<Fields: Sync> Sync for Value<Fields> {}
9696

97-
intrusive_adapter!(ValueAdapter<C> = UnsafeRef<Value<C>>: Value<C> { link: LinkedListLink } where C: Configuration);
97+
intrusive_adapter!(ValueAdapter<C> = UnsafeRef<ValueForConfiguration<C>>: ValueForConfiguration<C> { link: LinkedListLink } where C: Configuration);
98+
99+
#[cfg(not(feature = "shuttle"))]
100+
#[cfg(target_pointer_width = "64")]
101+
const _: [(); std::mem::size_of::<Value<()>>()] = [(); std::mem::size_of::<[usize; 7]>()];
102+
103+
#[allow(type_alias_bounds)]
104+
type ValueForConfiguration<C: Configuration> = Value<C::Fields<'static>>;
98105

99106
/// Struct storing the interned fields.
100-
pub struct Value<C>
101-
where
102-
C: Configuration,
103-
{
107+
pub struct Value<Fields> {
104108
/// The index of the shard containing this value.
105109
shard: u16,
106110

@@ -111,7 +115,7 @@ where
111115
///
112116
/// These are valid for read-only access as long as the lock is held
113117
/// or the value has been validated in the current revision.
114-
fields: UnsafeCell<C::Fields<'static>>,
118+
fields: UnsafeCell<Fields>,
115119

116120
/// Memos attached to this interned value.
117121
///
@@ -165,13 +169,10 @@ impl ValueShared {
165169
}
166170
}
167171

168-
impl<C> Value<C>
169-
where
170-
C: Configuration,
171-
{
172+
impl<Fields> Value<Fields> {
172173
/// Fields of this interned struct.
173174
#[cfg(feature = "salsa_unstable")]
174-
pub fn fields(&self) -> &C::Fields<'static> {
175+
pub fn fields(&self) -> &Fields {
175176
// SAFETY: The fact that this function is safe is technically unsound. However, interned
176177
// values are only exposed if they have been validated in the current revision, which
177178
// ensures that they are not reused while being accessed.
@@ -548,7 +549,9 @@ where
548549
.unwrap_or((Durability::MAX, Revision::max()));
549550

550551
// Allocate the value slot.
551-
let id = zalsa_local.allocate(zalsa, self.ingredient_index, |id| Value::<C> {
552+
let id = zalsa_local.allocate(zalsa, self.ingredient_index, |id| ValueForConfiguration::<
553+
C,
554+
> {
552555
shard: shard_index as u16,
553556
link: LinkedListLink::new(),
554557
memos: UnsafeCell::new(MemoTable::default()),
@@ -561,7 +564,7 @@ where
561564
}),
562565
});
563566

564-
let value = zalsa.table().get::<Value<C>>(id);
567+
let value = Self::table_get(zalsa, id);
565568
// SAFETY: We hold the lock for the shard containing the value.
566569
let value_shared = unsafe { &mut *value.shared.get() };
567570

@@ -580,7 +583,7 @@ where
580583
shard.key_map.insert_unique(hash, id, hasher);
581584

582585
debug_assert_eq!(hash, {
583-
let value = zalsa.table().get::<Value<C>>(id);
586+
let value = Self::table_get(zalsa, id);
584587

585588
// SAFETY: We hold the lock for the shard containing the value.
586589
unsafe { self.hasher.hash_one(&*value.fields.get()) }
@@ -652,7 +655,7 @@ where
652655
unsafe fn value_hash<'db>(&'db self, id: Id, zalsa: &'db Zalsa) -> u64 {
653656
// This closure is only called if the table is resized. So while it's expensive
654657
// to lookup all values, it will only happen rarely.
655-
let value = zalsa.table().get::<Value<C>>(id);
658+
let value = Self::table_get(zalsa, id);
656659

657660
// SAFETY: We hold the lock for the shard containing the value.
658661
unsafe { self.hasher.hash_one(&*value.fields.get()) }
@@ -667,12 +670,12 @@ where
667670
id: Id,
668671
key: &Key,
669672
zalsa: &'db Zalsa,
670-
found_value: &Cell<Option<&'db Value<C>>>,
673+
found_value: &Cell<Option<&'db ValueForConfiguration<C>>>,
671674
) -> bool
672675
where
673676
C::Fields<'db>: HashEqLike<Key>,
674677
{
675-
let value = zalsa.table().get::<Value<C>>(id);
678+
let value = Self::table_get(zalsa, id);
676679
found_value.set(Some(value));
677680

678681
// SAFETY: We hold the lock for the shard containing the value.
@@ -690,7 +693,7 @@ where
690693
/// Lookup the data for an interned value based on its ID.
691694
pub fn data<'db>(&'db self, db: &'db dyn Database, id: Id) -> &'db C::Fields<'db> {
692695
let zalsa = db.zalsa();
693-
let value = zalsa.table().get::<Value<C>>(id);
696+
let value = Self::table_get(zalsa, id);
694697

695698
debug_assert!(
696699
{
@@ -732,8 +735,16 @@ where
732735
pub fn entries<'db>(
733736
&'db self,
734737
db: &'db dyn crate::Database,
735-
) -> impl Iterator<Item = &'db Value<C>> {
736-
db.zalsa().table().slots_of::<Value<C>>()
738+
) -> impl Iterator<Item = &'db ValueForConfiguration<C>> {
739+
db.zalsa().table().slots_of::<ValueForConfiguration<C>>()
740+
}
741+
742+
#[inline]
743+
fn table_get(zalsa: &Zalsa, id: Id) -> &ValueForConfiguration<C>
744+
where
745+
C: Configuration,
746+
{
747+
zalsa.table().get::<ValueForConfiguration<C>>(id)
737748
}
738749
}
739750

@@ -762,7 +773,7 @@ where
762773
let current_revision = zalsa.current_revision();
763774
self.revision_queue.record(current_revision);
764775

765-
let value = zalsa.table().get::<Value<C>>(input);
776+
let value = zalsa.table().get::<ValueForConfiguration<C>>(input);
766777

767778
// SAFETY: `value.shard` is guaranteed to be in-bounds for `self.shards`.
768779
let _shard = unsafe { self.shards.get_unchecked(value.shard as usize) }.lock();
@@ -811,10 +822,7 @@ where
811822
}
812823
}
813824

814-
impl<C> Slot for Value<C>
815-
where
816-
C: Configuration,
817-
{
825+
impl<Fields: Send + Sync + 'static> Slot for Value<Fields> {
818826
#[inline(always)]
819827
unsafe fn memos(&self, _current_revision: Revision) -> &MemoTable {
820828
// SAFETY: The fact that we have a reference to the `Value` means it must

0 commit comments

Comments
 (0)