Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speedup indexing with vscode + rustanalyzer #42

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading