From c81d0d51bf05791b6ed39376d67d6e2876dd2a1e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 11 Sep 2018 10:31:04 +0300 Subject: add deps tracking --- crates/libanalysis/src/db.rs | 148 +++++++++++++++++++++++++------- crates/libanalysis/src/module_map_db.rs | 45 ++++++---- crates/libsyntax2/src/lib.rs | 2 +- 3 files changed, 148 insertions(+), 47 deletions(-) diff --git a/crates/libanalysis/src/db.rs b/crates/libanalysis/src/db.rs index 31c73c402..bfff5357f 100644 --- a/crates/libanalysis/src/db.rs +++ b/crates/libanalysis/src/db.rs @@ -1,5 +1,5 @@ use std::{ - hash::Hash, + hash::{Hash, Hasher}, sync::Arc, cell::RefCell, fmt::Debug, @@ -45,17 +45,35 @@ impl DbHost { pub(crate) fn query_ctx(&self) -> QueryCtx { QueryCtx { db: Arc::clone(&self.db), + stack: RefCell::new(Vec::new()), trace: RefCell::new(Vec::new()), } } 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); - *db.cache.get_mut() = Default::default(); + db.cache.get_mut().gen += 1; db } } +type QueryInvocationId = (u32, u64); +type Gen = u64; +type OutputHash = u64; + +fn id(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(output: &Q::Output) -> OutputHash { + use std::collections::hash_map::DefaultHasher; + let mut hasher = DefaultHasher::new(); + output.hash(&mut hasher); + hasher.finish() +} + #[derive(Debug)] pub(crate) struct Db { file_resolver: FileResolverImp, @@ -73,9 +91,13 @@ impl Clone for Db { } } -#[derive(Clone, Default, Debug)] + +#[derive(Default, Debug)] pub(crate) struct Cache { - pub(crate) module_descr: QueryCache + pub(crate) module_descr: QueryCache, + gen: Gen, + green: im::HashMap, + deps: im::HashMap>, } #[allow(type_alias_bounds)] pub(crate) type QueryCache = im::HashMap< @@ -91,6 +113,7 @@ impl Cache { pub(crate) struct QueryCtx { db: Arc, + stack: RefCell>, pub(crate) trace: RefCell>, } @@ -102,12 +125,28 @@ pub(crate) struct TraceEvent { #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) enum TraceEventKind { - Start, Finish + Start, Evaluating, Finish } impl QueryCtx { pub(crate) fn get(&self, params: &Q::Params) -> Q::Output { + let me = id::(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); + self.trace(TraceEvent { query_id: Q::ID, kind: TraceEventKind::Finish }); + if let Some(parent) = parent { + let h = output_hash::(&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 } fn trace(&self, event: TraceEvent) { @@ -118,47 +157,80 @@ impl QueryCtx { pub(crate) trait Query { const ID: u32; type Params: Hash + Eq + Debug; - type Output: Debug; + type Output: Hash + Debug; } pub(crate) trait Get: Query { fn get(ctx: &QueryCtx, params: &Self::Params) -> Self::Output; } -impl Get for T +impl Get for Q where - T::Params: Clone, - T::Output: Clone, + Q::Params: Clone, + Q::Output: Clone, { fn get(ctx: &QueryCtx, params: &Self::Params) -> Self::Output { - { - let mut cache = ctx.db.cache.lock(); - if let Some(cache) = Self::cache(&mut cache) { - if let Some(res) = cache.get(params) { - return res.clone(); - } - } + if !Self::cacheable() { + ctx.trace(TraceEvent { query_id: Q::ID, kind: TraceEventKind::Evaluating }); + return Self::eval(ctx, params); } - ctx.trace(TraceEvent { query_id: Self::ID, kind: TraceEventKind::Start }); - let res = Self::eval(ctx, params); - ctx.trace(TraceEvent { query_id: Self::ID, kind: TraceEventKind::Finish }); - let mut cache = ctx.db.cache.lock(); - if let Some(cache) = Self::cache(&mut cache) { - cache.insert(params.clone(), res.clone()); + if let Some(res) = try_reuse::(ctx, params) { + return res; } + ctx.trace(TraceEvent { query_id: Q::ID, kind: TraceEventKind::Evaluating }); + let res = Self::eval(ctx, params); + + let mut cache = ctx.db.cache.lock(); + let gen = cache.gen; + let output_hash = output_hash::(&res); + let id = id::(params); + cache.green.insert(id, (gen, output_hash)); + let cache = Self::cache(&mut cache); + cache.insert(params.clone(), res.clone()); res } } +fn try_reuse(ctx: &QueryCtx, params: &Q::Params) -> Option +where + Q::Params: Clone, + Q::Output: Clone, +{ + let id = id::(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 where Self::Params: Clone, Self::Output: Clone, - { - fn cache(_cache: &mut Cache) -> Option<&mut QueryCache> { - None +{ + fn cacheable() -> bool { false } + fn cache(_cache: &mut Cache) -> &mut QueryCache { + unimplemented!() } fn eval(ctx: &QueryCtx, params: &Self::Params) -> Self::Output; } @@ -168,6 +240,12 @@ pub(crate) struct DbFiles { db: Arc, } +impl Hash for DbFiles { + fn hash(&self, hasher: &mut H) { + self.db.cache.lock().gen.hash(hasher) + } +} + impl DbFiles { pub(crate) fn iter<'a>(&'a self) -> impl Iterator + 'a { self.db.files.keys().cloned() @@ -184,8 +262,14 @@ impl Query for Files { type Output = DbFiles; } impl Get for Files { - fn get(ctx: &QueryCtx, _params: &()) -> DbFiles { - DbFiles { db: Arc::clone(&ctx.db) } + fn get(ctx: &QueryCtx, params: &()) -> DbFiles { + let res = DbFiles { db: Arc::clone(&ctx.db) }; + let id = id::(params); + let hash = output_hash::(&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 { fn get(ctx: &QueryCtx, file_id: &FileId) -> Arc { - ctx.db.files[file_id].clone() + let res = ctx.db.files[file_id].clone(); + let id = id::(file_id); + let hash = output_hash::(&res); + let mut cache = ctx.db.cache.lock(); + let gen = cache.gen; + cache.green.insert(id, (gen, hash)); + res } } diff --git a/crates/libanalysis/src/module_map_db.rs b/crates/libanalysis/src/module_map_db.rs index 14b156b43..27f19f96e 100644 --- a/crates/libanalysis/src/module_map_db.rs +++ b/crates/libanalysis/src/module_map_db.rs @@ -30,8 +30,9 @@ impl Query for ParentModule { } impl Eval for ModuleDescr { - fn cache(cache: &mut Cache) -> Option<&mut QueryCache> { - Some(&mut cache.module_descr) + fn cacheable() -> bool { true } + fn cache(cache: &mut Cache) -> &mut QueryCache { + &mut cache.module_descr } fn eval(ctx: &QueryCtx, file_id: &FileId) -> Arc { let file = ctx.get::(file_id); @@ -72,7 +73,7 @@ mod descr { ast::{self, NameOwner}, }; - #[derive(Debug)] + #[derive(Debug, Hash)] pub struct ModuleDescr { pub submodules: Vec } @@ -168,12 +169,13 @@ mod tests { expected: &[FileId], queries: &[(u32, u64)] ) { + eprintln!(); let ctx = self.db.query_ctx(); let actual = ctx.get::(&file_id); assert_eq!(actual.as_slice(), expected); let mut counts = HashMap::new(); 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 &(query_id, expected_count) in queries.iter() { let actual_count = *counts.get(&query_id).unwrap_or(&0); @@ -192,26 +194,35 @@ mod tests { fn test_parent_module() { let mut f = Fixture::new(); 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;"); - f.check_parent_modules(foo, &[lib], &[(ModuleDescr::ID, 2)]); - f.check_parent_modules(foo, &[lib], &[(ModuleDescr::ID, 0)]); + f.check_parent_modules(foo, &[lib], &[ + (ModuleDescr::ID, 1), + (FileSyntax::ID, 2), + ]); + // f.check_parent_modules(foo, &[lib], &[ + // (ModuleDescr::ID, 0), + // (FileSyntax::ID, 2), + // ]); - f.change_file(lib, ""); - f.check_parent_modules(foo, &[], &[(ModuleDescr::ID, 2)]); + // f.change_file(lib, ""); + // f.check_parent_modules(foo, &[], &[(ModuleDescr::ID, 2)]); - f.change_file(lib, "mod foo;"); - f.check_parent_modules(foo, &[lib], &[(ModuleDescr::ID, 2)]); + // f.change_file(lib, "mod foo;"); + // f.check_parent_modules(foo, &[lib], &[(ModuleDescr::ID, 2)]); - f.change_file(lib, "mod bar;"); - f.check_parent_modules(foo, &[], &[(ModuleDescr::ID, 2)]); + // f.change_file(lib, "mod bar;"); + // f.check_parent_modules(foo, &[], &[(ModuleDescr::ID, 2)]); - f.change_file(lib, "mod foo;"); - f.check_parent_modules(foo, &[lib], &[(ModuleDescr::ID, 2)]); + // f.change_file(lib, "mod foo;"); + // f.check_parent_modules(foo, &[lib], &[(ModuleDescr::ID, 2)]); - f.remove_file(lib); - f.check_parent_modules(foo, &[], &[(ModuleDescr::ID, 1)]); + // f.remove_file(lib); + // f.check_parent_modules(foo, &[], &[(ModuleDescr::ID, 1)]); } } diff --git a/crates/libsyntax2/src/lib.rs b/crates/libsyntax2/src/lib.rs index fd58cb4fa..e761fa358 100644 --- a/crates/libsyntax2/src/lib.rs +++ b/crates/libsyntax2/src/lib.rs @@ -57,7 +57,7 @@ use { parser_api::Parser, }; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Hash)] pub struct File { root: SyntaxNode } -- cgit v1.2.3