From c3b6139f394a0e24cba0b5b1166daa80aba601a6 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Tue, 17 Dec 2024 16:50:33 +0100 Subject: [PATCH] Upgrade salsa (#15039) The only code change is that Salsa now requires the `Db` to implement `Clone` to create "lightweight" snapshots. --- Cargo.lock | 7 ++++--- Cargo.toml | 2 +- crates/red_knot/src/main.rs | 2 +- crates/red_knot_python_semantic/src/db.rs | 1 + crates/red_knot_server/src/server/api.rs | 6 +++--- crates/red_knot_test/src/db.rs | 1 + crates/red_knot_workspace/src/db.rs | 13 ++----------- crates/red_knot_workspace/src/workspace.rs | 4 ++-- crates/ruff/src/commands/analyze_graph.rs | 4 ++-- crates/ruff_db/src/files.rs | 9 +-------- crates/ruff_db/src/lib.rs | 4 ++-- crates/ruff_graph/src/db.rs | 13 +------------ fuzz/fuzz_targets/red_knot_check_invalid_syntax.rs | 3 ++- 13 files changed, 23 insertions(+), 46 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 07b4b3957f..3f5155adf7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3193,7 +3193,7 @@ checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1" [[package]] name = "salsa" version = "0.18.0" -source = "git+https://github.com/salsa-rs/salsa.git?rev=254c749b02cde2fd29852a7463a33e800b771758#254c749b02cde2fd29852a7463a33e800b771758" +source = "git+https://github.com/salsa-rs/salsa.git?rev=3c7f1694c9efba751dbeeacfbc93b227586e316a#3c7f1694c9efba751dbeeacfbc93b227586e316a" dependencies = [ "append-only-vec", "arc-swap", @@ -3203,6 +3203,7 @@ dependencies = [ "indexmap", "lazy_static", "parking_lot", + "rayon", "rustc-hash 2.1.0", "salsa-macro-rules", "salsa-macros", @@ -3213,12 +3214,12 @@ dependencies = [ [[package]] name = "salsa-macro-rules" version = "0.1.0" -source = "git+https://github.com/salsa-rs/salsa.git?rev=254c749b02cde2fd29852a7463a33e800b771758#254c749b02cde2fd29852a7463a33e800b771758" +source = "git+https://github.com/salsa-rs/salsa.git?rev=3c7f1694c9efba751dbeeacfbc93b227586e316a#3c7f1694c9efba751dbeeacfbc93b227586e316a" [[package]] name = "salsa-macros" version = "0.18.0" -source = "git+https://github.com/salsa-rs/salsa.git?rev=254c749b02cde2fd29852a7463a33e800b771758#254c749b02cde2fd29852a7463a33e800b771758" +source = "git+https://github.com/salsa-rs/salsa.git?rev=3c7f1694c9efba751dbeeacfbc93b227586e316a#3c7f1694c9efba751dbeeacfbc93b227586e316a" dependencies = [ "heck", "proc-macro2", diff --git a/Cargo.toml b/Cargo.toml index 8931a9a4fb..4ea76812c9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -118,7 +118,7 @@ rand = { version = "0.8.5" } rayon = { version = "1.10.0" } regex = { version = "1.10.2" } rustc-hash = { version = "2.0.0" } -salsa = { git = "https://github.com/salsa-rs/salsa.git", rev = "254c749b02cde2fd29852a7463a33e800b771758" } +salsa = { git = "https://github.com/salsa-rs/salsa.git", rev = "3c7f1694c9efba751dbeeacfbc93b227586e316a" } schemars = { version = "0.8.16" } seahash = { version = "4.1.0" } serde = { version = "1.0.197", features = ["derive"] } diff --git a/crates/red_knot/src/main.rs b/crates/red_knot/src/main.rs index 2afc14841a..49730267b2 100644 --- a/crates/red_knot/src/main.rs +++ b/crates/red_knot/src/main.rs @@ -279,7 +279,7 @@ impl MainLoop { while let Ok(message) = self.receiver.recv() { match message { MainLoopMessage::CheckWorkspace => { - let db = db.snapshot(); + let db = db.clone(); let sender = self.sender.clone(); // Spawn a new task that checks the workspace. This needs to be done in a separate thread diff --git a/crates/red_knot_python_semantic/src/db.rs b/crates/red_knot_python_semantic/src/db.rs index ccb019d220..1e40d7ad13 100644 --- a/crates/red_knot_python_semantic/src/db.rs +++ b/crates/red_knot_python_semantic/src/db.rs @@ -27,6 +27,7 @@ pub(crate) mod tests { use ruff_db::{Db as SourceDb, Upcast}; #[salsa::db] + #[derive(Clone)] pub(crate) struct TestDb { storage: salsa::Storage, files: Files, diff --git a/crates/red_knot_server/src/server/api.rs b/crates/red_knot_server/src/server/api.rs index ebef3472d1..be1aca8637 100644 --- a/crates/red_knot_server/src/server/api.rs +++ b/crates/red_knot_server/src/server/api.rs @@ -91,11 +91,11 @@ fn background_request_task<'a, R: traits::BackgroundDocumentRequestHandler>( let db = match path { AnySystemPath::System(path) => { match session.workspace_db_for_path(path.as_std_path()) { - Some(db) => db.snapshot(), - None => session.default_workspace_db().snapshot(), + Some(db) => db.clone(), + None => session.default_workspace_db().clone(), } } - AnySystemPath::SystemVirtual(_) => session.default_workspace_db().snapshot(), + AnySystemPath::SystemVirtual(_) => session.default_workspace_db().clone(), }; let Some(snapshot) = session.take_snapshot(url) else { diff --git a/crates/red_knot_test/src/db.rs b/crates/red_knot_test/src/db.rs index f8819802df..df1cfc503e 100644 --- a/crates/red_knot_test/src/db.rs +++ b/crates/red_knot_test/src/db.rs @@ -9,6 +9,7 @@ use ruff_db::vendored::VendoredFileSystem; use ruff_db::{Db as SourceDb, Upcast}; #[salsa::db] +#[derive(Clone)] pub(crate) struct Db { workspace_root: SystemPathBuf, storage: salsa::Storage, diff --git a/crates/red_knot_workspace/src/db.rs b/crates/red_knot_workspace/src/db.rs index 318112f26c..a32400b00f 100644 --- a/crates/red_knot_workspace/src/db.rs +++ b/crates/red_knot_workspace/src/db.rs @@ -21,6 +21,7 @@ pub trait Db: SemanticDb + Upcast { } #[salsa::db] +#[derive(Clone)] pub struct RootDatabase { workspace: Option, storage: salsa::Storage, @@ -80,17 +81,6 @@ impl RootDatabase { { Cancelled::catch(|| f(self)) } - - #[must_use] - pub fn snapshot(&self) -> Self { - Self { - workspace: self.workspace, - storage: self.storage.clone(), - files: self.files.snapshot(), - system: Arc::clone(&self.system), - rule_selection: Arc::clone(&self.rule_selection), - } - } } impl Upcast for RootDatabase { @@ -184,6 +174,7 @@ pub(crate) mod tests { use crate::DEFAULT_LINT_REGISTRY; #[salsa::db] + #[derive(Clone)] pub(crate) struct TestDb { storage: salsa::Storage, events: Arc>>, diff --git a/crates/red_knot_workspace/src/workspace.rs b/crates/red_knot_workspace/src/workspace.rs index 7aa3cc9489..6b7134ecfc 100644 --- a/crates/red_knot_workspace/src/workspace.rs +++ b/crates/red_knot_workspace/src/workspace.rs @@ -195,13 +195,13 @@ impl Workspace { let result = Arc::new(std::sync::Mutex::new(Vec::new())); let inner_result = Arc::clone(&result); - let db = db.snapshot(); + let db = db.clone(); let workspace_span = workspace_span.clone(); rayon::scope(move |scope| { for file in &files { let result = inner_result.clone(); - let db = db.snapshot(); + let db = db.clone(); let workspace_span = workspace_span.clone(); scope.spawn(move |_| { diff --git a/crates/ruff/src/commands/analyze_graph.rs b/crates/ruff/src/commands/analyze_graph.rs index f8a0684d42..0eb36cea6b 100644 --- a/crates/ruff/src/commands/analyze_graph.rs +++ b/crates/ruff/src/commands/analyze_graph.rs @@ -81,7 +81,7 @@ pub(crate) fn analyze_graph( // Collect and resolve the imports for each file. let result = Arc::new(Mutex::new(Vec::new())); let inner_result = Arc::clone(&result); - let db = db.snapshot(); + let db = db.clone(); rayon::scope(move |scope| { for resolved_file in paths { @@ -137,7 +137,7 @@ pub(crate) fn analyze_graph( continue; }; - let db = db.snapshot(); + let db = db.clone(); let glob_resolver = glob_resolver.clone(); let root = root.clone(); let result = inner_result.clone(); diff --git a/crates/ruff_db/src/files.rs b/crates/ruff_db/src/files.rs index ed257efc13..d4354957aa 100644 --- a/crates/ruff_db/src/files.rs +++ b/crates/ruff_db/src/files.rs @@ -48,7 +48,7 @@ pub fn vendored_path_to_file( } /// Lookup table that maps [file paths](`FilePath`) to salsa interned [`File`] instances. -#[derive(Default)] +#[derive(Default, Clone)] pub struct Files { inner: Arc, } @@ -253,13 +253,6 @@ impl Files { root.set_revision(db).to(FileRevision::now()); } } - - #[must_use] - pub fn snapshot(&self) -> Self { - Self { - inner: Arc::clone(&self.inner), - } - } } impl std::fmt::Debug for Files { diff --git a/crates/ruff_db/src/lib.rs b/crates/ruff_db/src/lib.rs index adccefb7c4..d7d85a0db9 100644 --- a/crates/ruff_db/src/lib.rs +++ b/crates/ruff_db/src/lib.rs @@ -48,13 +48,13 @@ mod tests { /// /// Uses an in memory filesystem and it stubs out the vendored files by default. #[salsa::db] - #[derive(Default)] + #[derive(Default, Clone)] pub(crate) struct TestDb { storage: salsa::Storage, files: Files, system: TestSystem, vendored: VendoredFileSystem, - events: std::sync::Arc>>, + events: Arc>>, } impl TestDb { diff --git a/crates/ruff_graph/src/db.rs b/crates/ruff_graph/src/db.rs index 520f79cecf..a70ae6d91e 100644 --- a/crates/ruff_graph/src/db.rs +++ b/crates/ruff_graph/src/db.rs @@ -16,7 +16,7 @@ static EMPTY_VENDORED: std::sync::LazyLock = std::sync::Lazy }); #[salsa::db] -#[derive(Default)] +#[derive(Default, Clone)] pub struct ModuleDb { storage: salsa::Storage, files: Files, @@ -55,17 +55,6 @@ impl ModuleDb { Ok(db) } - - /// Create a snapshot of the current database. - #[must_use] - pub fn snapshot(&self) -> Self { - Self { - storage: self.storage.clone(), - system: self.system.clone(), - files: self.files.snapshot(), - rule_selection: Arc::clone(&self.rule_selection), - } - } } impl Upcast for ModuleDb { diff --git a/fuzz/fuzz_targets/red_knot_check_invalid_syntax.rs b/fuzz/fuzz_targets/red_knot_check_invalid_syntax.rs index 7be30ebecc..1259b8c336 100644 --- a/fuzz/fuzz_targets/red_knot_check_invalid_syntax.rs +++ b/fuzz/fuzz_targets/red_knot_check_invalid_syntax.rs @@ -22,12 +22,13 @@ use ruff_python_parser::{parse_unchecked, Mode}; /// /// Uses an in memory filesystem and it stubs out the vendored files by default. #[salsa::db] +#[derive(Clone)] struct TestDb { storage: salsa::Storage, files: Files, system: TestSystem, vendored: VendoredFileSystem, - events: std::sync::Arc>>, + events: std::sync::Arc>>, rule_selection: std::sync::Arc, }