Skip to content

Commit a232751

Browse files
committed
add read dependency on reused interned values
1 parent 64e0849 commit a232751

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

src/interned.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ where
341341
.unwrap_or((Durability::MAX, Revision::max()));
342342

343343
let value = value.shared.get_mut().with(|value_shared| {
344-
// SAFETY: We hold the lock and the value has not been read in this revision.
344+
// SAFETY: We hold the lock.
345345
let value_shared = unsafe { &mut *value_shared };
346346

347347
// Mark the slot as reused.
@@ -354,13 +354,31 @@ where
354354
unsafe { &*UnsafeRef::into_raw(cursor.remove().unwrap()) }
355355
});
356356

357-
// SAFETY: We hold the lock.
358-
let id = value.shared.with_mut(|value_shared| unsafe {
357+
let id = value.shared.with_mut(|value_shared| {
358+
// SAFETY: We hold the lock.
359+
let value_shared = unsafe { &mut *value_shared };
360+
359361
// Note we need to retain the previous durability here to ensure queries trying
360362
// to read the old value are revalidated.
361-
(*value_shared).durability =
362-
std::cmp::max((*value_shared).durability, durability);
363-
(*value_shared).id
363+
value_shared.durability = std::cmp::max(value_shared.durability, durability);
364+
365+
let index = self.database_key_index(value_shared.id);
366+
367+
// Record a dependency on the value.
368+
zalsa_local.report_tracked_read_simple(
369+
index,
370+
value_shared.durability,
371+
value_shared.first_interned_at,
372+
);
373+
374+
zalsa.event(&|| {
375+
Event::new(EventKind::DidReuseInternedValue {
376+
key: index,
377+
revision: current_revision,
378+
})
379+
});
380+
381+
value_shared.id
364382
});
365383

366384
// Reuse the value slot with the new data.
@@ -371,20 +389,15 @@ where
371389
*fields = self.to_internal_data(assemble(id, key));
372390
});
373391

392+
// TODO: Need to free the memory safely here.
393+
value.memos.clear();
394+
374395
// Move the value to the front of the LRU list.
375396
//
376397
// SAFETY: The value pointer is valid for the lifetime of the database
377398
// and only accessed mutably while holding the lock.
378399
shared.lru.push_front(unsafe { UnsafeRef::from_raw(value) });
379400

380-
let index = self.database_key_index(id);
381-
zalsa.event(&|| {
382-
Event::new(EventKind::DidReuseInternedValue {
383-
key: index,
384-
revision: current_revision,
385-
})
386-
});
387-
388401
return id;
389402
}
390403
}

src/table/memo.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ pub(crate) struct MemoTable {
1919
memos: RwLock<ThinVec<MemoEntry>>,
2020
}
2121

22+
impl MemoTable {
23+
pub(crate) fn clear(&self) {
24+
self.memos.write().clear();
25+
}
26+
}
27+
2228
pub trait Memo: Any + Send + Sync {
2329
/// Returns the `origin` of this memo
2430
fn origin(&self) -> &QueryOrigin;

0 commit comments

Comments
 (0)