Use git ls-files -s instead of ls-tree for full-tree enumeration#2013
Use git ls-files -s instead of ls-tree for full-tree enumeration#2013tyrielv 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
| using (LibGit2Repo repo = new LibGit2Repo(this.tracer, this.enlistment.WorkingDirectoryBackingRoot)) | ||
| { | ||
| string headTreeSha = repo.GetTreeSha("HEAD"); | ||
| if (headTreeSha != null && string.Equals(headTreeSha, targetTreeSha, StringComparison.OrdinalIgnoreCase)) |
There was a problem hiding this comment.
The guard that decides whether to use the fast ls-files path compares HEAD's tree SHA against a value the real callers pass as a commit SHA, so the comparison may never be true and the optimization may never actually run.
| } | ||
|
|
||
| // ls-files -s only returns file entries, never trees | ||
| this.EnqueueFileAddOperation(activity, result); |
There was a problem hiding this comment.
The new ls-files path only enqueues file adds and never directory entries. FastFetch's checkout relies on directory operations to create folders before writing files, and the file writer does not create missing parent directories.
| DiffTreeResult blobAdd = new DiffTreeResult(); | ||
| blobAdd.TargetMode = Convert.ToUInt16(line.Substring(0, 6), 8); | ||
| blobAdd.TargetIsSymLink = blobAdd.TargetMode == SymLinkFileIndexEntry; | ||
| blobAdd.TargetSha = line.Substring(7, GVFSConstants.ShaStringLength); |
There was a problem hiding this comment.
ls-files -s reads the index, which can differ from HEAD's tree (staged or unmerged entries). The parser ignores the stage column, so unmerged paths could produce duplicate adds with the wrong blob SHA.
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.