Skip to content

Commit db71a89

Browse files
committed
add plumbing to reuse interned slots
1 parent 99ec0a4 commit db71a89

File tree

6 files changed

+58
-14
lines changed

6 files changed

+58
-14
lines changed

src/active_query.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ impl ActiveQuery {
142142
/// Used during cycle recovery, see [`Runtime::unblock_cycle_and_maybe_throw`].
143143
pub(super) fn remove_cycle_participants(&mut self, cycle: &Cycle) {
144144
for p in cycle.participant_keys() {
145-
let p: DatabaseKeyIndex = p.into();
145+
let p: DatabaseKeyIndex = p;
146146
self.input_outputs.shift_remove(&(EdgeKind::Input, p));
147147
}
148148
}

src/function/fetch.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ where
2121
self.evict_value_from_memo_for(zalsa, evicted);
2222
}
2323

24-
zalsa_local.report_tracked_read(self.database_key_index(id).into(), durability, changed_at);
24+
zalsa_local.report_tracked_read(self.database_key_index(id), durability, changed_at);
2525

2626
value
2727
}

src/function/specify.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ where
3939
//
4040
// Now, if We invoke Q3 first, We get one result for Q2, but if We invoke Q4 first, We get a different value. That's no good.
4141
let database_key_index = <C::Input<'db>>::database_key_index(db.as_dyn_database(), key);
42-
let dependency_index = database_key_index.into();
42+
let dependency_index = database_key_index;
4343
if !zalsa_local.is_output_of_active_query(dependency_index) {
4444
panic!("can only use `specify` on salsa structs created during the current tracked fn");
4545
}
@@ -92,7 +92,7 @@ where
9292

9393
// Record that the current query *specified* a value for this cell.
9494
let database_key_index = self.database_key_index(key);
95-
zalsa_local.add_output(database_key_index.into());
95+
zalsa_local.add_output(database_key_index);
9696
}
9797

9898
/// Invoked when the query `executor` has been validated as having green inputs

src/interned.rs

+44-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::durability::Durability;
22
use crate::id::AsId;
33
use crate::ingredient::fmt_index;
44
use crate::plumbing::{Jar, JarAux};
5+
use crate::revision::AtomicRevision;
56
use crate::table::memo::MemoTable;
67
use crate::table::sync::SyncTable;
78
use crate::table::Slot;
@@ -67,6 +68,10 @@ where
6768
data: C::Data<'static>,
6869
memos: MemoTable,
6970
syncs: SyncTable,
71+
// The revision the value was first interned in.
72+
first_interned_at: Revision,
73+
// The most recent interned revision.
74+
last_interned_at: AtomicRevision,
7075
}
7176

7277
impl<C: Configuration> Default for JarImpl<C> {
@@ -120,7 +125,8 @@ where
120125
db: &'db dyn crate::Database,
121126
data: impl Lookup<C::Data<'db>>,
122127
) -> C::Struct<'db> {
123-
let zalsa_local = db.zalsa_local();
128+
let (zalsa, zalsa_local) = db.zalsas();
129+
let current_revision = zalsa.current_revision();
124130

125131
// Optimisation to only get read lock on the map if the data has already
126132
// been interned.
@@ -142,9 +148,13 @@ where
142148
// SAFETY: Read lock on map is held during this block
143149
let id = unsafe { *bucket.as_ref().1.get() };
144150

151+
// Sync the value's revision.
152+
let value = zalsa.table().get::<Value<C>>(id);
153+
value.last_interned_at.store(current_revision);
154+
145155
// Record a dependency on this value.
146156
let index = self.database_key_index(id);
147-
zalsa_local.report_tracked_read(index, Durability::MAX, Revision::start());
157+
zalsa_local.report_tracked_read(index, Durability::MAX, value.first_interned_at);
148158

149159
return C::struct_from_id(id);
150160
}
@@ -160,9 +170,13 @@ where
160170
let id = *entry.get();
161171
drop(entry);
162172

173+
// Sync the value's revision.
174+
let value = zalsa.table().get::<Value<C>>(id);
175+
value.last_interned_at.store(current_revision);
176+
163177
// Record a dependency on this value.
164178
let index = self.database_key_index(id);
165-
zalsa_local.report_tracked_read(index, Durability::MAX, Revision::start());
179+
zalsa_local.report_tracked_read(index, Durability::MAX, value.first_interned_at);
166180

167181
C::struct_from_id(id)
168182
}
@@ -175,12 +189,14 @@ where
175189
data: internal_data,
176190
memos: Default::default(),
177191
syncs: Default::default(),
192+
first_interned_at: current_revision,
193+
last_interned_at: AtomicRevision::from(current_revision),
178194
});
179195
entry.insert(next_id);
180196

181197
// Record a dependency on this value.
182198
let index = self.database_key_index(next_id);
183-
zalsa_local.report_tracked_read(index, Durability::MAX, Revision::start());
199+
zalsa_local.report_tracked_read(index, Durability::MAX, current_revision);
184200

185201
C::struct_from_id(next_id)
186202
}
@@ -199,15 +215,25 @@ where
199215
/// Rarely used since end-users generally carry a struct with a pointer directly
200216
/// to the interned item.
201217
pub fn data<'db>(&'db self, db: &'db dyn Database, id: Id) -> &'db C::Data<'db> {
202-
let internal_data = db.zalsa().table().get::<Value<C>>(id);
203-
unsafe { Self::from_internal_data(&internal_data.data) }
218+
let value = db.zalsa().table().get::<Value<C>>(id);
219+
assert!(
220+
value.last_interned_at.load() >= db.zalsa().current_revision(),
221+
"Data was not interned in the current revision."
222+
);
223+
unsafe { Self::from_internal_data(&value.data) }
204224
}
205225

206226
/// Lookup the fields from an interned struct.
207227
/// Note that this is not "leaking" since no dependency edge is required.
208228
pub fn fields<'db>(&'db self, db: &'db dyn Database, s: C::Struct<'db>) -> &'db C::Data<'db> {
209229
self.data(db, C::deref_struct(s))
210230
}
231+
232+
pub fn reset(&mut self, db: &mut dyn Database) {
233+
// Trigger a new revision.
234+
let _zalsa_mut = db.zalsa_mut();
235+
self.key_map.clear();
236+
}
211237
}
212238

213239
impl<C> Ingredient for IngredientImpl<C>
@@ -218,8 +244,18 @@ where
218244
self.ingredient_index
219245
}
220246

221-
fn maybe_changed_after(&self, _db: &dyn Database, _input: Id, _revision: Revision) -> bool {
222-
// Interned data currently never changes.
247+
fn maybe_changed_after(&self, db: &dyn Database, input: Id, revision: Revision) -> bool {
248+
let value = db.zalsa().table().get::<Value<C>>(input);
249+
if value.first_interned_at > revision {
250+
// The slot was reused.
251+
return true;
252+
}
253+
254+
if value.first_interned_at < revision {
255+
// The slot is valid in this revision but we have to sync the value's revision.
256+
value.last_interned_at.store(db.zalsa().current_revision());
257+
}
258+
223259
false
224260
}
225261

src/revision.rs

+8
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,11 @@ impl AtomicRevision {
6262
self.data.store(r.as_usize(), Ordering::SeqCst);
6363
}
6464
}
65+
66+
impl From<Revision> for AtomicRevision {
67+
fn from(value: Revision) -> Self {
68+
Self {
69+
data: AtomicUsize::from(value.as_usize()),
70+
}
71+
}
72+
}

src/tracked_struct.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ where
291291
match zalsa_local.tracked_struct_id(&identity) {
292292
Some(id) => {
293293
// The struct already exists in the intern map.
294-
zalsa_local.add_output(self.database_key_index(id).into());
294+
zalsa_local.add_output(self.database_key_index(id));
295295
self.update(zalsa, current_revision, id, &current_deps, fields);
296296
C::struct_from_id(id)
297297
}
@@ -300,7 +300,7 @@ where
300300
// This is a new tracked struct, so create an entry in the struct map.
301301
let id = self.allocate(zalsa, zalsa_local, current_revision, &current_deps, fields);
302302
let key = self.database_key_index(id);
303-
zalsa_local.add_output(key.into());
303+
zalsa_local.add_output(key);
304304
zalsa_local.store_tracked_struct_id(identity, id);
305305
C::struct_from_id(id)
306306
}

0 commit comments

Comments
 (0)