Skip to content

Switch to fully dynamically dispatched salsa #5242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 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 crates/ra_db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ authors = ["rust-analyzer developers"]
doctest = false

[dependencies]
salsa = "0.14.1"
salsa = { git = "https://github.com/nikomatsakis/salsa", branch = "dynamic-databases-rfc" }
relative-path = "1.0.0"
rustc-hash = "1.1.0"

Expand Down
7 changes: 2 additions & 5 deletions crates/ra_db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pub trait SourceDatabase: CheckCanceled + FileLoader + std::fmt::Debug {
fn crate_graph(&self) -> Arc<CrateGraph>;
}

fn parse_query(db: &impl SourceDatabase, file_id: FileId) -> Parse<ast::SourceFile> {
fn parse_query(db: &dyn SourceDatabase, file_id: FileId) -> Parse<ast::SourceFile> {
let _p = profile("parse_query").detail(|| format!("{:?}", file_id));
let text = db.file_text(file_id);
SourceFile::parse(&*text)
Expand All @@ -136,10 +136,7 @@ pub trait SourceDatabaseExt: SourceDatabase {
fn source_root_crates(&self, id: SourceRootId) -> Arc<FxHashSet<CrateId>>;
}

fn source_root_crates(
db: &(impl SourceDatabaseExt + SourceDatabase),
id: SourceRootId,
) -> Arc<FxHashSet<CrateId>> {
fn source_root_crates(db: &dyn SourceDatabaseExt, id: SourceRootId) -> Arc<FxHashSet<CrateId>> {
let graph = db.crate_graph();
let res = graph
.iter()
Expand Down
2 changes: 1 addition & 1 deletion crates/ra_hir_def/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct FunctionData {
}

impl FunctionData {
pub(crate) fn fn_data_query(db: &impl DefDatabase, func: FunctionId) -> Arc<FunctionData> {
pub(crate) fn fn_data_query(db: &dyn DefDatabase, func: FunctionId) -> Arc<FunctionData> {
let loc = func.lookup(db);
let item_tree = db.item_tree(loc.id.file_id);
let func = &item_tree[loc.id.value];
Expand Down
28 changes: 14 additions & 14 deletions crates/ra_hir_def/src/test_db.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Database used for testing `hir_def`.

use std::{
panic,
fmt, panic,
sync::{Arc, Mutex},
};

Expand All @@ -18,10 +18,10 @@ use crate::db::DefDatabase;
crate::db::InternDatabaseStorage,
crate::db::DefDatabaseStorage
)]
#[derive(Debug, Default)]
#[derive(Default)]
pub struct TestDB {
runtime: salsa::Runtime<TestDB>,
events: Mutex<Option<Vec<salsa::Event<TestDB>>>>,
storage: salsa::Storage<TestDB>,
events: Mutex<Option<Vec<salsa::Event>>>,
}

impl Upcast<dyn AstDatabase> for TestDB {
Expand All @@ -37,20 +37,20 @@ impl Upcast<dyn DefDatabase> for TestDB {
}

impl salsa::Database for TestDB {
fn salsa_runtime(&self) -> &salsa::Runtime<Self> {
&self.runtime
}
fn salsa_runtime_mut(&mut self) -> &mut salsa::Runtime<Self> {
&mut self.runtime
}
fn salsa_event(&self, event: impl Fn() -> salsa::Event<TestDB>) {
fn salsa_event(&self, event: salsa::Event) {
let mut events = self.events.lock().unwrap();
if let Some(events) = &mut *events {
events.push(event());
events.push(event);
}
}
}

impl fmt::Debug for TestDB {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TestDB").finish()
}
}

impl panic::RefUnwindSafe for TestDB {}

impl FileLoader for TestDB {
Expand Down Expand Up @@ -78,7 +78,7 @@ impl TestDB {
panic!("Can't find module for file")
}

pub fn log(&self, f: impl FnOnce()) -> Vec<salsa::Event<TestDB>> {
pub fn log(&self, f: impl FnOnce()) -> Vec<salsa::Event> {
*self.events.lock().unwrap() = Some(Vec::new());
f();
self.events.lock().unwrap().take().unwrap()
Expand All @@ -92,7 +92,7 @@ impl TestDB {
// This pretty horrible, but `Debug` is the only way to inspect
// QueryDescriptor at the moment.
salsa::EventKind::WillExecute { database_key } => {
Some(format!("{:?}", database_key))
Some(format!("{:?}", database_key.debug(self)))
}
_ => None,
})
Expand Down
24 changes: 11 additions & 13 deletions crates/ra_hir_expand/src/test_db.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Database used for testing `hir_expand`.

use std::{
panic,
fmt, panic,
sync::{Arc, Mutex},
};

Expand All @@ -13,25 +13,23 @@ use rustc_hash::FxHashSet;
ra_db::SourceDatabaseStorage,
crate::db::AstDatabaseStorage
)]
#[derive(Debug, Default)]
#[derive(Default)]
pub struct TestDB {
runtime: salsa::Runtime<TestDB>,
events: Mutex<Option<Vec<salsa::Event<TestDB>>>>,
storage: salsa::Storage<TestDB>,
events: Mutex<Option<Vec<salsa::Event>>>,
}

impl salsa::Database for TestDB {
fn salsa_runtime(&self) -> &salsa::Runtime<Self> {
&self.runtime
}

fn salsa_runtime_mut(&mut self) -> &mut salsa::Runtime<Self> {
&mut self.runtime
impl fmt::Debug for TestDB {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TestDB").finish()
}
}

fn salsa_event(&self, event: impl Fn() -> salsa::Event<TestDB>) {
impl salsa::Database for TestDB {
fn salsa_event(&self, event: salsa::Event) {
let mut events = self.events.lock().unwrap();
if let Some(events) = &mut *events {
events.push(event());
events.push(event);
}
}
}
Expand Down
1 change: 0 additions & 1 deletion crates/ra_hir_ty/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use crate::{
use hir_expand::name::Name;

#[salsa::query_group(HirDatabaseStorage)]
#[salsa::requires(salsa::Database)]
pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
#[salsa::invoke(infer_wait)]
#[salsa::transparent]
Expand Down
2 changes: 1 addition & 1 deletion crates/ra_hir_ty/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,7 @@ pub(crate) fn impl_trait_query(db: &dyn HirDatabase, impl_id: ImplId) -> Option<
}

pub(crate) fn return_type_impl_traits(
db: &impl HirDatabase,
db: &dyn HirDatabase,
def: hir_def::FunctionId,
) -> Option<Arc<Binders<ReturnTypeImplTraits>>> {
// FIXME unify with fn_sig_for_fn instead of doing lowering twice, maybe
Expand Down
31 changes: 14 additions & 17 deletions crates/ra_hir_ty/src/test_db.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Database used for testing `hir`.

use std::{
panic,
fmt, panic,
sync::{Arc, Mutex},
};

Expand All @@ -26,10 +26,15 @@ use crate::{
hir_def::db::DefDatabaseStorage,
crate::db::HirDatabaseStorage
)]
#[derive(Debug, Default)]
#[derive(Default)]
pub struct TestDB {
events: Mutex<Option<Vec<salsa::Event<TestDB>>>>,
runtime: salsa::Runtime<TestDB>,
storage: salsa::Storage<TestDB>,
events: Mutex<Option<Vec<salsa::Event>>>,
}
impl fmt::Debug for TestDB {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TestDB").finish()
}
}

impl Upcast<dyn AstDatabase> for TestDB {
Expand All @@ -45,27 +50,19 @@ impl Upcast<dyn DefDatabase> for TestDB {
}

impl salsa::Database for TestDB {
fn salsa_runtime(&self) -> &salsa::Runtime<TestDB> {
&self.runtime
}

fn salsa_runtime_mut(&mut self) -> &mut salsa::Runtime<Self> {
&mut self.runtime
}

fn salsa_event(&self, event: impl Fn() -> salsa::Event<TestDB>) {
fn salsa_event(&self, event: salsa::Event) {
let mut events = self.events.lock().unwrap();
if let Some(events) = &mut *events {
events.push(event());
events.push(event);
}
}
}

impl salsa::ParallelDatabase for TestDB {
fn snapshot(&self) -> salsa::Snapshot<TestDB> {
salsa::Snapshot::new(TestDB {
storage: self.storage.snapshot(),
events: Default::default(),
runtime: self.runtime.snapshot(self),
})
}
}
Expand Down Expand Up @@ -182,7 +179,7 @@ impl TestDB {
}

impl TestDB {
pub fn log(&self, f: impl FnOnce()) -> Vec<salsa::Event<TestDB>> {
pub fn log(&self, f: impl FnOnce()) -> Vec<salsa::Event> {
*self.events.lock().unwrap() = Some(Vec::new());
f();
self.events.lock().unwrap().take().unwrap()
Expand All @@ -196,7 +193,7 @@ impl TestDB {
// This pretty horrible, but `Debug` is the only way to inspect
// QueryDescriptor at the moment.
salsa::EventKind::WillExecute { database_key } => {
Some(format!("{:?}", database_key))
Some(format!("{:?}", database_key.debug(self)))
}
_ => None,
})
Expand Down
4 changes: 2 additions & 2 deletions crates/ra_hir_ty/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use hir_def::{
};
use hir_expand::{db::AstDatabase, InFile};
use insta::assert_snapshot;
use ra_db::{fixture::WithFixture, salsa::Database, FileRange, SourceDatabase};
use ra_db::{fixture::WithFixture, FileRange, SourceDatabase, SourceDatabaseExt};
use ra_syntax::{
algo,
ast::{self, AstNode},
Expand Down Expand Up @@ -317,7 +317,7 @@ fn typing_whitespace_inside_a_function_should_not_invalidate_types() {
"
.to_string();

db.query_mut(ra_db::FileTextQuery).set(pos.file_id, Arc::new(new_text));
db.set_file_text(pos.file_id, Arc::new(new_text));

{
let events = db.log_executed(|| {
Expand Down
2 changes: 1 addition & 1 deletion crates/ra_hir_ty/src/traits/chalk/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use hir_def::{AdtId, AssocContainerId, DefWithBodyId, Lookup, TypeAliasId};

pub use unsafe_tls::{set_current_program, with_current_program};

pub struct DebugContext<'a>(&'a (dyn HirDatabase + 'a));
pub struct DebugContext<'a>(&'a dyn HirDatabase);

impl DebugContext<'_> {
pub fn debug_struct_id(
Expand Down
15 changes: 6 additions & 9 deletions crates/ra_ide/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use std::{fmt, iter::FromIterator, sync::Arc};

use hir::MacroFile;
use ra_db::{
salsa::{
debug::{DebugQueryTable, TableEntry},
Database,
},
salsa::debug::{DebugQueryTable, TableEntry},
FileTextQuery, SourceRootId,
};
use ra_ide_db::{
Expand All @@ -14,15 +11,15 @@ use ra_ide_db::{
};
use ra_prof::{memory_usage, Bytes};
use ra_syntax::{ast, Parse, SyntaxNode};
use rustc_hash::FxHashMap;

use crate::FileId;
use rustc_hash::FxHashMap;

fn syntax_tree_stats(db: &RootDatabase) -> SyntaxTreeStats {
db.query(ra_db::ParseQuery).entries::<SyntaxTreeStats>()
ra_db::ParseQuery.in_db(db).entries::<SyntaxTreeStats>()
}
fn macro_syntax_tree_stats(db: &RootDatabase) -> SyntaxTreeStats {
db.query(hir::db::ParseMacroQuery).entries::<SyntaxTreeStats>()
hir::db::ParseMacroQuery.in_db(db).entries::<SyntaxTreeStats>()
}

// Feature: Status
Expand All @@ -35,10 +32,10 @@ fn macro_syntax_tree_stats(db: &RootDatabase) -> SyntaxTreeStats {
// | VS Code | **Rust Analyzer: Status**
// |===
pub(crate) fn status(db: &RootDatabase) -> String {
let files_stats = db.query(FileTextQuery).entries::<FilesStats>();
let files_stats = FileTextQuery.in_db(db).entries::<FilesStats>();
let syntax_tree_stats = syntax_tree_stats(db);
let macro_syntax_tree_stats = macro_syntax_tree_stats(db);
let symbols_stats = db.query(LibrarySymbolsQuery).entries::<LibrarySymbolsStats>();
let symbols_stats = LibrarySymbolsQuery.in_db(db).entries::<LibrarySymbolsStats>();
format!(
"{}\n{}\n{}\n{} (macros)\n\n\nmemory:\n{}\ngc {:?} seconds ago",
files_stats,
Expand Down
20 changes: 10 additions & 10 deletions crates/ra_ide_db/src/change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,21 @@ impl RootDatabase {

let sweep = SweepStrategy::default().discard_values().sweep_all_revisions();

self.query(ra_db::ParseQuery).sweep(sweep);
self.query(hir::db::ParseMacroQuery).sweep(sweep);
ra_db::ParseQuery.in_db(self).sweep(sweep);
hir::db::ParseMacroQuery.in_db(self).sweep(sweep);

// Macros do take significant space, but less then the syntax trees
// self.query(hir::db::MacroDefQuery).sweep(sweep);
// self.query(hir::db::MacroArgQuery).sweep(sweep);
// self.query(hir::db::MacroExpandQuery).sweep(sweep);

self.query(hir::db::AstIdMapQuery).sweep(sweep);
hir::db::AstIdMapQuery.in_db(self).sweep(sweep);

self.query(hir::db::BodyWithSourceMapQuery).sweep(sweep);
hir::db::BodyWithSourceMapQuery.in_db(self).sweep(sweep);

self.query(hir::db::ExprScopesQuery).sweep(sweep);
self.query(hir::db::InferQueryQuery).sweep(sweep);
self.query(hir::db::BodyQuery).sweep(sweep);
hir::db::ExprScopesQuery.in_db(self).sweep(sweep);
hir::db::InferQueryQuery.in_db(self).sweep(sweep);
hir::db::BodyQuery.in_db(self).sweep(sweep);
}

pub fn per_query_memory_usage(&mut self) -> Vec<(String, Bytes)> {
Expand All @@ -170,14 +170,14 @@ impl RootDatabase {
macro_rules! sweep_each_query {
($($q:path)*) => {$(
let before = memory_usage().allocated;
self.query($q).sweep(sweep);
$q.in_db(self).sweep(sweep);
let after = memory_usage().allocated;
let q: $q = Default::default();
let name = format!("{:?}", q);
acc.push((name, before - after));

let before = memory_usage().allocated;
self.query($q).sweep(sweep.discard_everything());
$q.in_db(self).sweep(sweep.discard_everything());
let after = memory_usage().allocated;
let q: $q = Default::default();
let name = format!("{:?} (deps)", q);
Expand Down Expand Up @@ -252,7 +252,7 @@ impl RootDatabase {
// write.
// We do this after collecting the non-interned queries to correctly attribute memory used
// by interned data.
self.runtime.synthetic_write(Durability::HIGH);
self.salsa_runtime_mut().synthetic_write(Durability::HIGH);

sweep_each_query![
// AstDatabase
Expand Down
Loading