You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
running "git lfs track" in a large repository is very slow #1611
Executing git lfs track in a large repository can be very slow. It takes >30s to run git lfs track in a repository with 31k files under version control and > 600k files in the same folder (mostly ignored in .gitignore) on modern hardware (most recent MacBook Pro at time of writing).
Being in itself an issue, it makes the usage of the popular tool Sourcetree unusable as it will run a git lfs track after every staging action.
Ahh, this is probably due to the fact that it's searching for existing files matching the pattern to see if they need to be in LFS. Users have had issues where they run git lfs track "*.psd", and then accidentally commit an existing PSD file without LFS.
@sinbad: Any idea why SourceTree is running track after every staging action? That doesn't seem correct. I think GitHub Desktop only calls it when you change the file types that are tracked.
@MatthiasEgli: Are you by chance tracking files individually, or are you tracking file extensions like *.psd? While it's possible to track individual files with LFS, it is not recommended for repositories with lots of files.
As for Git LFS itself, an easy workaround is modifying .gitattributes yourself. It may make sense to add an option to git lfs track (or even a git config option) that disables this feature too. It's nice for new users, but not great for repos with large trees.
SourceTree doesn't run git lfs track "blah" i.e. to change what's being tracked, unless you specifically ask for new files to be tracked, but it does use git lfs track (with no arguments) to get the list of tracked files when you stage files - this is to check whether you're adding oversized content without tracking them in LFS, so it can give you a helpful reminder. To avoid any issues with being out of sync with changes made outside it doesn't cache this information, and it's usually very fast.
It's possible that the process of searching deep directory structures for nested .gitattributes is what's making plain git lfs track slow?
In our case git lfs track is slow without any entries in .gitattributes (the file is empty). It is fast if I remove all the files which are not tracked by git (they are all ignored by respective lines in .gitignore). When starting to track files by adding entries to .gitattributes the performance doesn't change notably, e.g. it is still taking >30s to scan 600k files.
To me it looks like git lfs track does not consider .gitignore "early" enough, e.g. it scans all files without skipping folders/files mentioned in .gitignore.
Fat finger ticket closing. I agree with @technoweenie, git lfs track should not scan all the files which are already ignored by something in .gitignore, such that filepath.Walk only walks through a subset of the file tree (e.g. only the parts which are not ignored by git).
such that filepath.Walk only walks through a subset of the file tree
I'm not sure if this is possible in the Go implementation of filepath.Walk. That function looks at the directory structure on disk in order to perform its traversal.
Implementing a DFS or BFS across a directory structure that Git provides us would certainly be possible, but I'd worry about buffering a potentially large amount of directories in memory at one time.
filepath.Walk is notoriously inefficient on large directories because it walks in lexical order, and gets that stat info. The fastest directory scan is via simple File.Readdirnames - this is not sorted nor does it access stat info (like File.Readdir). You can potentially make it even faster via partitioning - launching a new goroutine for a recursive descent.
Pre-loading the .gitignore details at the start and skipping the descent into dirs matched by that is entirely possible too.
I've got a bit of a queue but I could take a crack at a more efficient replacement for filepath.Walk as above if you like, since this affects SourceTree it's kind of in my interests.
With 159350 files in 1452 dirs (max depth 5, each level 3-5 subdirs) I got these benchmarks from (3 runs shown on the same data):
filepath.Walk took 690.394824ms
tools.FastWalk took 228.529018ms
filepath.Walk took 653.240956ms
tools.FastWalk took 240.741351ms
filepath.Walk took 657.719823ms
tools.FastWalk took 244.931468ms
The gap expands significantly the larger the number of files. Still, both completed in under a second with more files than were quoted above, so this doesn't explain the 30 second delay the OP observed. I'll have to look into this further when I get more time.
It's possible filepath.Walk becomes extremely degenerate when most of the files are in the same directory. @MatthiasEgli you said you had 31k files under source control but "600k files in the same folder" - did you actually mean 600? I had a lot more files but maybe not the same distribution. How many leaf folders do you have?
The large number of files are generate by sbt; most are due to caches for incremental compilation. In our case, we have an sbt project with 33 sub-projects and approximately 15000 source files in 2500 directories. After numerous sbt compiles, there are over 600000 files and 32000 directories. All of the files created by sbt compile are in .gitignore.
OK - this code should also have the ability to load gitignores and skip them, I just haven’t written the tests for that part yet. Should be able to get a significant speed up from that too. More updates to come.
Folder with a bit over 500,000 files in it; 200,000 in the .git folder and 300,000 in a nested folder which is covered by .gitignore, a thousand or so scattered elsewhere:
filepath.Walk took 22.511937691s
tools.FastWalkGitRepo took 12.2449ms
filepath.Walk took 22.452540693s
tools.FastWalkGitRepo took 13.621396ms
Note the units: 22 seconds for filepath.Walk, 12-14 milliseconds for tools.FastWalkGitRepo 🤘 Most of that is gained via skipping those huge dirs; without that FastWalk only halves the time.
I've got a few broken tests in other areas I'll have to fix up then I'll submit a PR.
Activity
technoweenie commentedon Oct 26, 2016
Ahh, this is probably due to the fact that it's searching for existing files matching the pattern to see if they need to be in LFS. Users have had issues where they run
git lfs track "*.psd", and then accidentally commit an existing PSD file without LFS.@sinbad: Any idea why SourceTree is running
trackafter every staging action? That doesn't seem correct. I think GitHub Desktop only calls it when you change the file types that are tracked.@MatthiasEgli: Are you by chance tracking files individually, or are you tracking file extensions like
*.psd? While it's possible to track individual files with LFS, it is not recommended for repositories with lots of files.As for Git LFS itself, an easy workaround is modifying
.gitattributesyourself. It may make sense to add an option togit lfs track(or even a git config option) that disables this feature too. It's nice for new users, but not great for repos with large trees.sinbad commentedon Oct 27, 2016
SourceTree doesn't run
git lfs track "blah"i.e. to change what's being tracked, unless you specifically ask for new files to be tracked, but it does usegit lfs track(with no arguments) to get the list of tracked files when you stage files - this is to check whether you're adding oversized content without tracking them in LFS, so it can give you a helpful reminder. To avoid any issues with being out of sync with changes made outside it doesn't cache this information, and it's usually very fast.It's possible that the process of searching deep directory structures for nested
.gitattributesis what's making plaingit lfs trackslow?MatthiasEgli commentedon Oct 27, 2016
In our case
git lfs trackis slow without any entries in.gitattributes(the file is empty). It is fast if I remove all the files which are not tracked bygit(they are all ignored by respective lines in.gitignore). When starting to track files by adding entries to.gitattributesthe performance doesn't change notably, e.g. it is still taking >30s to scan 600k files.To me it looks like
git lfs trackdoes not consider.gitignore"early" enough, e.g. it scans all files without skipping folders/files mentioned in.gitignore.ttaylorr commentedon Oct 27, 2016
Since
git lfs trackusesgit ls-filesunderneath, we could take into account the.gitignoreby giving the--exclude-standardflag to thels-filescommand, as described: https://git-scm.com/docs/git-ls-filestechnoweenie commentedon Oct 27, 2016
Ah,
git lfs trackshouldn't be scanning 600k files then. Or at least, not the contents. It usesfilepath.Walk.EDIT: to be clear,
git lfs track "*.foo"will usels-files, but that's not the case here.MatthiasEgli commentedon Oct 27, 2016
Fat finger ticket closing. I agree with @technoweenie,
git lfs trackshould not scan all the files which are already ignored by something in.gitignore, such thatfilepath.Walkonly walks through a subset of the file tree (e.g. only the parts which are not ignored by git).ttaylorr commentedon Oct 28, 2016
I'm not sure if this is possible in the Go implementation of
filepath.Walk. That function looks at the directory structure on disk in order to perform its traversal.Implementing a DFS or BFS across a directory structure that Git provides us would certainly be possible, but I'd worry about buffering a potentially large amount of directories in memory at one time.
sinbad commentedon Oct 29, 2016
filepath.Walkis notoriously inefficient on large directories because it walks in lexical order, and gets thatstatinfo. The fastest directory scan is via simpleFile.Readdirnames- this is not sorted nor does it accessstatinfo (likeFile.Readdir). You can potentially make it even faster via partitioning - launching a new goroutine for a recursive descent.Pre-loading the
.gitignoredetails at the start and skipping the descent into dirs matched by that is entirely possible too.sinbad commentedon Oct 29, 2016
I've got a bit of a queue but I could take a crack at a more efficient replacement for
filepath.Walkas above if you like, since this affects SourceTree it's kind of in my interests.sinbad commentedon Oct 31, 2016
First bash at a fast file walker: https://github.com/github/git-lfs/compare/fast-directory-walk
With 159350 files in 1452 dirs (max depth 5, each level 3-5 subdirs) I got these benchmarks from (3 runs shown on the same data):
The gap expands significantly the larger the number of files. Still, both completed in under a second with more files than were quoted above, so this doesn't explain the 30 second delay the OP observed. I'll have to look into this further when I get more time.
sinbad commentedon Oct 31, 2016
It's possible
filepath.Walkbecomes extremely degenerate when most of the files are in the same directory. @MatthiasEgli you said you had 31k files under source control but "600k files in the same folder" - did you actually mean 600? I had a lot more files but maybe not the same distribution. How many leaf folders do you have?jeffrey-aguilera commentedon Oct 31, 2016
The large number of files are generate by sbt; most are due to caches for incremental compilation. In our case, we have an sbt project with 33 sub-projects and approximately 15000 source files in 2500 directories. After numerous
sbt compiles, there are over 600000 files and 32000 directories. All of the files created bysbt compileare in.gitignore.sinbad commentedon Oct 31, 2016
OK - this code should also have the ability to load gitignores and skip them, I just haven’t written the tests for that part yet. Should be able to get a significant speed up from that too. More updates to come.
sinbad commentedon Nov 1, 2016
Think I've made some progress 😄
Folder with a bit over 500,000 files in it; 200,000 in the
.gitfolder and 300,000 in a nested folder which is covered by.gitignore, a thousand or so scattered elsewhere:Note the units: 22 seconds for
filepath.Walk, 12-14 milliseconds fortools.FastWalkGitRepo🤘 Most of that is gained via skipping those huge dirs; without thatFastWalkonly halves the time.I've got a few broken tests in other areas I'll have to fix up then I'll submit a PR.