Skip to content

Commit

Permalink
Remove ops_salsa_runtime_mut, replace it with direct synthetic_write API
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Feb 22, 2024
1 parent 543d7e9 commit f89d17b
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion crates/ide-db/src/apply_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl RootDatabase {
pub fn request_cancellation(&mut self) {
let _p =
tracing::span!(tracing::Level::INFO, "RootDatabase::request_cancellation").entered();
self.salsa_runtime_mut().synthetic_write(Durability::LOW);
self.synthetic_write(Durability::LOW);
}

pub fn apply_change(&mut self, change: Change) {
Expand Down
4 changes: 2 additions & 2 deletions crates/salsa/salsa-macros/src/database_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ pub(crate) fn database(args: TokenStream, input: TokenStream) -> TokenStream {
self.#db_storage_field.salsa_runtime()
}

fn ops_salsa_runtime_mut(&mut self) -> &mut salsa::Runtime {
self.#db_storage_field.salsa_runtime_mut()
fn synthetic_write(&mut self, durability: salsa::Durability) {
self.#db_storage_field.salsa_runtime_mut().synthetic_write(durability)
}

fn fmt_index(
Expand Down
13 changes: 9 additions & 4 deletions crates/salsa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,16 @@ pub trait Database: plumbing::DatabaseOps {
self.ops_salsa_runtime()
}

/// Gives access to the underlying salsa 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.
///
/// This method should not be overridden by `Database` implementors.
fn salsa_runtime_mut(&mut self) -> &mut Runtime {
self.ops_salsa_runtime_mut()
/// **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.
fn synthetic_write(&mut self, durability: Durability) {
plumbing::DatabaseOps::synthetic_write(self, durability)
}
}

Expand Down
11 changes: 9 additions & 2 deletions crates/salsa/src/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,15 @@ pub trait DatabaseOps {
/// Gives access to the underlying salsa runtime.
fn ops_salsa_runtime(&self) -> &Runtime;

/// Gives access to the underlying salsa runtime.
fn ops_salsa_runtime_mut(&mut self) -> &mut 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.
fn synthetic_write(&mut self, durability: Durability);

/// Formats a database key index in a human readable fashion.
fn fmt_index(
Expand Down
4 changes: 2 additions & 2 deletions crates/salsa/tests/incremental/memoized_volatile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn revalidate() {

// Second generation: volatile will change (to 1) but memoized1
// will not (still 0, as 1/2 = 0)
query.salsa_runtime_mut().synthetic_write(Durability::LOW);
query.synthetic_write(Durability::LOW);
query.memoized2();
query.assert_log(&["Volatile invoked", "Memoized1 invoked"]);
query.memoized2();
Expand All @@ -67,7 +67,7 @@ fn revalidate() {
// 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_mut().synthetic_write(Durability::LOW);
query.synthetic_write(Durability::LOW);

query.memoized2();
query.assert_log(&["Volatile invoked", "Memoized1 invoked", "Memoized2 invoked"]);
Expand Down
4 changes: 2 additions & 2 deletions crates/salsa/tests/on_demand_inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ fn on_demand_input_durability() {
}
"#]].assert_debug_eq(&events);

db.salsa_runtime_mut().synthetic_write(Durability::LOW);
db.synthetic_write(Durability::LOW);
events.replace(vec![]);
assert_eq!(db.c(1), 10);
assert_eq!(db.c(2), 20);
Expand All @@ -128,7 +128,7 @@ fn on_demand_input_durability() {
}
"#]].assert_debug_eq(&events);

db.salsa_runtime_mut().synthetic_write(Durability::HIGH);
db.synthetic_write(Durability::HIGH);
events.replace(vec![]);
assert_eq!(db.c(1), 10);
assert_eq!(db.c(2), 20);
Expand Down
4 changes: 2 additions & 2 deletions crates/salsa/tests/storage_varieties/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn volatile_twice() {
let v2 = db.volatile(); // volatiles are cached, so 2nd read returns the same
assert_eq!(v1, v2);

db.salsa_runtime_mut().synthetic_write(Durability::LOW); // clears volatile caches
db.synthetic_write(Durability::LOW); // clears volatile caches

let v3 = db.volatile(); // will re-increment the counter
let v4 = db.volatile(); // second call will be cached
Expand All @@ -40,7 +40,7 @@ fn intermingled() {
assert_eq!(v1, v3);
assert_eq!(v2, v4);

db.salsa_runtime_mut().synthetic_write(Durability::LOW); // clears volatile caches
db.synthetic_write(Durability::LOW); // clears volatile caches

let v5 = db.memoized(); // re-executes volatile, caches new result
let v6 = db.memoized(); // re-use cached result
Expand Down

0 comments on commit f89d17b

Please sign in to comment.