Skip to content

Commit

Permalink
Fix indexing with vscode + rustanalyzer
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
alexggh committed Dec 6, 2023
1 parent 39a7c18 commit 44ced0a
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ type Cache = BTreeMap<String, CacheEntry>;
struct CacheEntry {
manifest_ts: SystemTime,
workspace_manifest_ts: SystemTime,
workspace_manifest_path: PathBuf,
crate_names: CrateNames,
}

Expand All @@ -186,17 +187,16 @@ pub fn crate_name(orig_name: &str) -> Result<FoundCrate, Error> {
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<Cache> = Mutex::new(BTreeMap::new());
let mut cache = CACHE.lock().unwrap();

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 ||
Expand All @@ -213,6 +213,9 @@ pub fn crate_name(orig_name: &str) -> Result<FoundCrate, Error> {
&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,
Expand Down Expand Up @@ -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]`.
Expand Down

0 comments on commit 44ced0a

Please sign in to comment.