Use git ls-files -s instead of ls-tree for full-tree enumeration#2013
Draft
tyrielv wants to merge 1 commit into
Draft
Use git ls-files -s instead of ls-tree for full-tree enumeration#2013tyrielv wants to merge 1 commit into
tyrielv wants to merge 1 commit into
Conversation
6976987 to
b10a10b
Compare
When no previous commit exists to diff against (sourceTreeSha == null), DiffHelper.PerformDiff previously ran 'git ls-tree -r -t HEAD' which walks all tree objects. On a large repo with ~2.5M files, this takes ~24s. Replace with 'git ls-files -s' which reads the index instead of walking tree objects. Benchmarked at ~6.5s on the same repo — a 3.7x speedup. The optimization is only applied when targetTreeSha matches HEAD's tree, since ls-files reads the index (which reflects HEAD). When they differ (e.g., FastFetch checking out a non-HEAD commit), falls back to ls-tree to preserve correctness. Also falls back to ls-tree if ls-files fails (e.g., index does not exist on fresh git init before first checkout). Assisted-by: Claude Opus 4.6 Signed-off-by: Tyrie Vella <tyrielv@gmail.com>
b10a10b to
d4988aa
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When no previous commit exists to diff against (
sourceTreeSha == null),DiffHelper.PerformDiffrunsgit ls-tree -r -t HEADto enumerate all blobs and trees. This walks every tree object — very slow on large repos.Replace with
git ls-files -s, which reads the git index instead of walking tree objects. The index is already materialized in GVFS-mounted repos, making this significantly faster.The optimization only applies when the target tree matches HEAD (i.e., the index reflects the tree we need). This is always the case for
gvfs prefetch, which resolves HEAD as its target (PrefetchVerb.LoadBlobPrefetchArgs→RevParse(HEAD)). For other callers like FastFetch force-checkout (which can target a non-HEAD commit), the code falls back tols-treeto preserve correctness.Benchmark (repo with ~2.5M files)
git ls-tree -r -t HEAD(before)git ls-files -s(after)Also benchmarked libgit2 alternatives: in-process recursive tree walk (2.9× faster than ls-tree) and in-process index read (1.9× — marshaling overhead).
git ls-files -swas the fastest and simplest option.Changes
LsFilesStaging()method that runsgit ls-files -sParseFromLsFilesStagingLine()parser for the<mode> <sha> <stage>\t<path>formatPerformDiffnow usesls-files -swhensourceTreeSha == nullandtargetTreeShamatches HEAD's tree (verified via libgit2). Falls back tols-treeotherwise.Safety
TargetMatchesHeadTree()resolves HEAD's tree SHA via libgit2 and compares to the requestedtargetTreeSha. Only uses the index-based path when they match.ls-treeif the index is unavailable, HEAD can't be resolved, or the target differs from HEAD.ls-files -sonly returns file entries (not tree entries). Tree entries fromls-treewere only used for directory creation, whichFlushStagedQueueshandles from file paths anyway.