Skip to content

Proposal: Access Git LFS cache from Git core (100x speed up!) #1329

Closed

Description

@larsxschneider

I found a way to make Git LFS faster up to a factor of 100x in
repositories with a large number of Git LFS files by accessing the Git LFS
cache directly from Git core. I reached out to the Git Community asking if
my approach would be acceptable for them. @sinbad @technoweenie
@ttaylorr what do you think about the proposed solution below?

What is the problem with Git LFS?

Git LFS is an application that is executed via Git clean/smudge filter.
The process invocation of these filters requires noticeable time (especially
on Windows) even if the Git LFS executable only accesses its local cache.

Based on my previous findings [5] Steve Streeting (@sinbad) improved the
clone times of Git LFS repositories with a lot of files by a factor of 10
or more [6][7].

Unfortunately that fix helps only with cloning. Any local Git operation
that invokes the clean/smudge filter (e.g. switching branches) is still
slow (see #1301).

Proposed solution

Git LFS caches its objects under .git/lfs/objects. Most of the time Git
LFS objects are already available in the cache (e.g. if you switch branches
back and forth). I implemented these "cache hits" natively in Git.
Please note that this implementation is just a quick and dirty proof of
concept. If the Git community agrees that this kind of approach would be
acceptable then I will start to work on a proper patch series with cross
platform support and unit tests.
Patch: larsxschneider/git@d75366a

Performance tests

I executed both test runs on a 2,5 GHz Intel Core i7 with SSD and OS X.
A test run is the consecutive execution of four Git commands:

  1. clone the repo
  2. checkout to the "removed-files" branch
  3. timed: checkout the "master" branch
  4. timed: checkout "removed-files" branch

Test command:

set -x; git lfs clone https://github.com/larsxschneider/lfstest-manyfiles.git repo; cd repo; git checkout removed-files; time git checkout master; time git checkout removed-files

I compiled Git with the following flags:

NO_GETTEXT=YesPlease NEEDS_SSL_WITH_CRYPTO=YesPlease make -j 8 CFLAGS="-I/usr/local/opt/openssl/include" LDFLAGS="-L/usr/local/opt/openssl/lib"

TEST RUN A -- Default Git 2.9 (ab7797d) and Git LFS 1.2.1

+ git lfs clone https://github.com/larsxschneider/lfstest-manyfiles.git repo
Cloning into 'repo'...
warning: templates not found /Users/lars/share/git-core/templates
remote: Counting objects: 15012, done.
remote: Total 15012 (delta 0), reused 0 (delta 0), pack-reused 15012
Receiving objects: 100% (15012/15012), 2.02 MiB | 1.77 MiB/s, done.
Checking connectivity... done.
Checking out files: 100% (15001/15001), done.
Git LFS: (15000 of 15000 files) 0 B / 77.04 KB
+ cd repo
+ git checkout removed-files
Branch removed-files set up to track remote branch removed-files from origin.
Switched to a new branch 'removed-files'
+ git checkout master
Checking out files: 100% (12000/12000), done.
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.

real    6m2.979s
user    2m39.066s
sys 2m41.610s
+ git checkout removed-files
Switched to branch 'removed-files'
Your branch is up-to-date with 'origin/removed-files'.

real    0m1.310s
user    0m0.385s
sys 0m0.881s

TEST RUN B -- Default Git 2.9 with native LFS cache and Git LFS 1.2.1

https://github.com/larsxschneider/git/tree/lfs-cache

+ git lfs clone https://github.com/larsxschneider/lfstest-manyfiles.git repo
Cloning into 'repo'...
warning: templates not found /Users/lars/share/git-core/templates
remote: Counting objects: 15012, done.
remote: Total 15012 (delta 0), reused 0 (delta 0), pack-reused 15012
Receiving objects: 100% (15012/15012), 2.02 MiB | 1.44 MiB/s, done.
Checking connectivity... done.
Git LFS: (15001 of 15000 files) 0 B / 77.04 KB
+ cd repo
+ git checkout removed-files
Branch removed-files set up to track remote branch removed-files from origin.
Switched to a new branch 'removed-files'
+ git checkout master
Checking out files: 100% (12000/12000), done.
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.

real    0m2.267s
user    0m0.295s
sys 0m1.948s
+ git checkout removed-files
Switched to branch 'removed-files'
Your branch is up-to-date with 'origin/removed-files'.

real    0m0.715s
user    0m0.072s
sys 0m0.672s

Results

Default Git:                      6m2.979s + 0m1.310s = 364s
Git with native LFS cache access: 0m2.267s + 0m0.715s = 4s

The native cache solution is almost 100x faster when switching branches
on my local machine with a test repository containing 15,000 Git LFS files.
Based on my previous experience with Git LFS clone I expect even more
dramatic results on Windows.

Thanks,
Lars

[1] https://git-lfs.github.com/
[2] https://github.com/blog/1986-announcing-git-large-file-storage-lfs
[3] http://blogs.atlassian.com/2016/02/git-lfs-for-designers-game-developers-architects/
[4] https://about.gitlab.com/2015/11/23/announcing-git-lfs-support-in-gitlab/
[5] #931 (comment)
[6] #988
[7] https://developer.atlassian.com/blog/2016/04/git-lfs-12-clone-faster/
[8] http://article.gmane.org/gmane.comp.version-control.git/297809

Activity

  1. technoweenie commented on Jun 27, 2016

    @technoweenie
    Contributor

    I think this is a great idea. Though I can understand the git community not wanting to embed checks for .git/lfs/objects in the core git code. Here's a response to your mailing list question:

    Would it be possible to move all this code to a separate daemon?
    Instead of spawning a new process to do the filtering, you send a
    command "convert this" over maybe unix socket and either receive the
    whole result over the socket, or receive a path of the result.

    I don't think hard coding "git-lfs" is a good way to go (if you keep
    that in the final impl. of course). I guess the costly part is
    spawning processes and going through the same process initialization
    for every object. If we keep a daemon running, all that is gone. You
    still have to pay for extra context switches and memory copy (unless
    you send the path, but then it could be racy), but I think that's
    negligible. And all smudge/clean filters can do caching and more if
    they want to.

    What do you think about a daemon that git manages?

  2. larsxschneider commented on Jun 29, 2016

    @larsxschneider
    MemberAuthor

    I considered a daemon, but a daemon makes it always
    harder for the user as the user needs to ensure the daemon is
    running!

    I think the core problem is that we invoke the filter for every file:
    https://github.com/git/git/blob/master/convert.c#L461-L475

    Couldn't we start the filter executable at the beginning of the Git process
    and communicate with it via stdin/stdout whenever we hit the Git filter
    code? Would that work?

    http://thread.gmane.org/gmane.comp.version-control.git/298293/focus=298455

    Would you support that from the Git LFS side?

  3. technoweenie commented on Jun 29, 2016

    @technoweenie
    Contributor

    Yup! I imagine that'd have to be a new type of smudge filter though, right?

  4. larsxschneider commented on Jun 29, 2016

    @larsxschneider
    MemberAuthor

    Not necessarily. We could make this an option to a smudge filter (e.g. activated with a flag streaming or something).

    [filter "lfs"]
        clean = git-lfs clean %f
        smudge = git-lfs smudge %f
        required = true
        streaming = true
    

    (actually right now I am not sure if I really understood what you meant)

  5. technoweenie commented on Jun 29, 2016

    @technoweenie
    Contributor

    Cool, that works. Though if there's no easy way for git lfs smudge to detect streaming mode, we could easily add a new command.

  6. sinbad commented on Jul 4, 2016

    @sinbad
    Contributor

    Just catching up with this after vacation. I'd love for there to be a streaming option to allow filter processes which exist for the life of the git process.

    Defining the stream format could be slightly challenging though unless it also includes some size metadata, since if you only have the usual system streams to rely on, delimiting between files in the binary data stream requires knowing how many bytes to read until you're done (no EOF, and binary data means you can't use control characters). Using a simple protocol could allow for size to be pre-negotiated, or alternatively the processes could negotiate the creation of a separate pipe for each content item so you can still use EOF to terminate and not have to pre-calculate the size (not a problem for us because we know the size, but maybe that might be a requirement git won't like)

  7. larsxschneider commented on Jul 5, 2016

    @larsxschneider
    MemberAuthor

    My plan is not to stream the file content. I plan to implement it on top of https://github.com/gitster/git/commits/jh/clean-smudge-annex and stream just the file names. I am not sure if this works. I will start looking into the implementation on Friday this week and report my results here.

    On 04 Jul 2016, at 15:41, Steve Streeting [email protected] wrote:

    Just catching up with this after vacation. I'd love for there to be a streaming option to allow filter processes which exist for the life of the git process.

    Defining the stream format could be slightly challenging though unless it also includes some size metadata, since if you only have the usual system streams to rely on, delimiting between files in the binary data stream requires knowing how many bytes to read until you're done (no EOF, and binary data means you can't use control characters). Using a simple protocol could allow for size to be pre-negotiated, or alternatively the processes could negotiate the creation of a separate pipe for each content item so you can still use EOF to terminate and not have to pre-calculate the size (not a problem for us because we know the size, but maybe that might be a requirement git won't like)

    —
    You are receiving this because you authored the thread.
    Reply to this email directly, view it on GitHub, or mute the thread.

  8. larsxschneider commented on Jul 10, 2016

    @larsxschneider
    MemberAuthor

    I just posted my proposal to the Git list:
    http://thread.gmane.org/gmane.comp.version-control.git/299215

    @sinbad @technoweenie : Do you think that could work from a Git-LFS perspective? Do you see any obvious problems that I missed?


    Git's clean/smudge mechanism invokes an external filter process for every
    single file that is affected by the filter. If you have a lot of files to
    filter then the startup time of the external filter process becomes a problem
    as discussed here:
    http://thread.gmane.org/gmane.comp.version-control.git/298293

    This patch might be a solution to this problem and I am looking for feedback.
    Please note that this is a RFC/proof-of-concept patch. If the list agrees with
    this approach then I will post a proper patch series with more tests, error
    handling, and documentation.

    How does it work?

    Git's "convert.c" got a hashmap that maps a filter command to a process. If
    we need to filter a file, then we look into the hashmap. If the filter command
    is not there then we create and start a new filter process and setup in/out
    pipes. If the filter command is there then we just take the existing and running
    process.

    In case of a clean filter we send the filename of the file to clean via pipe
    to the filter process. The filter process reads the file, generates the cleaned
    version and sends it back to Git via pipe.

    In case of a smudge filter we send the destination filename of the file to
    smudge via pipe as well as its content size and content. The filter process
    reads all this info, generates the smudged version and stores the smudged
    version at the location of the destination filename. Finally, the filter
    process sends an "OK" to tell Git that everything worked as expected.

    This patch is based on Joey Hess' great "clean-smudge-annex" topic, idea,
    and implementation:
    https://github.com/gitster/git/tree/jh/clean-smudge-annex

    v1: http://thread.gmane.org/gmane.comp.version-control.git/297475
    v4 (latest): http://thread.gmane.org/gmane.comp.version-control.git/297994

    You can find a branch with with my patch on top of Joey's work here:
    https://github.com/larsxschneider/git/commits/clean-smudge-file-long-running/v1

    Questions

    (1) Do you see a general problem with this kind of "pipe" protocol and long
    running Git filter processes? Other than the test cases I haven't implemented
    a real world filter, yet (on my todo list for tomorrow).

    (2) Joey's topic, which is the base for my patch, looks stalled for more than
    2 weeks:
    http://thread.gmane.org/gmane.comp.version-control.git/297994/focus=298006
    I would be happy to address Junio's comments and post a reroll. However,
    I don't want to interfere with Joey's plans. How does the list usually
    address this kind of situation?

    @joey (in case you are reading this):
    My patch changes your initial idea quite a bit. However, I believe it is an
    improvement that could be beneficial for git-annex, too. Would you prefer to
    work with me on the combination of our ideas (file clean/smudge + long running
    clean/smudge processes) or would you prefer to keep your interface?

    (3) Clean/smudge filter use Git's async command API to create a separate thread
    and start the filter process. In my implementation I start the filter process
    directly as this eases the pipe communication. I assume I am missing something
    obvious here, but what is the advantage of the async command API? Git needs to
    wait for the result of the filter anyways and therefore async is not really
    necessary, or?

    (4) I am not really experienced with process pipes. Is it OK to use the
    "strbuf_read_once" function to read from a pipe? If I use "strbuf_read" then
    the process waits indefinitely. Is there a way to tell Git to read X bytes from
    a pipe (I just found the size "hint")?

    (5) As mentioned above "convert.c" got a hashmap that maps a filter command to
    a process. At the end of the Git process I would like to close the pipes and
    stop the filter processes. What would be the best place in the code to do that?

  9. sinbad commented on Jul 11, 2016

    @sinbad
    Contributor

    Yeah, that would work, although it's worth bearing in mind as I've commented before about needing the size information if using pipes to exchange multiple sets of content. This is catered for already in the case of the smudge filter, where you say that git sends the size information and the content to the smudge filter, but in the clean filter you don't mention the size, but say the content is sent back to git along the pipe. In order to re-use that pipe the clean filter would have to tell git how big the incoming data was ahead of time.

  10. larsxschneider commented on Jul 12, 2016

    @larsxschneider
    MemberAuthor
  11. arikon commented on Oct 15, 2016

    @arikon

    @larsxschneider Do you have any progress here?

  12. technoweenie commented on Oct 16, 2016

    @technoweenie
    Contributor

    The patch is still going through review on the git-core list. It's currently in its 10th iteration.

  13. larsxschneider commented on Oct 17, 2016

    @larsxschneider
    MemberAuthor

    @technoweenie is right, I am still working on it and I just send out the 11th iteration. Keep your fingers crossed that this one gets accepted 😄 👍

  14. tofutim commented on Oct 21, 2016

    @tofutim

    Hi, I'm trying to checkout a test project with 1000s of files in lfs. As you can imagine, I'm dying and I expect my team to balk on lfs if they see how long it takes to download. @larsxschneider, is it safe to use your version? Is there a way to download and try it?

  15. larsxschneider commented on Oct 22, 2016

    @larsxschneider
    MemberAuthor

    The Git core part of the patch was accepted by the Git maintainer:
    git/git@ffd0de0

    It is scheduled to be merged to master (search for "ls/filter-process"):
    http://public-inbox.org/git/[email protected]/

    @tofutim:
    I would recommend to wait until the next Git core release (probably in November/December). By that time the Git LFS counterpart will be ready and 1000s of files in LFS won't be a problem anymore.

    I just gave a talk on Git LFS with a few tips and tricks how to handle a lot of files today:
    https://www.youtube.com/watch?v=3seDPCi1FnA

  16. arikon commented on Nov 3, 2016

    @arikon

    @larsxschneider Congrats on patch to git approval! Do you have a patch for git-lfs in any state?

  17. larsxschneider commented on Nov 3, 2016

    @larsxschneider
    MemberAuthor

    Thanks! @ttaylorr is actively working on the Git-LFS patch ✨🤘🎉👏
    #1617

  18. corinaPH commented on Dec 8, 2016

    @corinaPH

    Was this patch included in a release?
    Thanks

  19. larsxschneider commented on Dec 8, 2016

    @larsxschneider
    MemberAuthor
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