From 907d44a75113d318102ff05a66b4dcdafa1b5e7f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 12 Sep 2018 17:13:11 +0300 Subject: any-cache --- crates/libanalysis/src/db.rs | 35 +++++++++++++++++---------------- crates/libanalysis/src/module_map_db.rs | 4 ---- 2 files changed, 18 insertions(+), 21 deletions(-) (limited to 'crates/libanalysis/src') diff --git a/crates/libanalysis/src/db.rs b/crates/libanalysis/src/db.rs index 2f344f788..d67cc189c 100644 --- a/crates/libanalysis/src/db.rs +++ b/crates/libanalysis/src/db.rs @@ -3,6 +3,7 @@ use std::{ sync::Arc, cell::RefCell, fmt::Debug, + any::Any, }; use parking_lot::Mutex; use libsyntax2::{File}; @@ -10,7 +11,6 @@ use im; use { FileId, imp::{FileResolverImp}, - module_map_db::ModuleDescr, }; #[derive(Debug)] @@ -94,11 +94,13 @@ impl Clone for Db { #[derive(Default, Debug)] pub(crate) struct Cache { - pub(crate) module_descr: QueryCache, gen: Gen, green: im::HashMap, deps: im::HashMap>, + results: im::HashMap>, } + + #[allow(type_alias_bounds)] pub(crate) type QueryCache = im::HashMap< ::Params, @@ -109,6 +111,15 @@ impl Cache { fn new() -> Cache { Default::default() } + + fn get_result(&self, id: QueryInvocationId) -> Q::Output + where + Q::Output: Clone + { + let res = &self.results[&id]; + let res = res.downcast_ref::().unwrap(); + res.clone() + } } pub(crate) struct QueryCtx { @@ -150,8 +161,8 @@ impl QueryCtx { pub(crate) trait Query { const ID: u32; - type Params: Hash + Eq + Debug; - type Output: Hash + Debug; + type Params: Hash + Eq + Debug + Any + 'static; + type Output: Hash + Debug + Any + 'static; } pub(crate) trait Get: Query { @@ -164,11 +175,6 @@ where Q::Output: Clone, { fn get(ctx: &QueryCtx, params: &Self::Params) -> Self::Output { - if !Self::cacheable() { - ctx.trace(TraceEvent { query_id: Q::ID, kind: TraceEventKind::Evaluating }); - return Self::eval(ctx, params); - } - if let Some(res) = try_reuse::(ctx, params) { return res; } @@ -185,8 +191,7 @@ where 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()); + cache.results.insert(me, Arc::new(res.clone())); res } } @@ -201,7 +206,7 @@ where 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()); + return Some(cache.get_result::(id)); } (_, hash) => hash, }; @@ -218,7 +223,7 @@ where return None; } cache.green.insert(id, (curr_gen, old_hash)); - Some(Q::cache(&mut cache)[params].clone()) + Some(cache.get_result::(id)) } pub(crate) trait Eval: Query @@ -226,10 +231,6 @@ where Self::Params: Clone, Self::Output: Clone, { - fn cacheable() -> bool { false } - fn cache(_cache: &mut Cache) -> &mut QueryCache { - unimplemented!() - } fn eval(ctx: &QueryCtx, params: &Self::Params) -> Self::Output; } diff --git a/crates/libanalysis/src/module_map_db.rs b/crates/libanalysis/src/module_map_db.rs index 72173c7bc..4d4bd9104 100644 --- a/crates/libanalysis/src/module_map_db.rs +++ b/crates/libanalysis/src/module_map_db.rs @@ -30,10 +30,6 @@ impl Query for ParentModule { } impl Eval for ModuleDescr { - 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); Arc::new(descr::ModuleDescr::new(file.ast())) -- cgit v1.2.3