Skip to content

Commit

Permalink
Upgrade to Salsa with tables (#13016)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser authored Aug 21, 2024
1 parent 678045e commit 5c5dfc1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 36 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,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/MichaReiser/salsa.git", tag = "red-knot-0.0.1" }
salsa = { git = "https://github.com/salsa-rs/salsa.git", rev = "f608ff8b24f07706492027199f51132244034f29" }
schemars = { version = "0.8.16" }
seahash = { version = "4.1.0" }
serde = { version = "1.0.197", features = ["derive"] }
Expand Down
8 changes: 2 additions & 6 deletions crates/red_knot_workspace/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,7 @@ impl Workspace {
new_packages.insert(path, package);
}

self.set_package_tree(db)
.with_durability(Durability::MEDIUM)
.to(new_packages);
self.set_package_tree(db).to(new_packages);
}

pub fn update_package(self, db: &mut dyn Db, metadata: PackageMetadata) -> anyhow::Result<()> {
Expand Down Expand Up @@ -358,9 +356,7 @@ impl Package {
assert_eq!(root, metadata.root());

if self.name(db) != metadata.name() {
self.set_name(db)
.with_durability(Durability::MEDIUM)
.to(metadata.name);
self.set_name(db).to(metadata.name);
}
}

Expand Down
32 changes: 8 additions & 24 deletions crates/ruff_db/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,7 @@ impl Files {

for root in roots.all() {
if root.path(db).starts_with(&path) {
root.set_revision(db)
.with_durability(Durability::HIGH)
.to(FileRevision::now());
root.set_revision(db).to(FileRevision::now());
}
}
}
Expand All @@ -249,9 +247,7 @@ impl Files {
let roots = inner.roots.read().unwrap();

for root in roots.all() {
root.set_revision(db)
.with_durability(Durability::HIGH)
.to(FileRevision::now());
root.set_revision(db).to(FileRevision::now());
}
}

Expand Down Expand Up @@ -381,23 +377,17 @@ impl File {
return;
};
let metadata = db.system().path_metadata(path);
let durability = db.files().root(db, path).map(|root| root.durability(db));
Self::sync_impl(db, metadata, file, durability);
Self::sync_impl(db, metadata, file);
}

fn sync_system_virtual_path(db: &mut dyn Db, path: &SystemVirtualPath, file: File) {
let metadata = db.system().virtual_path_metadata(path);
Self::sync_impl(db, metadata, file, None);
Self::sync_impl(db, metadata, file);
}

/// Private method providing the implementation for [`Self::sync_system_path`] and
/// [`Self::sync_system_virtual_path`].
fn sync_impl(
db: &mut dyn Db,
metadata: crate::system::Result<Metadata>,
file: File,
durability: Option<Durability>,
) {
fn sync_impl(db: &mut dyn Db, metadata: crate::system::Result<Metadata>, file: File) {
let (status, revision, permission) = match metadata {
Ok(metadata) if metadata.file_type().is_file() => (
FileStatus::Exists,
Expand All @@ -410,25 +400,19 @@ impl File {
_ => (FileStatus::NotFound, FileRevision::zero(), None),
};

let durability = durability.unwrap_or_default();

if file.status(db) != status {
tracing::debug!("Updating the status of '{}'", file.path(db),);
file.set_status(db).with_durability(durability).to(status);
file.set_status(db).to(status);
}

if file.revision(db) != revision {
tracing::debug!("Updating the revision of '{}'", file.path(db));
file.set_revision(db)
.with_durability(durability)
.to(revision);
file.set_revision(db).to(revision);
}

if file.permissions(db) != permission {
tracing::debug!("Updating the permissions of '{}'", file.path(db),);
file.set_permissions(db)
.with_durability(durability)
.to(permission);
file.set_permissions(db).to(permission);
}
}

Expand Down
14 changes: 12 additions & 2 deletions crates/ruff_db/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,20 @@ pub fn assert_const_function_query_was_not_run<Db, Q, QDb, R>(
Db: salsa::Database,
Q: Fn(QDb) -> R,
{
let (query_name, will_execute_event) = find_will_execute_event(db, query, (), events);
// Salsa now interns singleton ingredients. But we know that it is a singleton, so we can just search for
// any event of that ingredient.
let query_name = query_name(&query);

let event = events.iter().find(|event| {
if let salsa::EventKind::WillExecute { database_key } = event.kind {
db.ingredient_debug_name(database_key.ingredient_index()) == query_name
} else {
false
}
});

db.attach(|_| {
if let Some(will_execute_event) = will_execute_event {
if let Some(will_execute_event) = event {
panic!(
"Expected query {query_name}() not to have run but it did: {will_execute_event:?}"
);
Expand Down

0 comments on commit 5c5dfc1

Please sign in to comment.