Skip to content

fix(project): batch Git ignore checks per directory level - #60

Merged
xiaoyumuxi merged 1 commit into
mainfrom
fix/1994-batched-git-ignore-scan
Nov 15, 2026
Merged

xiaoyumuxi merged 1 commit into
mainfrom
fix/1994-batched-git-ignore-scan

Conversation

@xiaoyumuxi

Copy link
Copy Markdown
Contributor

关联 Issue / Linked issue

Lithe-IDEA 升级 PINNED_CORE_REVISION 到包含本修复的 commit 后,由那个 PR 关闭 #1994。

PR 类型 / Type

  • fix:缺陷修复

改动说明 / What changes

背景 / Why

在 Lithe 里打开 Kubernetes 这类大型 Go 仓库,gopls 要等约 30 秒才开始工作。原因是语言服务器启动前,lsp-engine 先调用 lithe_project::indexing::prepare_provider_exclusions 算出 Git 忽略的目录,作为 gopls 的 directoryFilters。旧实现为每个父目录串行运行一次 git check-ignore --stdin -z,被忽略的子目录再各运行一次 git ls-files,Git 进程数与目录数成正比:Kubernetes 上要 2,179 个进程、29.2 秒。

本 PR 把这些问题改成按目录层批量询问,进程数只与目录深度和仓库数有关,算出的排除结果不变。

改动 / Changes

lithe-git(rust/crates/git/src/status.rs)

  • 新增 tracked_directories(directory, directories):沿用现有的 pathspec_batches / LS_FILES_PATHSPEC_BUDGET 分批,一次 git --literal-pathspecs ls-files -z 回答一批目录下有没有已跟踪内容。gitlink 输出目录本身,普通文件输出其后代,两种都会归到对应目录。
  • Git 失败、退出码非零,或者输出的路径对应不到任何请求的目录(例如 Unicode 规范化不同)时返回 None,调用方回退到逐目录询问。

lithe-project 索引策略(rust/crates/project/src/indexing.rs)

  • IndexingContext 新增两个字段,configure、rollback_failed_policy、refreshed_context 三处构造新的一代时都重置为空:
    • prefetched:预取到的 Git 答案;
    • prefetched_scopes:已经预取过的可见性规则,它的锁同时让预取串行执行。
  • 新增 prefetch_git_answers(rules):
    • 从工作区根按层 BFS,把子目录按所属仓库分组(最近一个含 .git 的目录,没有则用工作区根),每层每个仓库只运行一次 check-ignore。
    • 被忽略、且不是嵌套仓库的子目录,再用一次批量 tracked_directories 判断。
    • 判断规则与 prepare_parent 相同:嵌套仓库或未被忽略的目录保留;被忽略但含已跟踪内容的目录保留;其余排除。
    • 只往遍历真正会询问的地方下探:未排除、已有答案、在调用方规则下可见、未委托给其他工作区根、不是嵌套 linked worktree、不在手动排除或包含目录之内。
    • 已有答案(decisions 或之前的预取)的目录不再重复询问。
  • 预取答案不写入 decisions:
    • prepare_parent 只在一个父目录的所有候选子目录都有预取答案时才采用;有任何缺失(例如预取之后新建的目录)就按原路径逐目录询问 Git。
    • 所以 excluded_roots(gopls 的 directoryFilters)和索引状态看到的排除集合与原来一致。
    • check-ignore 失败的答案仍按原规则:该目录记为保留,并设置 git_failed。
  • prepare_provider_exclusions 在 prepare_parent(root, "") 之前,用与随后快照相同的内置规则预取。
  • release_lease 在最后一个租约释放时,和 decisions 一起清空 prefetched。退役的记录会继续留在表里用来取消迟到的读者,不清空会一直占着与目录数同量级的内存。
  • 原 prepare_parent 的尾部逻辑抽成 store_decisions,行为不变。

快照与搜索索引(files/snapshot.rs、search_index.rs)

  • snapshot 和 WorkspaceSearchIndex::build 遍历前,用各自的可见性规则预取;随后遍历时 reason → prepare_parent 直接复用这些答案。

回归测试 indexing::tests::provider_preparation_asks_git_per_directory_level_not_per_parent(#[cfg(unix)])

  • 合成仓库:
    • 8 个包 × 4 个模块,.gitignore 忽略 generated/;
    • pkg0/mod0/generated/Tracked.txt 用 git add -f 跟踪;
    • pkg1/nested 是一个嵌套仓库,用自己的规则忽略 cache/。
  • 通过 GitExecutionOptions.executable 指向一个包装脚本,统计 Git 进程数。
  • 断言内容:
    • excluded_roots 与逐目录询问的结果一致:有已跟踪文件的 pkg0/mod0/generated 保留,只排除其下的 out,嵌套仓库的 cache 被排除;
    • 快照文件列表正确;
    • git_failed 未被设置;
    • check-ignore 不超过 5 次、ls-files 不超过 3 次;
    • 释放租约后预取被清空。

不在本 PR 范围内 / Out of scope

类型专属:fix

复现与根因

  • 复现:在 Kubernetes 检出(c50e5395,31,268 个已跟踪文件)上用 gitIgnoreEnabled: true 配置索引策略,然后调用 prepare_provider_exclusions。
    • 实际:耗时 29.2 秒,运行 2,179 次 git check-ignore,这段时间里 gopls 收不到第一代工作区设置。
    • 期望:耗时不随目录数量线性增长,排除结果不变。
  • 根因:c6c0135(feat(workspace): 支持目录索引排除并统一 Git 设置入口 1lck/Lithe-IDEA#1597,目录索引排除,合入时 Core 还在 Lithe-IDEA 仓库里)引入的 prepare_parent 按父目录逐个询问 Git,每次都要新起一个进程,并且是串行的。
  • 影响平台:macOS 和 Windows 共用这段 Core 逻辑。

检查项

  • 已先写回归测试,确认它修复前失败、修复后通过。
    • 测试路径:rust/crates/project/src/indexing.rs 中的 provider_preparation_asks_git_per_directory_level_not_per_parent。
    • 修复前:测试放在 main 的实现上,check-ignore ran 76 times, ls-files 33 times,耗时 1.82 s。
    • 修复后:通过,5 次 / 3 次,耗时 0.47 s。
    • 释放后清空的断言是后来加的,它用到了新字段,不能在 main 上编译。
  • [N/A] 手动复现:已经自动化。
  • 已检查全部调用方:
    • prepare_parent 只有两个调用方:prepare_provider_exclusions 和 reason,后者被 is_excluded 用于快照、搜索索引和目录列表。
    • lithe_git::ignored_paths 签名未变,另外两个调用方(history.rs、search_everywhere.rs)不受影响。
    • lsp-engine 只用 prepare_provider_exclusions 和 excluded_roots(engine.rs、configuration.rs、scan_settings.rs),接口未变。
  • 另一平台:两端用的是同一份 Core 修复。Windows 目标的 clippy 已通过,但没有在 Windows 上实机测量。
  • 只改了修复所需的代码,没有夹带重构;store_decisions 只是为了让两条路径共用写入逻辑。
  • [N/A] Agent Note:Core 仓库没有 .agents/notes/implemented/bug-fix/。“不得按目录逐个启动 Git 进程”这条约束由回归测试里的进程数断言守住。

范围检查 / Scope checks

  • 功能矩阵:N/A,没有新增宿主能力,跨平台行为不变,只是变快。
  • 插件归属:Core 改动不涉及任何具体插件;测试只用测试内临时创建的合成仓库。
  • UI / i18n:N/A,没有界面文字。
  • 统一日志系统:沿用原有的终态和 git_failed 降级路径,没有新增事件;generate-log-events.mjs --check 通过。
  • 逻辑归属:全部在 Rust Core。
  • 性能与内存:
    • 没有新增常驻进程、轮询或线程;预取在原来的调用线程上执行。
    • Git 进程数从“与目录数成正比”降到“目录层数 × 仓库数”。
    • prefetched 每个目录一项,与已有的 decisions 同量级;索引换代时重建,最后一个租约释放时清空。
    • 改动前后的基线见下方“验证”。
  • 设置:N/A。
  • 插件契约:N/A,没有改契约、能力版本、插件函数协议或宿主方法。
  • 发行包与资源:N/A,没有新增运行时资源、缓存文件或下载物,不写安装目录。
  • 无用代码:本次没有让任何代码变成无用。

验证 / Verification

本机为 Apple M4、24 GB 内存,Rust 1.100.0,Git 2.56.1。

命令或操作 平台 结果
cargo fmt --manifest-path rust/Cargo.toml --all -- --check macOS arm64 通过
node scripts/logging/generate-log-events.mjs --check macOS arm64 通过
scripts/rust-core/verify-rust-core-comments.sh core git lsp-engine project macOS arm64 通过
node scripts/rust-core/verify-platform-tests.mjs macOS arm64 通过
cargo test --locked --manifest-path rust/Cargo.toml -p lithe-core -p lithe-git -p lithe-lsp-engine -p lithe-project macOS arm64 1,233 个通过,0 个失败
cargo clippy --locked …(同上 4 个包)--all-targets --all-features -- -D warnings macOS arm64 通过
cargo clippy --locked …(同上 4 个包)--all-targets --target x86_64-pc-windows-msvc -- -D warnings macOS 交叉检查 Windows 目标 通过
回归测试先在 main 实现上运行 macOS arm64 按预期失败:check-ignore ran 76 times, ls-files 33 times

改动前后基线。 测量方法:

  • 用一个临时的 ignored 集成测试(未提交)调用 indexing::configure(gitIgnoreEnabled: true),再对 prepare_provider_exclusions 计时。
  • 之后输出 excluded_roots 和快照文件列表。
  • Git 进程数通过 GitExecutionOptions.executable 指向的计数包装脚本单独再跑一遍统计。
  • 测量期间机器空闲。
仓库 版本 prepare_provider_exclusions check-ignore ls-files 排除目录 快照文件
Kubernetes c50e5395 main 5802c42 29.2 s(29,203 / 29,212 / 29,281 ms) 2,179 0 0 31,211
Kubernetes c50e5395 本 PR 3.1 s(3,651 / 3,095 / 3,105 ms) 14 0 0 31,211
Lithe-IDEA 检出 2923ba3b5 main 5802c42 0.84 s 52 7 7 2,409
Lithe-IDEA 检出 2923ba3b5 本 PR 0.20 s 7 2 7 2,409
  • 每个仓库改动前后的排除目录和快照文件列表逐字节相同。
  • Kubernetes 的新鲜检出里没有被忽略的目录,所以另外用一个带被忽略构建目录和嵌套 linked worktree 的 Lithe-IDEA 检出,确认排除集合一致(7 个:.artifacts、.claude/worktrees、.codex、.idea、.lithe、dist、shared/fixtures)。
  • 剩余 3.1 s 的构成没有单独剖析。

未能执行的检查和原因:

  • 没有在 Windows 上运行测试或测量,只做了 Windows 目标的 clippy。scripts/rust-core/affected-crates.mjs 对本改动只选了 Linux 作业,没有 macOS / Windows 原生作业,所以 CI 也不会在 Windows 上运行这些测试。
  • 没有在 Lithe 桌面端端到端测量 gopls 启动时间:需要先在 Lithe-IDEA 升级 PINNED_CORE_REVISION,放在后续 PR 里做。

提交前确认 / Final checks

  • 分支从最新 main(5802c42)创建,目标分支 main。
  • 以 Draft 提交,本地验证已通过。
  • 没有无关改动、构建产物、凭据或个人路径;临时计时测试未提交。
  • 本次测量没有启动 Lithe 应用,没有进程残留。

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant