2018-10-01 12:23:18 +00:00
|
|
|
use crate::implementation::{TestContext, TestContextImpl};
|
2018-10-05 09:28:51 +00:00
|
|
|
use salsa::Query;
|
2018-10-01 12:23:18 +00:00
|
|
|
|
2018-10-02 00:42:41 +00:00
|
|
|
salsa::query_prototype! {
|
|
|
|
crate trait MemoizedDepInputsContext: TestContext {
|
2018-10-05 09:28:51 +00:00
|
|
|
fn dep_memoized2(key: ()) -> usize {
|
|
|
|
type Memoized2;
|
|
|
|
}
|
|
|
|
fn dep_memoized1(key: ()) -> usize {
|
|
|
|
type Memoized1;
|
|
|
|
}
|
|
|
|
fn dep_derived1(key: ()) -> usize {
|
|
|
|
type Derived1;
|
2018-10-05 09:51:18 +00:00
|
|
|
storage dependencies;
|
2018-10-05 09:28:51 +00:00
|
|
|
}
|
|
|
|
fn dep_input1(key: ()) -> usize {
|
|
|
|
type Input1;
|
2018-10-05 09:51:18 +00:00
|
|
|
storage input;
|
2018-10-05 09:28:51 +00:00
|
|
|
}
|
|
|
|
fn dep_input2(key: ()) -> usize {
|
|
|
|
type Input2;
|
2018-10-05 09:51:18 +00:00
|
|
|
storage input;
|
2018-10-05 09:28:51 +00:00
|
|
|
}
|
2018-10-01 12:23:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 09:51:18 +00:00
|
|
|
impl<DB: MemoizedDepInputsContext> salsa::QueryFunction<DB> for Memoized2 {
|
|
|
|
fn execute(db: &DB, (): ()) -> usize {
|
2018-10-05 08:54:51 +00:00
|
|
|
db.log().add("Memoized2 invoked");
|
2018-10-05 09:28:51 +00:00
|
|
|
db.dep_memoized1(())
|
2018-10-01 12:23:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 09:51:18 +00:00
|
|
|
impl<DB: MemoizedDepInputsContext> salsa::QueryFunction<DB> for Memoized1 {
|
|
|
|
fn execute(db: &DB, (): ()) -> usize {
|
2018-10-05 08:54:51 +00:00
|
|
|
db.log().add("Memoized1 invoked");
|
2018-10-05 09:28:51 +00:00
|
|
|
db.dep_derived1(()) * 2
|
2018-10-01 12:23:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 09:51:18 +00:00
|
|
|
impl<DB: MemoizedDepInputsContext> salsa::QueryFunction<DB> for Derived1 {
|
|
|
|
fn execute(db: &DB, (): ()) -> usize {
|
2018-10-05 08:54:51 +00:00
|
|
|
db.log().add("Derived1 invoked");
|
2018-10-05 09:28:51 +00:00
|
|
|
db.dep_input1(()) / 2
|
2018-10-01 12:23:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn revalidate() {
|
2018-10-05 09:28:51 +00:00
|
|
|
let db = &TestContextImpl::default();
|
2018-10-01 12:23:18 +00:00
|
|
|
|
|
|
|
// Initial run starts from Memoized2:
|
2018-10-05 09:28:51 +00:00
|
|
|
let v = db.dep_memoized2(());
|
2018-10-01 12:23:18 +00:00
|
|
|
assert_eq!(v, 0);
|
2018-10-05 09:28:51 +00:00
|
|
|
db.assert_log(&["Memoized2 invoked", "Memoized1 invoked", "Derived1 invoked"]);
|
2018-10-01 12:23:18 +00:00
|
|
|
|
|
|
|
// After that, we first try to validate Memoized1 but wind up
|
|
|
|
// running Memoized2. Note that we don't try to validate
|
|
|
|
// Derived1, so it is invoked by Memoized1.
|
2018-10-05 09:28:51 +00:00
|
|
|
Input1.set(db, (), 44);
|
|
|
|
let v = db.dep_memoized2(());
|
2018-10-01 12:23:18 +00:00
|
|
|
assert_eq!(v, 44);
|
2018-10-05 09:28:51 +00:00
|
|
|
db.assert_log(&["Memoized1 invoked", "Derived1 invoked", "Memoized2 invoked"]);
|
2018-10-01 12:23:18 +00:00
|
|
|
|
|
|
|
// Here validation of Memoized1 succeeds so Memoized2 never runs.
|
2018-10-05 09:28:51 +00:00
|
|
|
Input1.set(db, (), 45);
|
|
|
|
let v = db.dep_memoized2(());
|
2018-10-01 12:23:18 +00:00
|
|
|
assert_eq!(v, 44);
|
2018-10-05 09:28:51 +00:00
|
|
|
db.assert_log(&["Memoized1 invoked", "Derived1 invoked"]);
|
2018-10-01 12:23:18 +00:00
|
|
|
|
|
|
|
// Here, a change to input2 doesn't affect us, so nothing runs.
|
2018-10-05 09:28:51 +00:00
|
|
|
Input2.set(db, (), 45);
|
|
|
|
let v = db.dep_memoized2(());
|
2018-10-01 12:23:18 +00:00
|
|
|
assert_eq!(v, 44);
|
2018-10-05 09:28:51 +00:00
|
|
|
db.assert_log(&[]);
|
2018-10-01 12:23:18 +00:00
|
|
|
}
|