Skip to content

running "git lfs track" in a large repository is very slow #1611

Closed

Description

@MatthiasEgli

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.

Activity

  1. technoweenie commented on Oct 26, 2016

    @technoweenie
    Contributor

    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.

  2. sinbad commented on Oct 27, 2016

    @sinbad
    Contributor

    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?

  3. MatthiasEgli commented on Oct 27, 2016

    @MatthiasEgli
    Author

    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.

  4. ttaylorr commented on Oct 27, 2016

    @ttaylorr
    Contributor

    Since git lfs track uses git ls-files underneath, we could take into account the .gitignore by giving the --exclude-standard flag to the ls-files command, as described: https://git-scm.com/docs/git-ls-files

  5. technoweenie commented on Oct 27, 2016

    @technoweenie
    Contributor

    Ah, git lfs track shouldn't be scanning 600k files then. Or at least, not the contents. It uses filepath.Walk.

    EDIT: to be clear, git lfs track "*.foo" will use ls-files, but that's not the case here.

  6. MatthiasEgli commented on Oct 27, 2016

    @MatthiasEgli
    Author

    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).

  7. ttaylorr commented on Oct 28, 2016

    @ttaylorr
    Contributor

    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.

  8. sinbad commented on Oct 29, 2016

    @sinbad
    Contributor

    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.

  9. sinbad commented on Oct 29, 2016

    @sinbad
    Contributor

    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.

  10. sinbad commented on Oct 31, 2016

    @sinbad
    Contributor

    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):

    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.

  11. sinbad commented on Oct 31, 2016

    @sinbad
    Contributor

    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?

  12. jeffrey-aguilera commented on Oct 31, 2016

    @jeffrey-aguilera

    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.

  13. sinbad commented on Oct 31, 2016

    @sinbad
    Contributor

    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.

  14. sinbad commented on Nov 1, 2016

    @sinbad
    Contributor

    Think I've made some progress 😄

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions