From 44ced0a920b0682190b4b5f4370fb5236fb9c4b2 Mon Sep 17 00:00:00 2001 From: Alexandru Gheorghe Date: Wed, 6 Dec 2023 14:19:37 +0200 Subject: [PATCH] Fix indexing with vscode + rustanalyzer Seems like calling cargo to determine the workspace path every time really slows down things when indexing polkadot-sdk(almost 10 minutes) so let's cache the workspace manifest path an call cargo locate only when building the cache entry, this gives us similar performance as 1.3.0 version of this crate. Signed-off-by: Alexandru Gheorghe --- src/lib.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 58d6407..e4e0043 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -162,6 +162,7 @@ type Cache = BTreeMap; struct CacheEntry { manifest_ts: SystemTime, workspace_manifest_ts: SystemTime, + workspace_manifest_path: PathBuf, crate_names: CrateNames, } @@ -186,10 +187,7 @@ pub fn crate_name(orig_name: &str) -> Result { let manifest_dir = env::var("CARGO_MANIFEST_DIR").map_err(|_| Error::CargoManifestDirNotSet)?; let manifest_path = Path::new(&manifest_dir).join("Cargo.toml"); - let workspace_manifest_path = workspace_manifest_path(&manifest_path)?; - let manifest_ts = cargo_toml_timestamp(&manifest_path)?; - let workspace_manifest_ts = cargo_toml_timestamp(&workspace_manifest_path)?; static CACHE: Mutex = Mutex::new(BTreeMap::new()); let mut cache = CACHE.lock().unwrap(); @@ -197,6 +195,8 @@ pub fn crate_name(orig_name: &str) -> Result { let crate_names = match cache.entry(manifest_dir) { btree_map::Entry::Occupied(entry) => { let cache_entry = entry.into_mut(); + let workspace_manifest_path = cache_entry.workspace_manifest_path.as_path(); + let workspace_manifest_ts = cargo_toml_timestamp(&workspace_manifest_path)?; // Timestamp changed, rebuild this cache entry. if manifest_ts != cache_entry.manifest_ts || @@ -213,6 +213,9 @@ pub fn crate_name(orig_name: &str) -> Result { &cache_entry.crate_names }, btree_map::Entry::Vacant(entry) => { + let workspace_manifest_path = workspace_manifest_path(&manifest_path)?; + let workspace_manifest_ts = cargo_toml_timestamp(&workspace_manifest_path)?; + let cache_entry = entry.insert(read_cargo_toml( &manifest_path, &workspace_manifest_path, @@ -273,7 +276,12 @@ fn read_cargo_toml( let crate_names = extract_crate_names(&manifest, workspace_dependencies)?; - Ok(CacheEntry { manifest_ts, workspace_manifest_ts, crate_names }) + Ok(CacheEntry { + manifest_ts, + workspace_manifest_ts, + crate_names, + workspace_manifest_path: workspace_manifest_path.to_path_buf(), + }) } /// Extract all `[workspace.dependencies]`.