2018-09-30 14:34:52 +00:00
|
|
|
use crate::implementation::{TestContext, TestContextImpl};
|
2019-06-25 22:02:56 +00:00
|
|
|
use salsa::{Database, Durability};
|
2018-09-30 14:34:52 +00:00
|
|
|
|
2019-01-25 15:25:17 +00:00
|
|
|
#[salsa::query_group(MemoizedVolatile)]
|
2019-01-12 10:11:59 +00:00
|
|
|
pub(crate) trait MemoizedVolatileContext: TestContext {
|
|
|
|
// Queries for testing a "volatile" value wrapped by
|
|
|
|
// memoization.
|
|
|
|
fn memoized2(&self) -> usize;
|
|
|
|
fn memoized1(&self) -> usize;
|
|
|
|
fn volatile(&self) -> usize;
|
2018-09-30 14:34:52 +00:00
|
|
|
}
|
2018-09-30 11:28:22 +00:00
|
|
|
|
2020-07-03 10:46:00 +00:00
|
|
|
fn memoized2(db: &dyn MemoizedVolatileContext) -> usize {
|
2018-10-05 14:30:17 +00:00
|
|
|
db.log().add("Memoized2 invoked");
|
2018-10-19 01:26:48 +00:00
|
|
|
db.memoized1()
|
2018-09-30 14:34:52 +00:00
|
|
|
}
|
2018-09-30 11:28:22 +00:00
|
|
|
|
2020-07-03 10:46:00 +00:00
|
|
|
fn memoized1(db: &dyn MemoizedVolatileContext) -> usize {
|
2018-10-05 14:30:17 +00:00
|
|
|
db.log().add("Memoized1 invoked");
|
2018-10-19 01:26:48 +00:00
|
|
|
let v = db.volatile();
|
2018-10-05 14:30:17 +00:00
|
|
|
v / 2
|
2018-09-30 14:34:52 +00:00
|
|
|
}
|
2018-09-30 11:28:22 +00:00
|
|
|
|
2020-07-03 10:46:00 +00:00
|
|
|
fn volatile(db: &dyn MemoizedVolatileContext) -> usize {
|
2018-10-05 14:30:17 +00:00
|
|
|
db.log().add("Volatile invoked");
|
2019-06-19 16:59:03 +00:00
|
|
|
db.salsa_runtime().report_untracked_read();
|
2018-10-05 14:30:17 +00:00
|
|
|
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-19 01:26:48 +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() {
|
2019-09-27 09:50:16 +00:00
|
|
|
let mut query = TestContextImpl::default();
|
2018-09-30 11:28:22 +00:00
|
|
|
|
2018-10-19 01:26:48 +00:00
|
|
|
query.memoized2();
|
2018-09-30 11:28:22 +00:00
|
|
|
query.assert_log(&["Memoized2 invoked", "Memoized1 invoked", "Volatile invoked"]);
|
|
|
|
|
2018-10-19 01:26:48 +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)
|
2019-09-27 09:50:16 +00:00
|
|
|
query.salsa_runtime_mut().synthetic_write(Durability::LOW);
|
2018-10-19 01:26:48 +00:00
|
|
|
query.memoized2();
|
2019-01-04 13:39:13 +00:00
|
|
|
query.assert_log(&["Memoized1 invoked", "Volatile invoked"]);
|
2018-10-19 01:26:48 +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.
|
2019-09-27 09:50:16 +00:00
|
|
|
query.salsa_runtime_mut().synthetic_write(Durability::LOW);
|
2018-09-30 11:32:24 +00:00
|
|
|
|
2018-10-19 01:26:48 +00:00
|
|
|
query.memoized2();
|
2019-01-04 13:39:13 +00:00
|
|
|
query.assert_log(&["Memoized1 invoked", "Volatile invoked", "Memoized2 invoked"]);
|
2018-09-30 11:32:24 +00:00
|
|
|
|
2018-10-19 01:26:48 +00:00
|
|
|
query.memoized2();
|
2018-09-30 11:32:24 +00:00
|
|
|
query.assert_log(&[]);
|
2018-09-30 11:28:22 +00:00
|
|
|
}
|