***Update**: after rebasing on top of https://github.com/zed-industries/zed/pull/16915/ (that also changed how search worked), the results are still good, but not 10x. Instead of going from 10s to 500ms, it goes from 10s to 3s.* This improves the performance of project-wide search by an order of magnitude. After digging in, @as-cii and I found that opening buffers was the bottleneck for project-wide search (since Zed opens, parses, ... buffers when finding them, which is something VS Code doesn't do, for example). So this PR improves the performance of opening multiple buffers at once. It does this by doing two things: - It batches scan-requests in the worktree. When we search, we search files in chunks of 64. Previously we'd handle all 64 scan requests separately. The new code checks if the scan requests can be batched and if so it, it does that. - It batches `git status` calls when reloading the project entries for the opened buffers. Instead of calling `git status` for each file, it calls `git status` for a batch of files, and then extracts the status for each file. (It has to be said that I think the slow performance on `main` has been a regression introduced over the last few months with the changes made to project/worktree/git. I don't think it was this slow ~5 months ago. But I also don't think it was this fast ~5 months ago.) ## Benchmarks | Search | Before | After (without https://github.com/zed-industries/zed/pull/16915) | After (with https://github.com/zed-industries/zed/pull/16915) |--------|--------|-------|------| | `zed.dev` at `2b2a501192e78e`, searching for `<` (`4484` results) | 3.0s<br>2.9s<br>2.89s | 489ms<br>517ms<br>476ms | n/a | | `zed.dev` at `2b2a501192e78e`, searching for `:` (`25886+` results) | 3.9s<br>3.9s<br>3.8s | 70ms<br>66ms<br>72ms | n/a | | `zed` at `55dda0e6af`, searching for `<` (`10937+` results) | 10s<br>11s<br>12s | 500m<br>499ms<br>543ms | 3.4s<br>3.1s<br> | (All results recorded after doing a warm-up run that would start language servers etc.) Release Notes: - Performance of project-wide search has been improved by up to 10x. --------- Co-authored-by: Antonio <antonio@zed.dev>
102 lines
2.9 KiB
Rust
102 lines
2.9 KiB
Rust
use crate::repository::{GitFileStatus, RepoPath};
|
|
use anyhow::{anyhow, Result};
|
|
use std::{
|
|
path::{Path, PathBuf},
|
|
process::{Command, Stdio},
|
|
sync::Arc,
|
|
};
|
|
|
|
#[derive(Clone)]
|
|
pub struct GitStatus {
|
|
pub entries: Arc<[(RepoPath, GitFileStatus)]>,
|
|
}
|
|
|
|
impl GitStatus {
|
|
pub(crate) fn new(
|
|
git_binary: &Path,
|
|
working_directory: &Path,
|
|
path_prefixes: &[PathBuf],
|
|
) -> Result<Self> {
|
|
let mut child = Command::new(git_binary);
|
|
|
|
child
|
|
.current_dir(working_directory)
|
|
.args([
|
|
"--no-optional-locks",
|
|
"status",
|
|
"--porcelain=v1",
|
|
"--untracked-files=all",
|
|
"-z",
|
|
])
|
|
.args(path_prefixes.iter().map(|path_prefix| {
|
|
if *path_prefix == Path::new("") {
|
|
Path::new(".")
|
|
} else {
|
|
path_prefix
|
|
}
|
|
}))
|
|
.stdin(Stdio::null())
|
|
.stdout(Stdio::piped())
|
|
.stderr(Stdio::piped());
|
|
|
|
#[cfg(windows)]
|
|
{
|
|
use std::os::windows::process::CommandExt;
|
|
child.creation_flags(windows::Win32::System::Threading::CREATE_NO_WINDOW.0);
|
|
}
|
|
|
|
let child = child
|
|
.spawn()
|
|
.map_err(|e| anyhow!("Failed to start git status process: {}", e))?;
|
|
|
|
let output = child
|
|
.wait_with_output()
|
|
.map_err(|e| anyhow!("Failed to read git blame output: {}", e))?;
|
|
|
|
if !output.status.success() {
|
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
|
return Err(anyhow!("git status process failed: {}", stderr));
|
|
}
|
|
|
|
let stdout = String::from_utf8_lossy(&output.stdout);
|
|
let mut entries = stdout
|
|
.split('\0')
|
|
.filter_map(|entry| {
|
|
if entry.is_char_boundary(3) {
|
|
let (status, path) = entry.split_at(3);
|
|
let status = status.trim();
|
|
Some((
|
|
RepoPath(PathBuf::from(path)),
|
|
match status {
|
|
"A" | "??" => GitFileStatus::Added,
|
|
"M" => GitFileStatus::Modified,
|
|
_ => return None,
|
|
},
|
|
))
|
|
} else {
|
|
None
|
|
}
|
|
})
|
|
.collect::<Vec<_>>();
|
|
entries.sort_unstable_by(|a, b| a.0.cmp(&b.0));
|
|
Ok(Self {
|
|
entries: entries.into(),
|
|
})
|
|
}
|
|
|
|
pub fn get(&self, path: &Path) -> Option<GitFileStatus> {
|
|
self.entries
|
|
.binary_search_by(|(repo_path, _)| repo_path.0.as_path().cmp(path))
|
|
.ok()
|
|
.map(|index| self.entries[index].1)
|
|
}
|
|
}
|
|
|
|
impl Default for GitStatus {
|
|
fn default() -> Self {
|
|
Self {
|
|
entries: Arc::new([]),
|
|
}
|
|
}
|
|
}
|