add synthetic write

This commit is contained in:
XFFXFF 2022-08-23 07:10:34 +08:00
parent 5ce4662b81
commit 340e2d521d
4 changed files with 35 additions and 8 deletions

View file

@ -12,6 +12,8 @@ pub trait Database: HasJarsDyn + AsSalsaDatabase {
} }
fn salsa_runtime(&self) -> &Runtime; fn salsa_runtime(&self) -> &Runtime;
fn salsa_runtime_mut(&mut self) -> &mut Runtime;
} }
/// Indicates a database that also supports parallel query /// Indicates a database that also supports parallel query

View file

@ -144,6 +144,19 @@ impl Runtime {
} }
} }
/// A "synthetic write" causes the system to act *as though* some
/// input of durability `durability` has changed. This is mostly
/// useful for profiling scenarios.
///
/// **WARNING:** Just like an ordinary write, this method triggers
/// cancellation. If you invoke it while a snapshot exists, it
/// will block until that snapshot is dropped -- if that snapshot
/// is owned by the current thread, this could trigger deadlock.
pub fn synthetic_write(&mut self, durability: Durability) {
self.new_revision();
self.report_tracked_write(durability)
}
/// Adds `key` to the list of output created by the current query /// Adds `key` to the list of output created by the current query
/// (if not already present). /// (if not already present).
pub(crate) fn add_output(&self, key: DependencyIndex) { pub(crate) fn add_output(&self, key: DependencyIndex) {

View file

@ -93,6 +93,10 @@ where
&self.runtime &self.runtime
} }
pub fn runtime_mut(&mut self) -> &mut Runtime {
&mut self.runtime
}
// ANCHOR: jars_mut // ANCHOR: jars_mut
/// Gets mutable access to the jars. This will trigger a new revision /// Gets mutable access to the jars. This will trigger a new revision
/// and it will also cancel any ongoing work in the current revision. /// and it will also cancel any ongoing work in the current revision.

View file

@ -6,6 +6,7 @@ use std::sync::{
Arc, Arc,
}; };
use salsa::Database;
use salsa_2022_tests::{HasLogger, Logger}; use salsa_2022_tests::{HasLogger, Logger};
use test_log::test; use test_log::test;
@ -60,20 +61,25 @@ fn get_volatile(db: &dyn Db, _input: MyInput) -> usize {
#[salsa::db(Jar)] #[salsa::db(Jar)]
#[derive(Default)] #[derive(Default)]
struct Database { struct DatabaseImpl {
storage: salsa::Storage<Self>, storage: salsa::Storage<Self>,
logger: Logger, logger: Logger,
} }
impl salsa::Database for Database { impl salsa::Database for DatabaseImpl {
fn salsa_runtime(&self) -> &salsa::Runtime { fn salsa_runtime(&self) -> &salsa::Runtime {
self.storage.runtime() self.storage.runtime()
} }
fn salsa_runtime_mut(&mut self) -> &mut salsa::Runtime {
self.storage.runtime_mut()
}
} }
impl Db for Database {} impl Db for DatabaseImpl {}
impl HasLogger for Database { impl HasLogger for DatabaseImpl {
fn logger(&self) -> &Logger { fn logger(&self) -> &Logger {
&self.logger &self.logger
} }
@ -85,7 +91,7 @@ fn load_n_potatoes() -> usize {
#[test] #[test]
fn lru_works() { fn lru_works() {
let mut db = Database::default(); let mut db = DatabaseImpl::default();
assert_eq!(load_n_potatoes(), 0); assert_eq!(load_n_potatoes(), 0);
for i in 0..128u32 { for i in 0..128u32 {
@ -101,7 +107,7 @@ fn lru_works() {
#[test] #[test]
fn lru_doesnt_break_volatile_queries() { fn lru_doesnt_break_volatile_queries() {
let mut db = Database::default(); let mut db = DatabaseImpl::default();
// Create all inputs first, so that there are no revision changes among calls to `get_volatile` // Create all inputs first, so that there are no revision changes among calls to `get_volatile`
let inputs: Vec<MyInput> = (0..128usize) let inputs: Vec<MyInput> = (0..128usize)
@ -121,7 +127,7 @@ fn lru_doesnt_break_volatile_queries() {
#[test] #[test]
fn lru_can_be_changed_at_runtime() { fn lru_can_be_changed_at_runtime() {
let mut db = Database::default(); let mut db = DatabaseImpl::default();
assert_eq!(load_n_potatoes(), 0); assert_eq!(load_n_potatoes(), 0);
let inputs: Vec<(u32, MyInput)> = (0..128).map(|i| (i, MyInput::new(&mut db, i))).collect(); let inputs: Vec<(u32, MyInput)> = (0..128).map(|i| (i, MyInput::new(&mut db, i))).collect();
@ -164,7 +170,7 @@ fn lru_can_be_changed_at_runtime() {
#[test] #[test]
fn lru_keeps_dependency_info() { fn lru_keeps_dependency_info() {
let mut db = Database::default(); let mut db = DatabaseImpl::default();
let capacity = 32; let capacity = 32;
// Invoke `get_hot_potato2` 33 times. This will (in turn) invoke // Invoke `get_hot_potato2` 33 times. This will (in turn) invoke
@ -178,6 +184,8 @@ fn lru_keeps_dependency_info() {
assert_eq!(x as usize, i); assert_eq!(x as usize, i);
} }
db.salsa_runtime_mut().synthetic_write(salsa::Durability::HIGH);
// We want to test that calls to `get_hot_potato2` are still considered // We want to test that calls to `get_hot_potato2` are still considered
// clean. Check that no new executions occur as we go here. // clean. Check that no new executions occur as we go here.
db.assert_logs_len((capacity + 1) * 2); db.assert_logs_len((capacity + 1) * 2);