From 340e2d521d12e39b5a82532bc3dcc8ee11a6f580 Mon Sep 17 00:00:00 2001 From: XFFXFF <1247714429@qq.com> Date: Tue, 23 Aug 2022 07:10:34 +0800 Subject: [PATCH] add synthetic write --- components/salsa-2022/src/database.rs | 2 ++ components/salsa-2022/src/runtime.rs | 13 +++++++++++++ components/salsa-2022/src/storage.rs | 4 ++++ salsa-2022-tests/tests/lru.rs | 24 ++++++++++++++++-------- 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/components/salsa-2022/src/database.rs b/components/salsa-2022/src/database.rs index 15104844..687f2d3f 100644 --- a/components/salsa-2022/src/database.rs +++ b/components/salsa-2022/src/database.rs @@ -12,6 +12,8 @@ pub trait Database: HasJarsDyn + AsSalsaDatabase { } fn salsa_runtime(&self) -> &Runtime; + + fn salsa_runtime_mut(&mut self) -> &mut Runtime; } /// Indicates a database that also supports parallel query diff --git a/components/salsa-2022/src/runtime.rs b/components/salsa-2022/src/runtime.rs index c4a03b58..aa0895cf 100644 --- a/components/salsa-2022/src/runtime.rs +++ b/components/salsa-2022/src/runtime.rs @@ -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 /// (if not already present). pub(crate) fn add_output(&self, key: DependencyIndex) { diff --git a/components/salsa-2022/src/storage.rs b/components/salsa-2022/src/storage.rs index 83e3d617..bc5dfc08 100644 --- a/components/salsa-2022/src/storage.rs +++ b/components/salsa-2022/src/storage.rs @@ -93,6 +93,10 @@ where &self.runtime } + pub fn runtime_mut(&mut self) -> &mut Runtime { + &mut self.runtime + } + // ANCHOR: jars_mut /// Gets mutable access to the jars. This will trigger a new revision /// and it will also cancel any ongoing work in the current revision. diff --git a/salsa-2022-tests/tests/lru.rs b/salsa-2022-tests/tests/lru.rs index bbb041b3..c15a7dac 100644 --- a/salsa-2022-tests/tests/lru.rs +++ b/salsa-2022-tests/tests/lru.rs @@ -6,6 +6,7 @@ use std::sync::{ Arc, }; +use salsa::Database; use salsa_2022_tests::{HasLogger, Logger}; use test_log::test; @@ -60,20 +61,25 @@ fn get_volatile(db: &dyn Db, _input: MyInput) -> usize { #[salsa::db(Jar)] #[derive(Default)] -struct Database { +struct DatabaseImpl { storage: salsa::Storage, logger: Logger, } -impl salsa::Database for Database { +impl salsa::Database for DatabaseImpl { fn salsa_runtime(&self) -> &salsa::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 { &self.logger } @@ -85,7 +91,7 @@ fn load_n_potatoes() -> usize { #[test] fn lru_works() { - let mut db = Database::default(); + let mut db = DatabaseImpl::default(); assert_eq!(load_n_potatoes(), 0); for i in 0..128u32 { @@ -101,7 +107,7 @@ fn lru_works() { #[test] 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` let inputs: Vec = (0..128usize) @@ -121,7 +127,7 @@ fn lru_doesnt_break_volatile_queries() { #[test] fn lru_can_be_changed_at_runtime() { - let mut db = Database::default(); + let mut db = DatabaseImpl::default(); assert_eq!(load_n_potatoes(), 0); 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] fn lru_keeps_dependency_info() { - let mut db = Database::default(); + let mut db = DatabaseImpl::default(); let capacity = 32; // 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); } + db.salsa_runtime_mut().synthetic_write(salsa::Durability::HIGH); + // We want to test that calls to `get_hot_potato2` are still considered // clean. Check that no new executions occur as we go here. db.assert_logs_len((capacity + 1) * 2);