2018-09-30 14:34:52 +00:00
|
|
|
use crate::implementation::{TestContext, TestContextImpl};
|
2018-10-05 08:54:51 +00:00
|
|
|
use salsa::Database;
|
2018-09-30 14:34:52 +00:00
|
|
|
|
2018-10-05 14:35:50 +00:00
|
|
|
salsa::query_group! {
|
2018-10-09 19:34:30 +00:00
|
|
|
pub(crate) trait MemoizedVolatileContext: TestContext {
|
2018-09-30 14:34:52 +00:00
|
|
|
// Queries for testing a "volatile" value wrapped by
|
|
|
|
// memoization.
|
2018-10-05 09:28:51 +00:00
|
|
|
fn memoized2(key: ()) -> usize {
|
|
|
|
type Memoized2;
|
|
|
|
}
|
|
|
|
fn memoized1(key: ()) -> usize {
|
|
|
|
type Memoized1;
|
|
|
|
}
|
|
|
|
fn volatile(key: ()) -> usize {
|
|
|
|
type Volatile;
|
2018-10-05 09:51:18 +00:00
|
|
|
storage volatile;
|
2018-10-05 09:28:51 +00:00
|
|
|
}
|
2018-09-30 14:34:52 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-30 11:28:22 +00:00
|
|
|
|
2018-10-05 14:30:17 +00:00
|
|
|
fn memoized2(db: &impl MemoizedVolatileContext, (): ()) -> usize {
|
|
|
|
db.log().add("Memoized2 invoked");
|
|
|
|
db.memoized1(())
|
2018-09-30 14:34:52 +00:00
|
|
|
}
|
2018-09-30 11:28:22 +00:00
|
|
|
|
2018-10-05 14:30:17 +00:00
|
|
|
fn memoized1(db: &impl MemoizedVolatileContext, (): ()) -> usize {
|
|
|
|
db.log().add("Memoized1 invoked");
|
|
|
|
let v = db.volatile(());
|
|
|
|
v / 2
|
2018-09-30 14:34:52 +00:00
|
|
|
}
|
2018-09-30 11:28:22 +00:00
|
|
|
|
2018-10-05 14:30:17 +00:00
|
|
|
fn volatile(db: &impl MemoizedVolatileContext, (): ()) -> usize {
|
|
|
|
db.log().add("Volatile invoked");
|
|
|
|
db.clock().increment()
|
2018-09-30 10:59:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-09-30 11:28:22 +00:00
|
|
|
fn volatile_x2() {
|
2018-09-30 14:34:52 +00:00
|
|
|
let query = TestContextImpl::default();
|
2018-09-30 10:59:28 +00:00
|
|
|
|
2018-10-09 16:18:16 +00:00
|
|
|
// Invoking volatile twice doesn't execute twice, because volatile
|
|
|
|
// queries are memoized by default.
|
2018-10-05 09:28:51 +00:00
|
|
|
query.volatile(());
|
|
|
|
query.volatile(());
|
2018-10-09 16:18:16 +00:00
|
|
|
query.assert_log(&["Volatile invoked"]);
|
2018-09-30 10:59:28 +00:00
|
|
|
}
|
2018-09-30 11:28:22 +00:00
|
|
|
|
2018-09-30 11:32:24 +00:00
|
|
|
/// Test that:
|
|
|
|
///
|
|
|
|
/// - On the first run of R0, we recompute everything.
|
|
|
|
/// - On the second run of R1, we recompute nothing.
|
|
|
|
/// - On the first run of R1, we recompute Memoized1 but not Memoized2 (since Memoized1 result
|
|
|
|
/// did not change).
|
|
|
|
/// - On the second run of R1, we recompute nothing.
|
|
|
|
/// - On the first run of R2, we recompute everything (since Memoized1 result *did* change).
|
2018-09-30 11:28:22 +00:00
|
|
|
#[test]
|
2018-09-30 11:32:24 +00:00
|
|
|
fn revalidate() {
|
2018-09-30 14:34:52 +00:00
|
|
|
let query = TestContextImpl::default();
|
2018-09-30 11:28:22 +00:00
|
|
|
|
2018-10-05 09:28:51 +00:00
|
|
|
query.memoized2(());
|
2018-09-30 11:28:22 +00:00
|
|
|
query.assert_log(&["Memoized2 invoked", "Memoized1 invoked", "Volatile invoked"]);
|
|
|
|
|
2018-10-05 09:28:51 +00:00
|
|
|
query.memoized2(());
|
2018-09-30 11:28:22 +00:00
|
|
|
query.assert_log(&[]);
|
|
|
|
|
2018-09-30 11:32:24 +00:00
|
|
|
// Second generation: volatile will change (to 1) but memoized1
|
|
|
|
// will not (still 0, as 1/2 = 0)
|
2018-09-30 11:28:22 +00:00
|
|
|
query.salsa_runtime().next_revision();
|
|
|
|
|
2018-10-05 09:28:51 +00:00
|
|
|
query.memoized2(());
|
2018-10-09 16:18:16 +00:00
|
|
|
query.assert_log(&["Volatile invoked", "Memoized1 invoked"]);
|
2018-09-30 11:32:24 +00:00
|
|
|
|
2018-10-05 09:28:51 +00:00
|
|
|
query.memoized2(());
|
2018-09-30 11:32:24 +00:00
|
|
|
query.assert_log(&[]);
|
|
|
|
|
|
|
|
// Third generation: volatile will change (to 2) and memoized1
|
|
|
|
// will too (to 1). Therefore, after validating that Memoized1
|
|
|
|
// changed, we now invoke Memoized2.
|
|
|
|
query.salsa_runtime().next_revision();
|
|
|
|
|
2018-10-05 09:28:51 +00:00
|
|
|
query.memoized2(());
|
2018-10-09 16:18:16 +00:00
|
|
|
query.assert_log(&["Volatile invoked", "Memoized1 invoked", "Memoized2 invoked"]);
|
2018-09-30 11:32:24 +00:00
|
|
|
|
2018-10-05 09:28:51 +00:00
|
|
|
query.memoized2(());
|
2018-09-30 11:32:24 +00:00
|
|
|
query.assert_log(&[]);
|
2018-09-30 11:28:22 +00:00
|
|
|
}
|