start salsa migration

This commit is contained in:
Aleksey Kladov 2018-10-07 13:18:25 +03:00
parent 93d77e9b22
commit d8aee31a60
8 changed files with 226 additions and 105 deletions

8
Cargo.lock generated
View file

@ -842,14 +842,6 @@ name = "safemem"
version = "0.3.0" version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "salsa"
version = "0.1.0"
dependencies = [
"im 12.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "salsa" name = "salsa"
version = "0.4.1" version = "0.4.1"

View file

@ -1,85 +1,193 @@
mod imp;
use std::{ use std::{
fmt,
sync::Arc, sync::Arc,
hash::{Hash, Hasher},
collections::HashSet,
}; };
use im;
use salsa; use salsa;
use crate::{FileId, imp::FileResolverImp}; use ra_syntax::File;
use ra_editor::{LineIndex};
use crate::{
symbol_index::SymbolIndex,
FileId, FileResolverImp
};
#[derive(Debug, Default, Clone)] #[derive(Default)]
pub(crate) struct State { pub(crate) struct RootDatabase {
pub(crate) file_map: im::HashMap<FileId, Arc<String>>, runtime: salsa::runtime::Runtime<RootDatabase>,
pub(crate) file_resolver: FileResolverImp
} }
#[derive(Debug)] impl fmt::Debug for RootDatabase {
pub(crate) struct Db { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
imp: imp::Db, fmt.write_str("RootDatabase { ... }")
}
#[derive(Clone, Copy)]
pub(crate) struct QueryCtx<'a> {
imp: &'a salsa::QueryCtx<State, imp::Data>,
}
pub(crate) struct Query<T, R>(pub(crate) u16, pub(crate) fn(QueryCtx, &T) -> R);
pub(crate) struct QueryRegistry {
imp: imp::QueryRegistry,
}
impl Default for Db {
fn default() -> Db {
Db::new()
} }
} }
impl Db { impl salsa::Database for RootDatabase {
pub(crate) fn new() -> Db { fn salsa_runtime(&self) -> &salsa::runtime::Runtime<RootDatabase> {
let reg = QueryRegistry::new(); &self.runtime
Db { imp: imp::Db::new(reg.imp) }
}
pub(crate) fn state(&self) -> &State {
self.imp.imp.ground_data()
}
pub(crate) fn with_changes(&self, new_state: State, changed_files: &[FileId], resolver_changed: bool) -> Db {
Db { imp: self.imp.with_changes(new_state, changed_files, resolver_changed) }
}
pub(crate) fn make_query<F: FnOnce(QueryCtx) -> R, R>(&self, f: F) -> R {
let ctx = QueryCtx { imp: &self.imp.imp.query_ctx() };
f(ctx)
}
#[allow(unused)]
pub(crate) fn trace_query<F: FnOnce(QueryCtx) -> R, R>(&self, f: F) -> (R, Vec<&'static str>) {
let ctx = QueryCtx { imp: &self.imp.imp.query_ctx() };
let res = f(ctx);
let trace = self.imp.extract_trace(ctx.imp);
(res, trace)
} }
} }
impl<'a> QueryCtx<'a> { salsa::database_storage! {
pub(crate) fn get<Q: imp::EvalQuery>(&self, q: Q, params: Q::Params) -> Arc<Q::Output> { pub(crate) struct RootDatabaseStorage for RootDatabase {
q.get(self, params) impl FilesDatabase {
fn file_text() for FileTextQuery;
fn file_set() for FileSetQuery;
}
impl SyntaxDatabase {
fn file_syntax() for FileSyntaxQuery;
fn file_lines() for FileLinesQuery;
fn file_symbols() for FileSymbolsQuery;
}
} }
} }
pub(crate) fn file_text(ctx: QueryCtx, file_id: FileId) -> Arc<String> { salsa::query_group! {
imp::file_text(ctx, file_id) pub(crate) trait FilesDatabase: salsa::Database {
fn file_text(file_id: FileId) -> Arc<String> {
type FileTextQuery;
storage input;
}
fn file_set(key: ()) -> Arc<FileSet> {
type FileSetQuery;
storage input;
}
}
} }
pub(crate) fn file_set(ctx: QueryCtx) -> Arc<(Vec<FileId>, FileResolverImp)> { #[derive(Default, Debug)]
imp::file_set(ctx) pub(crate) struct FileSet {
pub(crate) files: HashSet<FileId>,
pub(crate) resolver: FileResolverImp,
} }
impl QueryRegistry {
fn new() -> QueryRegistry { impl PartialEq for FileSet {
let mut reg = QueryRegistry { imp: imp::QueryRegistry::new() }; fn eq(&self, other: &FileSet) -> bool {
crate::queries::register_queries(&mut reg); self.files == other.files
crate::module_map::register_queries(&mut reg);
reg
}
pub(crate) fn add<Q: imp::EvalQuery>(&mut self, q: Q, name: &'static str) {
self.imp.add(q, name)
} }
} }
impl Eq for FileSet {
}
impl Hash for FileSet {
fn hash<H: Hasher>(&self, hasher: &mut H) {
let mut files = self.files.iter().cloned().collect::<Vec<_>>();
files.sort();
files.hash(hasher);
}
}
salsa::query_group! {
pub(crate) trait SyntaxDatabase: FilesDatabase {
fn file_syntax(file_id: FileId) -> File {
type FileSyntaxQuery;
}
fn file_lines(file_id: FileId) -> Arc<LineIndex> {
type FileLinesQuery;
}
fn file_symbols(file_id: FileId) -> Arc<SymbolIndex> {
type FileSymbolsQuery;
}
}
}
fn file_syntax(db: &impl SyntaxDatabase, file_id: FileId) -> File {
let text = db.file_text(file_id);
File::parse(&*text)
}
fn file_lines(db: &impl SyntaxDatabase, file_id: FileId) -> Arc<LineIndex> {
let text = db.file_text(file_id);
Arc::new(LineIndex::new(&*text))
}
fn file_symbols(db: &impl SyntaxDatabase, file_id: FileId) -> Arc<SymbolIndex> {
let syntax = db.file_syntax(file_id);
Arc::new(SymbolIndex::for_file(file_id, syntax))
}
// mod imp;
// use std::{
// sync::Arc,
// };
// use im;
// use salsa;
// use {FileId, imp::FileResolverImp};
// #[derive(Debug, Default, Clone)]
// pub(crate) struct State {
// pub(crate) file_map: im::HashMap<FileId, Arc<String>>,
// pub(crate) file_resolver: FileResolverImp
// }
// #[derive(Debug)]
// pub(crate) struct Db {
// imp: imp::Db,
// }
// #[derive(Clone, Copy)]
// pub(crate) struct QueryCtx<'a> {
// imp: &'a salsa::QueryCtx<State, imp::Data>,
// }
// pub(crate) struct Query<T, R>(pub(crate) u16, pub(crate) fn(QueryCtx, &T) -> R);
// pub(crate) struct QueryRegistry {
// imp: imp::QueryRegistry,
// }
// impl Default for Db {
// fn default() -> Db {
// Db::new()
// }
// }
// impl Db {
// pub(crate) fn new() -> Db {
// let reg = QueryRegistry::new();
// Db { imp: imp::Db::new(reg.imp) }
// }
// pub(crate) fn state(&self) -> &State {
// self.imp.imp.ground_data()
// }
// pub(crate) fn with_changes(&self, new_state: State, changed_files: &[FileId], resolver_changed: bool) -> Db {
// Db { imp: self.imp.with_changes(new_state, changed_files, resolver_changed) }
// }
// pub(crate) fn make_query<F: FnOnce(QueryCtx) -> R, R>(&self, f: F) -> R {
// let ctx = QueryCtx { imp: &self.imp.imp.query_ctx() };
// f(ctx)
// }
// #[allow(unused)]
// pub(crate) fn trace_query<F: FnOnce(QueryCtx) -> R, R>(&self, f: F) -> (R, Vec<&'static str>) {
// let ctx = QueryCtx { imp: &self.imp.imp.query_ctx() };
// let res = f(ctx);
// let trace = self.imp.extract_trace(ctx.imp);
// (res, trace)
// }
// }
// impl<'a> QueryCtx<'a> {
// pub(crate) fn get<Q: imp::EvalQuery>(&self, q: Q, params: Q::Params) -> Arc<Q::Output> {
// q.get(self, params)
// }
// }
// pub(crate) fn file_text(ctx: QueryCtx, file_id: FileId) -> Arc<String> {
// imp::file_text(ctx, file_id)
// }
// pub(crate) fn file_set(ctx: QueryCtx) -> Arc<(Vec<FileId>, FileResolverImp)> {
// imp::file_set(ctx)
// }
// impl QueryRegistry {
// fn new() -> QueryRegistry {
// let mut reg = QueryRegistry { imp: imp::QueryRegistry::new() };
// ::queries::register_queries(&mut reg);
// ::module_map::register_queries(&mut reg);
// reg
// }
// pub(crate) fn add<Q: imp::EvalQuery>(&mut self, q: Q, name: &'static str) {
// self.imp.add(q, name)
// }
// }

View file

@ -14,12 +14,12 @@ extern crate salsa;
extern crate rustc_hash; extern crate rustc_hash;
mod symbol_index; mod symbol_index;
mod module_map; // mod module_map;
mod imp; mod imp;
mod job; mod job;
mod roots; mod roots;
mod db; mod db;
mod queries; // mod queries;
mod descriptors; mod descriptors;
use std::{ use std::{

View file

@ -1,11 +1,13 @@
use std::{ use std::{
collections::{HashMap, HashSet},
sync::Arc, sync::Arc,
panic, panic,
}; };
use parking_lot::RwLock;
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
use rayon::prelude::*; use rayon::prelude::*;
use rustc_hash::FxHashMap; use salsa::Database;
use ra_editor::LineIndex; use ra_editor::LineIndex;
use ra_syntax::File; use ra_syntax::File;
@ -14,7 +16,7 @@ use crate::{
imp::FileResolverImp, imp::FileResolverImp,
symbol_index::SymbolIndex, symbol_index::SymbolIndex,
descriptors::{ModuleDescriptor, ModuleTreeDescriptor}, descriptors::{ModuleDescriptor, ModuleTreeDescriptor},
db::Db, db::{self, FilesDatabase, SyntaxDatabase}
}; };
pub(crate) trait SourceRoot { pub(crate) trait SourceRoot {
@ -25,9 +27,9 @@ pub(crate) trait SourceRoot {
fn symbols(&self, acc: &mut Vec<Arc<SymbolIndex>>); fn symbols(&self, acc: &mut Vec<Arc<SymbolIndex>>);
} }
#[derive(Default, Debug)] #[derive(Default, Debug, Clone)]
pub(crate) struct WritableSourceRoot { pub(crate) struct WritableSourceRoot {
db: Db, db: Arc<RwLock<db::RootDatabase>>,
} }
impl WritableSourceRoot { impl WritableSourceRoot {
@ -36,51 +38,61 @@ impl WritableSourceRoot {
changes: &mut dyn Iterator<Item=(FileId, Option<String>)>, changes: &mut dyn Iterator<Item=(FileId, Option<String>)>,
file_resolver: Option<FileResolverImp>, file_resolver: Option<FileResolverImp>,
) -> WritableSourceRoot { ) -> WritableSourceRoot {
let resolver_changed = file_resolver.is_some(); let db = self.db.write();
let mut changed_files = Vec::new(); let mut changed = HashSet::new();
let mut new_state = self.db.state().clone(); let mut removed = HashSet::new();
for (file_id, text) in changes { for (file_id, text) in changes {
changed_files.push(file_id);
match text { match text {
Some(text) => {
new_state.file_map.insert(file_id, Arc::new(text));
},
None => { None => {
new_state.file_map.remove(&file_id); removed.insert(file_id);
}
Some(text) => {
db.query(db::FileTextQuery)
.set(file_id, Arc::new(text));
changed.insert(file_id);
} }
} }
} }
if let Some(file_resolver) = file_resolver { if let Some(resolver) = file_resolver {
new_state.file_resolver = file_resolver let mut files: HashSet<FileId> = db.file_set(())
.files
.clone();
for file_id in removed {
files.remove(&file_id);
} }
WritableSourceRoot { files.extend(changed);
db: self.db.with_changes(new_state, &changed_files, resolver_changed) db.query(db::FileSetQuery)
.set((), Arc::new(db::FileSet { files, resolver }))
} }
// TODO: reconcile sasla's API with our needs
// https://github.com/salsa-rs/salsa/issues/12
self.clone()
} }
} }
impl SourceRoot for WritableSourceRoot { impl SourceRoot for WritableSourceRoot {
fn module_tree(&self) -> Arc<ModuleTreeDescriptor> { fn module_tree(&self) -> Arc<ModuleTreeDescriptor> {
self.db.make_query(crate::module_map::module_tree) unimplemented!()
//self.db.make_query(::module_map::module_tree)
} }
fn contains(&self, file_id: FileId) -> bool { fn contains(&self, file_id: FileId) -> bool {
self.db.state().file_map.contains_key(&file_id) self.db.read().file_set(()).files.contains(&file_id)
} }
fn lines(&self, file_id: FileId) -> Arc<LineIndex> { fn lines(&self, file_id: FileId) -> Arc<LineIndex> {
self.db.make_query(|ctx| crate::queries::file_lines(ctx, file_id)) self.db.read().file_lines(file_id)
} }
fn syntax(&self, file_id: FileId) -> File { fn syntax(&self, file_id: FileId) -> File {
self.db.make_query(|ctx| crate::queries::file_syntax(ctx, file_id)) self.db.read().file_syntax(file_id)
} }
fn symbols<'a>(&'a self, acc: &mut Vec<Arc<SymbolIndex>>) { fn symbols<'a>(&'a self, acc: &mut Vec<Arc<SymbolIndex>>) {
self.db.make_query(|ctx| { let db = self.db.read();
let file_set = crate::queries::file_set(ctx); let symbols = db.file_set(());
let syms = file_set.0.iter() let symbols = symbols
.map(|file_id| crate::queries::file_symbols(ctx, *file_id)); .files
acc.extend(syms); .iter()
}); .map(|&file_id| db.file_symbols(file_id));
acc.extend(symbols);
} }
} }

View file

@ -17,6 +17,15 @@ pub(crate) struct SymbolIndex {
map: fst::Map, map: fst::Map,
} }
impl PartialEq for SymbolIndex {
fn eq(&self, other: &SymbolIndex) -> bool {
self.symbols == other.symbols
}
}
impl Eq for SymbolIndex {
}
impl Hash for SymbolIndex { impl Hash for SymbolIndex {
fn hash<H: Hasher>(&self, hasher: &mut H) { fn hash<H: Hasher>(&self, hasher: &mut H) {
self.symbols.hash(hasher) self.symbols.hash(hasher)

View file

@ -1,7 +1,7 @@
use superslice::Ext; use superslice::Ext;
use crate::TextUnit; use crate::TextUnit;
#[derive(Clone, Debug, Hash)] #[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct LineIndex { pub struct LineIndex {
newlines: Vec<TextUnit>, newlines: Vec<TextUnit>,
} }

View file

@ -17,7 +17,7 @@ pub struct StructureNode {
pub kind: SyntaxKind, pub kind: SyntaxKind,
} }
#[derive(Debug, Clone, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct FileSymbol { pub struct FileSymbol {
pub name: SmolStr, pub name: SmolStr,
pub node_range: TextRange, pub node_range: TextRange,

View file

@ -59,7 +59,7 @@ use crate::{
yellow::{GreenNode}, yellow::{GreenNode},
}; };
#[derive(Clone, Debug, Hash)] #[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct File { pub struct File {
root: SyntaxNode root: SyntaxNode
} }