diff options
author | Aleksey Kladov <[email protected]> | 2018-09-12 15:13:11 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-09-15 22:00:05 +0100 |
commit | 907d44a75113d318102ff05a66b4dcdafa1b5e7f (patch) | |
tree | d1f1511030946b859cae204b68d6bb8d645aee55 /crates | |
parent | dbdf72e2e2fb3ebc1a5cdeac4e70108371bb91fb (diff) |
any-cache
Diffstat (limited to 'crates')
-rw-r--r-- | crates/libanalysis/src/db.rs | 35 | ||||
-rw-r--r-- | crates/libanalysis/src/module_map_db.rs | 4 |
2 files changed, 18 insertions, 21 deletions
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::{ | |||
3 | sync::Arc, | 3 | sync::Arc, |
4 | cell::RefCell, | 4 | cell::RefCell, |
5 | fmt::Debug, | 5 | fmt::Debug, |
6 | any::Any, | ||
6 | }; | 7 | }; |
7 | use parking_lot::Mutex; | 8 | use parking_lot::Mutex; |
8 | use libsyntax2::{File}; | 9 | use libsyntax2::{File}; |
@@ -10,7 +11,6 @@ use im; | |||
10 | use { | 11 | use { |
11 | FileId, | 12 | FileId, |
12 | imp::{FileResolverImp}, | 13 | imp::{FileResolverImp}, |
13 | module_map_db::ModuleDescr, | ||
14 | }; | 14 | }; |
15 | 15 | ||
16 | #[derive(Debug)] | 16 | #[derive(Debug)] |
@@ -94,11 +94,13 @@ impl Clone for Db { | |||
94 | 94 | ||
95 | #[derive(Default, Debug)] | 95 | #[derive(Default, Debug)] |
96 | pub(crate) struct Cache { | 96 | pub(crate) struct Cache { |
97 | pub(crate) module_descr: QueryCache<ModuleDescr>, | ||
98 | gen: Gen, | 97 | gen: Gen, |
99 | green: im::HashMap<QueryInvocationId, (Gen, OutputHash)>, | 98 | green: im::HashMap<QueryInvocationId, (Gen, OutputHash)>, |
100 | deps: im::HashMap<QueryInvocationId, Vec<(QueryInvocationId, OutputHash)>>, | 99 | deps: im::HashMap<QueryInvocationId, Vec<(QueryInvocationId, OutputHash)>>, |
100 | results: im::HashMap<QueryInvocationId, Arc<Any>>, | ||
101 | } | 101 | } |
102 | |||
103 | |||
102 | #[allow(type_alias_bounds)] | 104 | #[allow(type_alias_bounds)] |
103 | pub(crate) type QueryCache<Q: Query> = im::HashMap< | 105 | pub(crate) type QueryCache<Q: Query> = im::HashMap< |
104 | <Q as Query>::Params, | 106 | <Q as Query>::Params, |
@@ -109,6 +111,15 @@ impl Cache { | |||
109 | fn new() -> Cache { | 111 | fn new() -> Cache { |
110 | Default::default() | 112 | Default::default() |
111 | } | 113 | } |
114 | |||
115 | fn get_result<Q: Query>(&self, id: QueryInvocationId) -> Q::Output | ||
116 | where | ||
117 | Q::Output: Clone | ||
118 | { | ||
119 | let res = &self.results[&id]; | ||
120 | let res = res.downcast_ref::<Q::Output>().unwrap(); | ||
121 | res.clone() | ||
122 | } | ||
112 | } | 123 | } |
113 | 124 | ||
114 | pub(crate) struct QueryCtx { | 125 | pub(crate) struct QueryCtx { |
@@ -150,8 +161,8 @@ impl QueryCtx { | |||
150 | 161 | ||
151 | pub(crate) trait Query { | 162 | pub(crate) trait Query { |
152 | const ID: u32; | 163 | const ID: u32; |
153 | type Params: Hash + Eq + Debug; | 164 | type Params: Hash + Eq + Debug + Any + 'static; |
154 | type Output: Hash + Debug; | 165 | type Output: Hash + Debug + Any + 'static; |
155 | } | 166 | } |
156 | 167 | ||
157 | pub(crate) trait Get: Query { | 168 | pub(crate) trait Get: Query { |
@@ -164,11 +175,6 @@ where | |||
164 | Q::Output: Clone, | 175 | Q::Output: Clone, |
165 | { | 176 | { |
166 | fn get(ctx: &QueryCtx, params: &Self::Params) -> Self::Output { | 177 | fn get(ctx: &QueryCtx, params: &Self::Params) -> Self::Output { |
167 | if !Self::cacheable() { | ||
168 | ctx.trace(TraceEvent { query_id: Q::ID, kind: TraceEventKind::Evaluating }); | ||
169 | return Self::eval(ctx, params); | ||
170 | } | ||
171 | |||
172 | if let Some(res) = try_reuse::<Q>(ctx, params) { | 178 | if let Some(res) = try_reuse::<Q>(ctx, params) { |
173 | return res; | 179 | return res; |
174 | } | 180 | } |
@@ -185,8 +191,7 @@ where | |||
185 | let output_hash = output_hash::<Q>(&res); | 191 | let output_hash = output_hash::<Q>(&res); |
186 | let id = id::<Q>(params); | 192 | let id = id::<Q>(params); |
187 | cache.green.insert(id, (gen, output_hash)); | 193 | cache.green.insert(id, (gen, output_hash)); |
188 | let cache = Self::cache(&mut cache); | 194 | cache.results.insert(me, Arc::new(res.clone())); |
189 | cache.insert(params.clone(), res.clone()); | ||
190 | res | 195 | res |
191 | } | 196 | } |
192 | } | 197 | } |
@@ -201,7 +206,7 @@ where | |||
201 | let curr_gen = cache.gen; | 206 | let curr_gen = cache.gen; |
202 | let old_hash = match *cache.green.get(&id)? { | 207 | let old_hash = match *cache.green.get(&id)? { |
203 | (gen, _) if gen == curr_gen => { | 208 | (gen, _) if gen == curr_gen => { |
204 | return Some(Q::cache(&mut cache)[params].clone()); | 209 | return Some(cache.get_result::<Q>(id)); |
205 | } | 210 | } |
206 | (_, hash) => hash, | 211 | (_, hash) => hash, |
207 | }; | 212 | }; |
@@ -218,7 +223,7 @@ where | |||
218 | return None; | 223 | return None; |
219 | } | 224 | } |
220 | cache.green.insert(id, (curr_gen, old_hash)); | 225 | cache.green.insert(id, (curr_gen, old_hash)); |
221 | Some(Q::cache(&mut cache)[params].clone()) | 226 | Some(cache.get_result::<Q>(id)) |
222 | } | 227 | } |
223 | 228 | ||
224 | pub(crate) trait Eval: Query | 229 | pub(crate) trait Eval: Query |
@@ -226,10 +231,6 @@ where | |||
226 | Self::Params: Clone, | 231 | Self::Params: Clone, |
227 | Self::Output: Clone, | 232 | Self::Output: Clone, |
228 | { | 233 | { |
229 | fn cacheable() -> bool { false } | ||
230 | fn cache(_cache: &mut Cache) -> &mut QueryCache<Self> { | ||
231 | unimplemented!() | ||
232 | } | ||
233 | fn eval(ctx: &QueryCtx, params: &Self::Params) -> Self::Output; | 234 | fn eval(ctx: &QueryCtx, params: &Self::Params) -> Self::Output; |
234 | } | 235 | } |
235 | 236 | ||
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 { | |||
30 | } | 30 | } |
31 | 31 | ||
32 | impl Eval for ModuleDescr { | 32 | impl Eval for ModuleDescr { |
33 | fn cacheable() -> bool { true } | ||
34 | fn cache(cache: &mut Cache) -> &mut QueryCache<Self> { | ||
35 | &mut cache.module_descr | ||
36 | } | ||
37 | fn eval(ctx: &QueryCtx, file_id: &FileId) -> Arc<descr::ModuleDescr> { | 33 | fn eval(ctx: &QueryCtx, file_id: &FileId) -> Arc<descr::ModuleDescr> { |
38 | let file = ctx.get::<FileSyntax>(file_id); | 34 | let file = ctx.get::<FileSyntax>(file_id); |
39 | Arc::new(descr::ModuleDescr::new(file.ast())) | 35 | Arc::new(descr::ModuleDescr::new(file.ast())) |