Skip to content

Commit

Permalink
docs: rearrange and documenting cache structures (#978)
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin authored Dec 11, 2024
1 parent 678b2d2 commit dde3f5d
Showing 1 changed file with 32 additions and 25 deletions.
57 changes: 32 additions & 25 deletions crates/tinymist-query/src/analysis/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub struct Analysis {
pub tokens_caches: Arc<Mutex<SemanticTokenCache>>,
/// The global caches for analysis.
pub caches: AnalysisGlobalCaches,
/// The revisioned cache for analysis.
/// The revision-managed cache for analysis.
pub analysis_rev_cache: Arc<Mutex<AnalysisRevCache>>,
/// The statistics about the analyzers.
pub stats: Arc<AnalysisStats>,
Expand All @@ -90,7 +90,7 @@ impl Analysis {
rev_lock: lg,
local: LocalContext {
tokens,
caches: AnalysisCaches::default(),
caches: AnalysisLocalCaches::default(),
shared: Arc::new(SharedContext {
slot,
lifetime,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<SemanticTokenContext>,
/// Local caches for analysis.
pub caches: AnalysisCaches,
pub caches: AnalysisLocalCaches,
/// The shared context
pub shared: Arc<SharedContext>,
}
Expand Down Expand Up @@ -1002,6 +992,9 @@ impl SharedContext {
}
}

// Needed by recursive computation
type DeferredCompute<T> = Arc<OnceCell<T>>;

#[derive(Clone)]
struct IncrCacheMap<K, V> {
revision: usize,
Expand Down Expand Up @@ -1102,8 +1095,16 @@ impl<T: Default + Clone> CacheMap<T> {
}
}

// Needed by recursive computation
type DeferredCompute<T> = Arc<OnceCell<T>>;
/// 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.
Expand All @@ -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<TypstFileId, ModuleAnalysisCache>,
pub struct AnalysisLocalCaches {
modules: HashMap<TypstFileId, ModuleAnalysisLocalCache>,
completion_files: OnceCell<Vec<TypstFileId>>,
root_files: OnceCell<Vec<TypstFileId>>,
module_deps: OnceCell<HashMap<TypstFileId, ModuleDependency>>,
}

/// 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<Arc<ExprInfo>>,
type_check: OnceCell<Arc<TypeScheme>>,
}

/// 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,
Expand Down

0 comments on commit dde3f5d

Please sign in to comment.