From dde3f5dff1b8f58eb5380cee3ef734d35d9aec51 Mon Sep 17 00:00:00 2001 From: Myriad-Dreamin <35292584+Myriad-Dreamin@users.noreply.github.com> Date: Wed, 11 Dec 2024 11:03:58 +0800 Subject: [PATCH] docs: rearrange and documenting cache structures (#978) --- crates/tinymist-query/src/analysis/global.rs | 57 +++++++++++--------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/crates/tinymist-query/src/analysis/global.rs b/crates/tinymist-query/src/analysis/global.rs index 9d4504aea..a6484327d 100644 --- a/crates/tinymist-query/src/analysis/global.rs +++ b/crates/tinymist-query/src/analysis/global.rs @@ -66,7 +66,7 @@ pub struct Analysis { pub tokens_caches: Arc>, /// The global caches for analysis. pub caches: AnalysisGlobalCaches, - /// The revisioned cache for analysis. + /// The revision-managed cache for analysis. pub analysis_rev_cache: Arc>, /// The statistics about the analyzers. pub stats: Arc, @@ -90,7 +90,7 @@ impl Analysis { rev_lock: lg, local: LocalContext { tokens, - caches: AnalysisCaches::default(), + caches: AnalysisLocalCaches::default(), shared: Arc::new(SharedContext { slot, lifetime, @@ -189,20 +189,9 @@ pub trait PeriscopeProvider { } } -/// Shared workers to limit resource usage -#[derive(Default)] -pub struct AnalysisGlobalWorkers { - /// A possible long running import dynamic analysis task - import: RateLimiter, - /// A possible long running expression dynamic analysis task - expression: RateLimiter, - /// A possible long running tooltip dynamic analysis task - tooltip: RateLimiter, -} - /// The local context guard that performs gc once dropped. pub struct LocalContextGuard { - /// Constructed local context + /// The guarded local context pub local: LocalContext, /// The revision lock pub rev_lock: AnalysisRevLock, @@ -260,12 +249,13 @@ impl LocalContextGuard { } } -/// The local context for analyzers. +/// The local context for analyzers. In addition to the shared context, it also +/// holds mutable local caches. pub struct LocalContext { /// The created semantic token context. pub(crate) tokens: Option, /// Local caches for analysis. - pub caches: AnalysisCaches, + pub caches: AnalysisLocalCaches, /// The shared context pub shared: Arc, } @@ -1002,6 +992,9 @@ impl SharedContext { } } +// Needed by recursive computation +type DeferredCompute = Arc>; + #[derive(Clone)] struct IncrCacheMap { revision: usize, @@ -1102,8 +1095,16 @@ impl CacheMap { } } -// Needed by recursive computation -type DeferredCompute = Arc>; +/// Shared workers to limit resource usage +#[derive(Default)] +pub struct AnalysisGlobalWorkers { + /// A possible long running import dynamic analysis task + import: RateLimiter, + /// A possible long running expression dynamic analysis task + expression: RateLimiter, + /// A possible long running tooltip dynamic analysis task + tooltip: RateLimiter, +} /// A global (compiler server spanned) cache for all level of analysis results /// of a module. @@ -1117,25 +1118,31 @@ pub struct AnalysisGlobalCaches { terms: CacheMap<(Value, Ty)>, } -/// A cache for all level of analysis results of a module. +/// A local (lsp request spanned) cache for all level of analysis results of a +/// module. +/// +/// You should not hold it across requests, because input like source code may +/// change. #[derive(Default)] -pub struct AnalysisCaches { - modules: HashMap, +pub struct AnalysisLocalCaches { + modules: HashMap, completion_files: OnceCell>, root_files: OnceCell>, module_deps: OnceCell>, } -/// A cache for module-level analysis results of a module. +/// A local cache for module-level analysis results of a module. /// -/// You should not holds across requests, because source code may change. +/// You should not hold it across requests, because input like source code may +/// change. #[derive(Default)] -pub struct ModuleAnalysisCache { +pub struct ModuleAnalysisLocalCache { expr_stage: OnceCell>, type_check: OnceCell>, } -/// The grid cache for all level of analysis results of a module. +/// A revision-managed (per input change) cache for all level of analysis +/// results of a module. #[derive(Default)] pub struct AnalysisRevCache { default_slot: AnalysisRevSlot,