Skip to content

Commit 932afa0

Browse files
committed
Assert size for interned Value
1 parent 5d11ab9 commit 932afa0

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; 8]>()];
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
///
@@ -164,13 +168,10 @@ impl ValueShared {
164168
}
165169
}
166170

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

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

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

@@ -579,7 +582,7 @@ where
579582
shard.key_map.insert_unique(hash, id, hasher);
580583

581584
debug_assert_eq!(hash, {
582-
let value = zalsa.table().get::<Value<C>>(id);
585+
let value = Self::table_get(zalsa, id);
583586

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

656659
// SAFETY: We hold the lock for the shard containing the value.
657660
unsafe { self.hasher.hash_one(&*value.fields.get()) }
@@ -666,12 +669,12 @@ where
666669
id: Id,
667670
key: &Key,
668671
zalsa: &'db Zalsa,
669-
found_value: &Cell<Option<&'db Value<C>>>,
672+
found_value: &Cell<Option<&'db ValueForConfiguration<C>>>,
670673
) -> bool
671674
where
672675
C::Fields<'db>: HashEqLike<Key>,
673676
{
674-
let value = zalsa.table().get::<Value<C>>(id);
677+
let value = Self::table_get(zalsa, id);
675678
found_value.set(Some(value));
676679

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

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

@@ -761,7 +772,7 @@ where
761772
let current_revision = zalsa.current_revision();
762773
self.revision_queue.record(current_revision);
763774

764-
let value = zalsa.table().get::<Value<C>>(input);
775+
let value = zalsa.table().get::<ValueForConfiguration<C>>(input);
765776

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

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

0 commit comments

Comments
 (0)