From a86f7a494acdb159ee471849fa7ae8f8fced79ad Mon Sep 17 00:00:00 2001 From: Myriad-Dreamin <35292584+Myriad-Dreamin@users.noreply.github.com> Date: Wed, 11 Dec 2024 14:05:52 +0800 Subject: [PATCH] perf: prefetch package index for completion (#983) --- crates/tinymist-world/src/package.rs | 5 +++++ crates/tinymist/src/server.rs | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/crates/tinymist-world/src/package.rs b/crates/tinymist-world/src/package.rs index 06ee6aade..83c83e34b 100644 --- a/crates/tinymist-world/src/package.rs +++ b/crates/tinymist-world/src/package.rs @@ -218,6 +218,11 @@ impl PackageStorage { } } + /// Get the cached package index without network access. + pub fn cached_index(&self) -> Option<&[(PackageSpec, Option)]> { + self.index.get().map(Vec::as_slice) + } + /// Download the package index. The result of this is cached for efficiency. pub fn download_index(&self) -> &[(PackageSpec, Option)] { self.index.get_or_init(|| { diff --git a/crates/tinymist/src/server.rs b/crates/tinymist/src/server.rs index 680b6325c..38a7b5dda 100644 --- a/crates/tinymist/src/server.rs +++ b/crates/tinymist/src/server.rs @@ -1069,6 +1069,16 @@ impl LanguageState { } fut_stat.stat.snap(); + if matches!(query, Completion(..)) { + // Prefetch the package index for completion. + if snap.world.registry.cached_index().is_none() { + let registry = snap.world.registry.clone(); + tokio::spawn(async move { + let _ = registry.download_index(); + }); + } + } + match query { SemanticTokensFull(req) => snap.run_semantic(req, R::SemanticTokensFull), SemanticTokensDelta(req) => snap.run_semantic(req, R::SemanticTokensDelta),