Skip to content

Commit 6505453

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

8 files changed

+132
-18
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-3
Original file line numberDiff line numberDiff line change
@@ -39,8 +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();
43-
if !zalsa_local.is_output_of_active_query(dependency_index) {
42+
if !zalsa_local.is_output_of_active_query(database_key_index) {
4443
panic!("can only use `specify` on salsa structs created during the current tracked fn");
4544
}
4645

@@ -92,7 +91,7 @@ where
9291

9392
// Record that the current query *specified* a value for this cell.
9493
let database_key_index = self.database_key_index(key);
95-
zalsa_local.add_output(database_key_index.into());
94+
zalsa_local.add_output(database_key_index);
9695
}
9796

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

src/interned.rs

+45-10
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, current_revision);
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, current_revision);
166180

167181
C::struct_from_id(id)
168182
}
@@ -171,43 +185,56 @@ where
171185
dashmap::mapref::entry::Entry::Vacant(entry) => {
172186
let zalsa = db.zalsa();
173187
let table = zalsa.table();
188+
174189
let next_id = zalsa_local.allocate(table, self.ingredient_index, || Value::<C> {
175190
data: internal_data,
176191
memos: Default::default(),
177192
syncs: Default::default(),
193+
first_interned_at: current_revision,
194+
last_interned_at: AtomicRevision::from(current_revision),
178195
});
179196
entry.insert(next_id);
180197

181198
// Record a dependency on this value.
182199
let index = self.database_key_index(next_id);
183-
zalsa_local.report_tracked_read(index, Durability::MAX, Revision::start());
200+
zalsa_local.report_tracked_read(index, Durability::MAX, current_revision);
184201

185202
C::struct_from_id(next_id)
186203
}
187204
}
188205
}
189206

190207
/// Returns the database key index for an interned value with the given id.
191-
pub fn database_key_index(&self, id: Id) -> DatabaseKeyIndex {
208+
pub fn database_key_index(&self, key_index: Id) -> DatabaseKeyIndex {
192209
DatabaseKeyIndex {
210+
key_index,
193211
ingredient_index: self.ingredient_index,
194-
key_index: id,
195212
}
196213
}
197214

198215
/// Lookup the data for an interned value based on its id.
199216
/// Rarely used since end-users generally carry a struct with a pointer directly
200217
/// to the interned item.
201218
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) }
219+
let value = db.zalsa().table().get::<Value<C>>(id);
220+
assert!(
221+
value.last_interned_at.load() >= db.zalsa().current_revision(),
222+
"Data was not interned in the current revision."
223+
);
224+
unsafe { Self::from_internal_data(&value.data) }
204225
}
205226

206227
/// Lookup the fields from an interned struct.
207228
/// Note that this is not "leaking" since no dependency edge is required.
208229
pub fn fields<'db>(&'db self, db: &'db dyn Database, s: C::Struct<'db>) -> &'db C::Data<'db> {
209230
self.data(db, C::deref_struct(s))
210231
}
232+
233+
pub fn reset(&mut self, db: &mut dyn Database) {
234+
// Trigger a new revision.
235+
let _zalsa_mut = db.zalsa_mut();
236+
self.key_map.clear();
237+
}
211238
}
212239

213240
impl<C> Ingredient for IngredientImpl<C>
@@ -218,8 +245,16 @@ where
218245
self.ingredient_index
219246
}
220247

221-
fn maybe_changed_after(&self, _db: &dyn Database, _input: Id, _revision: Revision) -> bool {
222-
// Interned data currently never changes.
248+
fn maybe_changed_after(&self, db: &dyn Database, input: Id, revision: Revision) -> bool {
249+
let value = db.zalsa().table().get::<Value<C>>(input);
250+
if value.first_interned_at > revision {
251+
// The slot was reused.
252+
return true;
253+
}
254+
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+
223258
false
224259
}
225260

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
}

tests/interned-revisions.rs

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//! Test that a `tracked` fn on a `salsa::input`
2+
//! compiles and executes successfully.
3+
4+
mod common;
5+
use salsa::{Database, Setter};
6+
use test_log::test;
7+
8+
#[salsa::input]
9+
struct Input {
10+
field1: usize,
11+
}
12+
13+
#[salsa::interned]
14+
struct Interned<'db> {
15+
field1: usize,
16+
}
17+
18+
#[test]
19+
fn test_shallow_memo() {
20+
#[salsa::tracked]
21+
fn function<'db>(db: &'db dyn Database, _input: Input) -> Interned<'db> {
22+
Interned::new(db, 0)
23+
}
24+
25+
let mut db = common::EventLoggerDatabase::default();
26+
let input = Input::new(&db, 0);
27+
28+
let result_in_rev_1 = function(&db, input);
29+
assert_eq!(result_in_rev_1.field1(&db), 0);
30+
31+
input.set_field1(&mut db).to(1);
32+
33+
let result_in_rev_2 = function(&db, input);
34+
assert_eq!(result_in_rev_2.field1(&db), 0);
35+
}
36+
37+
// #[test]
38+
// fn test_no_reintern_input() {
39+
// #[salsa::tracked]
40+
// fn function<'db>(db: &'db dyn Database, input: Input) -> Interned<'db> {
41+
// function2(db, input.field1(db))
42+
// }
43+
//
44+
// fn function2<'db>(db: &'db dyn Database, value: usize) -> Interned<'db> {
45+
// Interned::new(db, value)
46+
// }
47+
//
48+
// let mut db = common::EventLoggerDatabase::default();
49+
//
50+
// let input = Input::new(&db, 0);
51+
// let result_in_rev_1 = function(&db, input);
52+
// db.assert_logs(expect![[r#"
53+
// [
54+
// "Event { thread_id: ThreadId(3), kind: WillCheckCancellation }",
55+
// "Event { thread_id: ThreadId(3), kind: WillExecute { database_key: function(Id(0)) } }",
56+
// ]"#]]);
57+
//
58+
// assert_eq!(result_in_rev_1.field1(&db), 0);
59+
//
60+
// // Modify the input to force the value to be re-interned.
61+
// input.set_field1(&mut db).to(1);
62+
//
63+
// let result_in_rev_2 = function(&db, input);
64+
// db.assert_logs(expect![[r#"
65+
// [
66+
// "Event { thread_id: ThreadId(3), kind: DidSetCancellationFlag }",
67+
// "Event { thread_id: ThreadId(3), kind: WillCheckCancellation }",
68+
// "Event { thread_id: ThreadId(3), kind: WillExecute { database_key: function(Id(0)) } }",
69+
// ]"#]]);
70+
//
71+
// assert_eq!(result_in_rev_2.field1(&db), 1);
72+
// }

tests/preverify-struct-with-leaked-data-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,6 @@ fn test_leaked_inputs_ignored() {
9595
// value of 100 since the struct has already been read during
9696
// this revision.
9797
//
98-
// Contrast with preverify-struct-with-leaked-data-2.rs.
98+
// Contrast with preverify-struct-with-leaked-data.rs.
9999
assert_eq!(result_in_rev_2, (0, 0));
100100
}

0 commit comments

Comments
 (0)