make interned keys have durability high

This commit is contained in:
Niko Matsakis 2019-06-25 21:50:00 -04:00
parent 9d474363fc
commit ac93c950be
2 changed files with 99 additions and 12 deletions

View file

@ -1,5 +1,6 @@
use crate::db;
use salsa::{Database, InternId, SweepStrategy};
use salsa::debug::DebugQueryTable;
use salsa::{Database, Durability, InternId, SweepStrategy};
/// Query group for tests for how interned keys interact with GC.
#[salsa::query_group(Intern)]
@ -73,13 +74,13 @@ fn discard_during_same_revision() {
/// exercises precisely that scenario.
#[test]
fn discard_outdated() {
let mut db = db::DatabaseImpl::default();
let db = db::DatabaseImpl::default();
let foo_from_rev0 = db.repeat_intern1("foo");
let bar_from_rev0 = db.repeat_intern1("bar");
// Trigger a new revision.
db.set_dummy(());
db.salsa_runtime().synthetic_write(Durability::HIGH);
// In this revision, we use "bar".
let bar_from_rev1 = db.repeat_intern1("bar");
@ -110,3 +111,84 @@ fn discard_outdated() {
assert_eq!(db.lookup_intern_str(bar_from_rev1), "bar");
assert_eq!(db.lookup_intern_str(baz_from_rev1), "baz");
}
/// Variation on `discard_during_same_revision` --- here we show that
/// a synthetic write of level LOW isn't enough to collect interned
/// keys (which are considered durability HIGH).
#[test]
fn discard_durability_after_synthetic_write_low() {
let db = db::DatabaseImpl::default();
// This will assign index 0 for "foo".
let foo1a = db.repeat_intern1("foo");
assert_eq!(
Durability::HIGH,
db.query(RepeatIntern1Query).durability("foo")
);
// Trigger a new revision.
db.salsa_runtime().synthetic_write(Durability::LOW);
// If we are not careful, this would remove the interned key for
// "foo".
db.query(InternStrQuery).sweep(
SweepStrategy::default()
.discard_everything()
.sweep_all_revisions(),
);
// This would then reuse index 0 for "bar".
let bar1 = db.intern_str("bar");
// And here we would assign index *1* to "foo".
let foo2 = db.repeat_intern2("foo");
// But we would still have a cached result with the value 0 and
// with high durability, so we can reuse it. That gives an
// inconsistent result.
let foo1b = db.repeat_intern1("foo");
assert_ne!(foo2, bar1);
assert_eq!(foo1a, foo1b);
assert_eq!(foo1b, foo2);
}
/// Variation on previous test in which we do a synthetic write to
/// `Durability::HIGH`.
#[test]
fn discard_durability_after_synthetic_write_high() {
let db = db::DatabaseImpl::default();
// This will assign index 0 for "foo".
let foo1a = db.repeat_intern1("foo");
assert_eq!(
Durability::HIGH,
db.query(RepeatIntern1Query).durability("foo")
);
// Trigger a new revision -- marking even high things as having changed.
db.salsa_runtime().synthetic_write(Durability::HIGH);
// We are now able to collect "collect".
db.query(InternStrQuery).sweep(
SweepStrategy::default()
.discard_everything()
.sweep_all_revisions(),
);
// So we can reuse index 0 for "bar".
let bar1 = db.intern_str("bar");
// And here we assign index *1* to "foo".
let foo2 = db.repeat_intern2("foo");
let foo1b = db.repeat_intern1("foo");
// Thus foo1a (from before the synthetic write) and foo1b (from
// after) are different.
assert_ne!(foo1a, foo1b);
// But the things that come after the synthetic write are
// consistent.
assert_ne!(foo2, bar1);
assert_eq!(foo1b, foo2);
}