Visitar URL original
Idea: Improve hook speed by skipping before/after diff via readonly flag · Issue #1564 · pre-commit/pre-commit · GitHub
The Wayback Machine - https://web.archive.org/web/20201130222459/https://github.com/pre-commit/pre-commit/issues/1564
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Idea: Improve hook speed by skipping before/after diff via readonly flag #1564

Open
jhenkens opened this issue Aug 20, 2020 · 18 comments
Open

Idea: Improve hook speed by skipping before/after diff via readonly flag #1564

jhenkens opened this issue Aug 20, 2020 · 18 comments
Labels

Comments

@jhenkens
Copy link
Contributor

@jhenkens jhenkens commented Aug 20, 2020 •

diff_cmd = ('git', 'diff', '--no-ext-diff')
diff_before = cmd_output_b(*diff_cmd, retcode=None)
if not hook.pass_filenames:
filenames = ()
time_before = time.time()
language = languages[hook.language]
retcode, out = language.run_hook(hook, filenames, use_color)
duration = round(time.time() - time_before, 2) or 0
diff_after = cmd_output_b(*diff_cmd, retcode=None)
# if the hook makes changes, fail the commit

Hi all,

I am looking at implemented pre-commit to replace a harder-to-maintain custom bash script our company current uses. In my initial tests, pre-commit works great, except for the fact that it is slow. The majority of our hooks are readonly in nature, and run quite fast. However, on our repository, pre-commit is spending close to 1 second per hook performing a before/after diff.

I added some instrumentation, and in our test case with 10 executed hooks, the execution time of pre-commit is >7 seconds with the before/after diffs, and ~1 second with those diffs disabled. I will note this is an extremely large repository, with many thousands of files. Generally, git actions are somewhat slow on it.

In exploring issues related to perf, I had found #510, which seems to tackle a different problem - individual hooks that take a while, rather than the overhead of pre-commit itself. However, there was one comment that got me thinking, regarding a readonly attribute one could apply to a hook. I know - this metadata would only be as truthy as the author who wrote the hook, but if a hook was marked as readonly, pre-commit could theoretically skip the before/after hook diffs.

Any thoughts? Happy to contribute the PR if maintainers see this as valuable.

@asottile
Copy link
Member

@asottile asottile commented Aug 20, 2020

this idea was contemplated at some point but was rejected as it would be difficult for two reasons:

  • plugin-based hooks wouldn't ever be able to set this accurately
  • if it was set innaccurately it would look like the framework's fault that it allowed through a commit
  • in tests it really only made a difference on windows (and even then, not a significant difference)

one thing you could pursue is reducing the amount of git diffs by half (instead of before/after, carry over the diff from the previous hook run). but even that wasn't pursued because it wasn't significant

@asottile asottile added the question label Aug 20, 2020
@jhenkens
Copy link
Contributor Author

@jhenkens jhenkens commented Aug 20, 2020 •

I'm still converting more hooks for our use case, so our runtime is only going to deteriorate further. I'd argue that, for our case, it is substantial. Our current (hard to maintain and grok) hooks execute in the 1-2 second range.

I am running all tests on a Mac (though much of our development team is on Windows, so if it is even slower there than that is a compounding problem).

plugin-based hooks wouldn't ever be able to set this accurately

Perhaps I am mistaken on what a plugin-based hook is - I'm looking at https://pre-commit.com/#plugins, but it seems that it is entirely possible the author could incorrectly set it, but not that they wouldn't ever be able to set it correctly. If I write a validate-only hook, and publish it somewhere, I can, as the author, know it doesn't modify any files, and have it set as readonly.

if it was set innaccurately it would look like the framework's fault that it allowed through a commit

Could this be remedied by performing one "global" before/after diff for the entire run of pre-commit? For non-readonly hooks, we can still perform the before/after diffs, but then add one global check if any hooks were readonly, and fail/complain loudly if a change was improper? Perhaps run all readonly hooks first, with one set of diff, then continue to others?

one thing you could pursue is reducing the amount of git diffs by half (instead of before/after, carry over the diff from the previous hook run).

That would get us to roughly 4 seconds, versus 1 second. Definitely worth an attempt in my book.

@asottile
Copy link
Member

@asottile asottile commented Aug 20, 2020

plugin-based hooks wouldn't ever be able to set this accurately

Perhaps I am mistaken on what a plugin-based hook is - I'm looking at https://pre-commit.com/#plugins, but it seems that it is entirely possible the author could incorrectly set it, but not that they wouldn't ever be able to set it correctly. If I write a validate-only hook, and publish it somewhere, I can, as the author, know it doesn't modify any files, and have it set as readonly.

things like flake8 where readonly would be most beneficial couldn't set this accurately as they are plugin systems themselves which could contain rewriting plugins

if it was set innaccurately it would look like the framework's fault that it allowed through a commit

Could this be remedied by performing one "global" before/after diff for the entire run of pre-commit? For non-readonly hooks, we can still perform the before/after diffs, but then add one global check if any hooks were readonly, and fail/complain loudly if a change was improper?

yeah that would be a potential trade off

@jhenkens
Copy link
Contributor Author

@jhenkens jhenkens commented Aug 20, 2020

things like flake8 where readonly would be most beneficial couldn't set this accurately as they are plugin systems themselves which could contain rewriting plugins

Fair enough - I guess I need more coffee, given I couldn't think of that! In that case, my argument would be that the hook definition should not set readonly: true, but instead the per-repo configuration could override it, just as can be done with nearly every other hook property. If I know my configuration of flake8 should be readonly, I can set it as such.

@asottile
Copy link
Member

@asottile asottile commented Aug 20, 2020

yeah the thing is nobody is going to do that, or worse everyone is going to cargo cult copy paste that everywhere :/

@asottile
Copy link
Member

@asottile asottile commented Aug 20, 2020

I think for this to be a thing we'd need the following at least:

  • an --audit-readonly or something similar flag to run to debug when hooks set this incorrectly
  • overall before/after diffing to catchall incorrect readonlys
  • some smarter in-between diffing (rw hook followed by 3 ro hooks followed by rw hook should invoke ~4 diffs, validate readonly correctness at the 5th hook and skip the global hook) -- this is going to be very complex I imagine

hmm the other issue is how should readonly default, most things I believe are readonly, but the safest default is readonly: false to preserve the existing behaviour

ah and I just thought of another problem case, sometimes you can change the readonlyness by setting args, for example autopep8 -i vs just autopep8

@jhenkens
Copy link
Contributor Author

@jhenkens jhenkens commented Aug 20, 2020

Yes - I agree. Can be quite complex in implementation. An additional global setting "run_readonly_hooks_first: true` could also benefit in reducing the number of in-between diffs. Unless I am mistaken, hooks run in the order they are defined, but given they may come from multiple repos, that can block manual ordering to obey without such a feature (unless there is some other sorting/ordering mechanism I am unaware of).

I think many tools/hooks will be left readonly: false by default, and up to the specific repo's implementation to set to true. However, some hooks, like check-added-large-files are always going to be readonly, and are easily set by the maintainer of said hook.

I definitely think this is a "power-user feature" to get the most benefit for any given client implementation, but that's not to say it isn't valuable. I think it will provide some benefit to all users of "stock hooks", once they are updated to mark readonly, but most benefit to those willing to put the effort in for best optimization.

@asottile
Copy link
Member

@asottile asottile commented Aug 20, 2020

yeah the thing is, in most cases and for most repos this makes zero difference but would bring on a whole bunch of complexity

@jhenkens
Copy link
Contributor Author

@jhenkens jhenkens commented Aug 20, 2020

I just tested with a small repo, and you are probably right. It ran all 10 checks in ~1 second.

I think this can be summed up as "pre-commit is not friendly with large repos". Unsure if it is spefically index size, or commit graph size.

Unfortunately for us, that means that pre-commit is unusable unless we fix or fork.

@asottile
Copy link
Member

@asottile asottile commented Aug 20, 2020

actually, taking a step back it's very strange that git diff isn't fast though 🤔 -- when I worked at yelp we ran pre-commit on a 6MLOC+ code base with millions of commits and git diff returned nearly instantly

@jhenkens
Copy link
Contributor Author

@jhenkens jhenkens commented Aug 20, 2020 •

git --version
git version 2.27.0

Small repo

time git diff --no-ext-diff > /dev/null

real	0m0.018s
user	0m0.003s
sys	0m0.009s

Large repo:

time git diff --no-ext-diff > /dev/null

real	0m0.321s
user	0m0.042s
sys	0m1.634s

Both repos only had one change, staged, the .pre-commit-config.yaml.

The "big repo" is in the order of 10s of thousands of files, and hundreds of thousands of commits.

The wall time isn't that dramatic - when running it by itself, it is indistinguishably slower than on a small repo. The problem comes when you run it 20 times in a script. That adds up to 6 seconds of waiting for diffs, which aligns perfectly with my numbers determined by additional logging in run.py.

Edit: Updated git to 2.28:

10:30 $ time git diff --no-ext-diff > /dev/null

real	0m0.260s
user	0m0.039s
sys	0m1.657s
@asottile
Copy link
Member

@asottile asottile commented Aug 20, 2020

anyway, I think the first step would be to try and cut the diffs calls in half if you'd like to approach that

@jhenkens
Copy link
Contributor Author

@jhenkens jhenkens commented Aug 25, 2020

Thanks for helping with the review of #1566 . Is there anything else from our discussion here you think might be worth to add to the main fork?

@asottile
Copy link
Member

@asottile asottile commented Aug 25, 2020

the readonly idea might be feasible, but might be best developed separately first and see how it functions (similar to how types was introduced)

@asottile
Copy link
Member

@asottile asottile commented Sep 10, 2020

one other incremental improvement that can be done here is language: pygrep and language: fail are always readonly so some work can be skipped there

@jhenkens
Copy link
Contributor Author

@jhenkens jhenkens commented Sep 10, 2020

@admorgan
Copy link

@admorgan admorgan commented Oct 19, 2020

I haven't run the numbers, but since a list of files has already been generated would diffing only the involved files take less time?

@asottile
Copy link
Member

@asottile asottile commented Oct 19, 2020

I don't think it would make it faster but you can certainly try that

in the worst case it'll be necessarily slower as it'll need to utilize xargs-like processing (multiple calls due to command line length limits)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.