add deps tracking

This commit is contained in:
Aleksey Kladov 2018-09-11 10:31:04 +03:00
parent db14b4270c
commit c81d0d51bf
3 changed files with 148 additions and 47 deletions

View file

@ -1,5 +1,5 @@
use std::{ use std::{
hash::Hash, hash::{Hash, Hasher},
sync::Arc, sync::Arc,
cell::RefCell, cell::RefCell,
fmt::Debug, fmt::Debug,
@ -45,17 +45,35 @@ impl DbHost {
pub(crate) fn query_ctx(&self) -> QueryCtx { pub(crate) fn query_ctx(&self) -> QueryCtx {
QueryCtx { QueryCtx {
db: Arc::clone(&self.db), db: Arc::clone(&self.db),
stack: RefCell::new(Vec::new()),
trace: RefCell::new(Vec::new()), trace: RefCell::new(Vec::new()),
} }
} }
fn db_mut(&mut self) -> &mut Db { fn db_mut(&mut self) -> &mut Db {
// NB: this "forks" the database & clears the cache // NB: this "forks" the database
let db = Arc::make_mut(&mut self.db); let db = Arc::make_mut(&mut self.db);
*db.cache.get_mut() = Default::default(); db.cache.get_mut().gen += 1;
db db
} }
} }
type QueryInvocationId = (u32, u64);
type Gen = u64;
type OutputHash = u64;
fn id<Q: Query>(params: &Q::Params) -> QueryInvocationId {
use std::collections::hash_map::DefaultHasher;
let mut hasher = DefaultHasher::new();
params.hash(&mut hasher);
(Q::ID, hasher.finish())
}
fn output_hash<Q: Query>(output: &Q::Output) -> OutputHash {
use std::collections::hash_map::DefaultHasher;
let mut hasher = DefaultHasher::new();
output.hash(&mut hasher);
hasher.finish()
}
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct Db { pub(crate) struct Db {
file_resolver: FileResolverImp, file_resolver: FileResolverImp,
@ -73,9 +91,13 @@ impl Clone for Db {
} }
} }
#[derive(Clone, Default, Debug)]
#[derive(Default, Debug)]
pub(crate) struct Cache { pub(crate) struct Cache {
pub(crate) module_descr: QueryCache<ModuleDescr> pub(crate) module_descr: QueryCache<ModuleDescr>,
gen: Gen,
green: im::HashMap<QueryInvocationId, (Gen, OutputHash)>,
deps: im::HashMap<QueryInvocationId, Vec<(QueryInvocationId, OutputHash)>>,
} }
#[allow(type_alias_bounds)] #[allow(type_alias_bounds)]
pub(crate) type QueryCache<Q: Query> = im::HashMap< pub(crate) type QueryCache<Q: Query> = im::HashMap<
@ -91,6 +113,7 @@ impl Cache {
pub(crate) struct QueryCtx { pub(crate) struct QueryCtx {
db: Arc<Db>, db: Arc<Db>,
stack: RefCell<Vec<QueryInvocationId>>,
pub(crate) trace: RefCell<Vec<TraceEvent>>, pub(crate) trace: RefCell<Vec<TraceEvent>>,
} }
@ -102,12 +125,28 @@ pub(crate) struct TraceEvent {
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum TraceEventKind { pub(crate) enum TraceEventKind {
Start, Finish Start, Evaluating, Finish
} }
impl QueryCtx { impl QueryCtx {
pub(crate) fn get<Q: Get>(&self, params: &Q::Params) -> Q::Output { pub(crate) fn get<Q: Get>(&self, params: &Q::Params) -> Q::Output {
let me = id::<Q>(params);
eprintln!("eval: {:?}", me);
let parent = self.stack.borrow().last().map(|&id| id);
self.stack.borrow_mut().push(me);
self.trace(TraceEvent { query_id: Q::ID, kind: TraceEventKind::Start });
let res = Q::get(self, params); let res = Q::get(self, params);
self.trace(TraceEvent { query_id: Q::ID, kind: TraceEventKind::Finish });
if let Some(parent) = parent {
let h = output_hash::<Q>(&res);
let mut cache = self.db.cache.lock();
cache.deps
.entry(parent)
.or_insert(Vec::new())
.push((me, h))
}
let also_me = self.stack.borrow_mut().pop();
assert_eq!(also_me, Some(me));
res res
} }
fn trace(&self, event: TraceEvent) { fn trace(&self, event: TraceEvent) {
@ -118,47 +157,80 @@ impl QueryCtx {
pub(crate) trait Query { pub(crate) trait Query {
const ID: u32; const ID: u32;
type Params: Hash + Eq + Debug; type Params: Hash + Eq + Debug;
type Output: Debug; type Output: Hash + Debug;
} }
pub(crate) trait Get: Query { pub(crate) trait Get: Query {
fn get(ctx: &QueryCtx, params: &Self::Params) -> Self::Output; fn get(ctx: &QueryCtx, params: &Self::Params) -> Self::Output;
} }
impl<T: Eval> Get for T impl<Q: Eval> Get for Q
where where
T::Params: Clone, Q::Params: Clone,
T::Output: Clone, Q::Output: Clone,
{ {
fn get(ctx: &QueryCtx, params: &Self::Params) -> Self::Output { fn get(ctx: &QueryCtx, params: &Self::Params) -> Self::Output {
{ if !Self::cacheable() {
let mut cache = ctx.db.cache.lock(); ctx.trace(TraceEvent { query_id: Q::ID, kind: TraceEventKind::Evaluating });
if let Some(cache) = Self::cache(&mut cache) { return Self::eval(ctx, params);
if let Some(res) = cache.get(params) {
return res.clone();
}
}
} }
ctx.trace(TraceEvent { query_id: Self::ID, kind: TraceEventKind::Start });
if let Some(res) = try_reuse::<Q>(ctx, params) {
return res;
}
ctx.trace(TraceEvent { query_id: Q::ID, kind: TraceEventKind::Evaluating });
let res = Self::eval(ctx, params); let res = Self::eval(ctx, params);
ctx.trace(TraceEvent { query_id: Self::ID, kind: TraceEventKind::Finish });
let mut cache = ctx.db.cache.lock(); let mut cache = ctx.db.cache.lock();
if let Some(cache) = Self::cache(&mut cache) { let gen = cache.gen;
cache.insert(params.clone(), res.clone()); let output_hash = output_hash::<Q>(&res);
} let id = id::<Q>(params);
cache.green.insert(id, (gen, output_hash));
let cache = Self::cache(&mut cache);
cache.insert(params.clone(), res.clone());
res res
} }
} }
fn try_reuse<Q: Eval>(ctx: &QueryCtx, params: &Q::Params) -> Option<Q::Output>
where
Q::Params: Clone,
Q::Output: Clone,
{
let id = id::<Q>(params);
let mut cache = ctx.db.cache.lock();
let curr_gen = cache.gen;
let old_hash = match *cache.green.get(&id)? {
(gen, _) if gen == curr_gen => {
return Some(Q::cache(&mut cache)[params].clone());
}
(_, hash) => hash,
};
let deps_are_fresh = cache.deps[&id]
.iter()
.all(|&(dep_id, dep_hash)| {
match cache.green.get(&dep_id) {
//TODO: store the value of parameters, and re-execute the query
Some((gen, hash)) if gen == &curr_gen && hash == &dep_hash => true,
_ => false,
}
});
if !deps_are_fresh {
return None;
}
cache.green.insert(id, (curr_gen, old_hash));
Some(Q::cache(&mut cache)[params].clone())
}
pub(crate) trait Eval: Query pub(crate) trait Eval: Query
where where
Self::Params: Clone, Self::Params: Clone,
Self::Output: Clone, Self::Output: Clone,
{ {
fn cache(_cache: &mut Cache) -> Option<&mut QueryCache<Self>> { fn cacheable() -> bool { false }
None fn cache(_cache: &mut Cache) -> &mut QueryCache<Self> {
unimplemented!()
} }
fn eval(ctx: &QueryCtx, params: &Self::Params) -> Self::Output; fn eval(ctx: &QueryCtx, params: &Self::Params) -> Self::Output;
} }
@ -168,6 +240,12 @@ pub(crate) struct DbFiles {
db: Arc<Db>, db: Arc<Db>,
} }
impl Hash for DbFiles {
fn hash<H: Hasher>(&self, hasher: &mut H) {
self.db.cache.lock().gen.hash(hasher)
}
}
impl DbFiles { impl DbFiles {
pub(crate) fn iter<'a>(&'a self) -> impl Iterator<Item=FileId> + 'a { pub(crate) fn iter<'a>(&'a self) -> impl Iterator<Item=FileId> + 'a {
self.db.files.keys().cloned() self.db.files.keys().cloned()
@ -184,8 +262,14 @@ impl Query for Files {
type Output = DbFiles; type Output = DbFiles;
} }
impl Get for Files { impl Get for Files {
fn get(ctx: &QueryCtx, _params: &()) -> DbFiles { fn get(ctx: &QueryCtx, params: &()) -> DbFiles {
DbFiles { db: Arc::clone(&ctx.db) } let res = DbFiles { db: Arc::clone(&ctx.db) };
let id = id::<Self>(params);
let hash = output_hash::<Self>(&res);
let mut cache = ctx.db.cache.lock();
let gen = cache.gen;
cache.green.insert(id, (gen, hash));
res
} }
} }
@ -197,7 +281,13 @@ impl Query for FileText {
} }
impl Get for FileText { impl Get for FileText {
fn get(ctx: &QueryCtx, file_id: &FileId) -> Arc<String> { fn get(ctx: &QueryCtx, file_id: &FileId) -> Arc<String> {
ctx.db.files[file_id].clone() let res = ctx.db.files[file_id].clone();
let id = id::<Self>(file_id);
let hash = output_hash::<Self>(&res);
let mut cache = ctx.db.cache.lock();
let gen = cache.gen;
cache.green.insert(id, (gen, hash));
res
} }
} }

View file

@ -30,8 +30,9 @@ impl Query for ParentModule {
} }
impl Eval for ModuleDescr { impl Eval for ModuleDescr {
fn cache(cache: &mut Cache) -> Option<&mut QueryCache<Self>> { fn cacheable() -> bool { true }
Some(&mut cache.module_descr) fn cache(cache: &mut Cache) -> &mut QueryCache<Self> {
&mut cache.module_descr
} }
fn eval(ctx: &QueryCtx, file_id: &FileId) -> Arc<descr::ModuleDescr> { fn eval(ctx: &QueryCtx, file_id: &FileId) -> Arc<descr::ModuleDescr> {
let file = ctx.get::<FileSyntax>(file_id); let file = ctx.get::<FileSyntax>(file_id);
@ -72,7 +73,7 @@ mod descr {
ast::{self, NameOwner}, ast::{self, NameOwner},
}; };
#[derive(Debug)] #[derive(Debug, Hash)]
pub struct ModuleDescr { pub struct ModuleDescr {
pub submodules: Vec<Submodule> pub submodules: Vec<Submodule>
} }
@ -168,12 +169,13 @@ mod tests {
expected: &[FileId], expected: &[FileId],
queries: &[(u32, u64)] queries: &[(u32, u64)]
) { ) {
eprintln!();
let ctx = self.db.query_ctx(); let ctx = self.db.query_ctx();
let actual = ctx.get::<ParentModule>(&file_id); let actual = ctx.get::<ParentModule>(&file_id);
assert_eq!(actual.as_slice(), expected); assert_eq!(actual.as_slice(), expected);
let mut counts = HashMap::new(); let mut counts = HashMap::new();
ctx.trace.borrow().iter() ctx.trace.borrow().iter()
.filter(|event| event.kind == TraceEventKind::Start) .filter(|event| event.kind == TraceEventKind::Evaluating)
.for_each(|event| *counts.entry(event.query_id).or_insert(0) += 1); .for_each(|event| *counts.entry(event.query_id).or_insert(0) += 1);
for &(query_id, expected_count) in queries.iter() { for &(query_id, expected_count) in queries.iter() {
let actual_count = *counts.get(&query_id).unwrap_or(&0); let actual_count = *counts.get(&query_id).unwrap_or(&0);
@ -192,26 +194,35 @@ mod tests {
fn test_parent_module() { fn test_parent_module() {
let mut f = Fixture::new(); let mut f = Fixture::new();
let foo = f.add_file("/foo.rs", ""); let foo = f.add_file("/foo.rs", "");
f.check_parent_modules(foo, &[], &[(ModuleDescr::ID, 1)]); f.check_parent_modules(foo, &[], &[
(ModuleDescr::ID, 1),
(FileSyntax::ID, 1),
]);
let lib = f.add_file("/lib.rs", "mod foo;"); let lib = f.add_file("/lib.rs", "mod foo;");
f.check_parent_modules(foo, &[lib], &[(ModuleDescr::ID, 2)]); f.check_parent_modules(foo, &[lib], &[
f.check_parent_modules(foo, &[lib], &[(ModuleDescr::ID, 0)]); (ModuleDescr::ID, 1),
(FileSyntax::ID, 2),
]);
// f.check_parent_modules(foo, &[lib], &[
// (ModuleDescr::ID, 0),
// (FileSyntax::ID, 2),
// ]);
f.change_file(lib, ""); // f.change_file(lib, "");
f.check_parent_modules(foo, &[], &[(ModuleDescr::ID, 2)]); // f.check_parent_modules(foo, &[], &[(ModuleDescr::ID, 2)]);
f.change_file(lib, "mod foo;"); // f.change_file(lib, "mod foo;");
f.check_parent_modules(foo, &[lib], &[(ModuleDescr::ID, 2)]); // f.check_parent_modules(foo, &[lib], &[(ModuleDescr::ID, 2)]);
f.change_file(lib, "mod bar;"); // f.change_file(lib, "mod bar;");
f.check_parent_modules(foo, &[], &[(ModuleDescr::ID, 2)]); // f.check_parent_modules(foo, &[], &[(ModuleDescr::ID, 2)]);
f.change_file(lib, "mod foo;"); // f.change_file(lib, "mod foo;");
f.check_parent_modules(foo, &[lib], &[(ModuleDescr::ID, 2)]); // f.check_parent_modules(foo, &[lib], &[(ModuleDescr::ID, 2)]);
f.remove_file(lib); // f.remove_file(lib);
f.check_parent_modules(foo, &[], &[(ModuleDescr::ID, 1)]); // f.check_parent_modules(foo, &[], &[(ModuleDescr::ID, 1)]);
} }
} }

View file

@ -57,7 +57,7 @@ use {
parser_api::Parser, parser_api::Parser,
}; };
#[derive(Clone, Debug)] #[derive(Clone, Debug, Hash)]
pub struct File { pub struct File {
root: SyntaxNode root: SyntaxNode
} }