Skip to content

Commit ca80599

Browse files
authored
fix(project): batch Git ignore checks per directory level (#60)
Preparing provider exclusions asked Git once per parent directory: one `git check-ignore` for its children, plus one `git ls-files` for each ignored child. On Kubernetes that is about 2,200 serial Git processes and 29 s before gopls receives its first workspace generation (1lck/Lithe-IDEA#1994). Prefetch the same answers a directory level at a time: one `check-ignore` per level and repository, and one batched `ls-files` (the new `lithe_git::tracked_directories`) for the ignored children. Walks still record decisions per parent through `prepare_parent`, which takes them from the prefetch only when every candidate was answered there and otherwise asks Git as before, so the excluded roots that providers and the indexing status see do not change. Provider preparation, snapshots and search index builds prefetch with their own visibility rules; releasing the last lease frees the prefetched answers along with the decisions. Kubernetes: 29.2 s and 2,179 Git processes before, 3.1 s and 14 after, with identical file lists. The Lithe-IDEA checkout keeps the same seven excluded roots with 9 Git processes instead of 59.
1 parent 5802c42 commit ca80599

4 files changed

Lines changed: 475 additions & 2 deletions

File tree

‎rust/crates/git/src/status.rs‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,55 @@ pub fn ignored_paths(
302302
))
303303
}
304304

305+
/// Reports which `directories` hold at least one tracked entry, with one
306+
/// `git ls-files` per pathspec batch instead of one per directory.
307+
///
308+
/// `directories` are `/`-separated literal pathspecs relative to `directory`, the
309+
/// same form `git ls-files` prints. `None` means Git could not answer, or printed a
310+
/// path that no requested directory contains (for example a differently normalized
311+
/// Unicode name); callers then fall back to asking about each directory.
312+
pub fn tracked_directories(
313+
directory: &Path,
314+
directories: &[String],
315+
) -> Result<Option<HashSet<String>>, CoreError> {
316+
let requested = directories
317+
.iter()
318+
.map(String::as_str)
319+
.collect::<HashSet<_>>();
320+
let mut tracked = HashSet::new();
321+
for batch in pathspec_batches(directories, LS_FILES_PATHSPEC_BUDGET) {
322+
let mut arguments = vec![
323+
"--literal-pathspecs".to_string(),
324+
"ls-files".into(),
325+
"-z".into(),
326+
"--".into(),
327+
];
328+
arguments.extend(batch.iter().cloned());
329+
let response = execute_git_readonly(&directory.to_string_lossy(), &arguments, None);
330+
lithe_protocol::cancellation::check()?;
331+
let Ok(response) = response else {
332+
return Ok(None);
333+
};
334+
if response.exit_code != 0 {
335+
return Ok(None);
336+
}
337+
for path in response.stdout.split('\0').filter(|path| !path.is_empty()) {
338+
// A gitlink prints the directory itself; files print a descendant.
339+
let owners = path
340+
.match_indices('/')
341+
.map(|(index, _)| &path[..index])
342+
.chain([path])
343+
.filter(|prefix| requested.contains(prefix))
344+
.collect::<Vec<_>>();
345+
if owners.is_empty() {
346+
return Ok(None);
347+
}
348+
tracked.extend(owners.into_iter().map(str::to_string));
349+
}
350+
}
351+
Ok(Some(tracked))
352+
}
353+
305354
/// Upper bound on the pathspec bytes passed to one `git ls-files` call.
306355
///
307356
/// Windows limits a whole command line to 32,767 UTF-16 units; batching well

‎rust/crates/project/src/files/snapshot.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ pub fn snapshot(request: WorkspaceSnapshotRequest) -> Result<WorkspaceSnapshotRe
99
search_index::invalidate_root(&root);
1010
let rules = VisibilityRules::new(request.hidden_directory_names, request.hidden_file_patterns)
1111
.with_workspace_roots(&root, &request.workspace_roots);
12+
if let Some(policy) = &indexing_policy {
13+
policy.prefetch_git_answers(&rules)?;
14+
}
1215
let mut files = Vec::new();
1316
let mut nested_worktrees = Vec::new();
1417
let mut incomplete_directories = Vec::new();

0 commit comments

Comments
 (0)