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

Improve zsh completion performance #7766

Merged
merged 1 commit into from Jun 26, 2020
Merged

Improve zsh completion performance #7766

merged 1 commit into from Jun 26, 2020

Conversation

marlonrichert
Copy link
Contributor

@marlonrichert marlonrichert commented Jun 17, 2020

  • Move main completion cache handling to brew update.
  • Enable completion caching by default.
  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same change?
  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes? Here's an example.
  • Have you successfully run brew style with your changes locally?
  • Have you successfully run brew tests with your changes locally?

Improve perceived zsh completion performance by

  • delegating most of the cache handling to when brew update is being run, instead of doing it when the user tries to get a completion, since the user is expecting to wait during brew update anyway, and by
  • enabling caching of brew completions by default, since many users don't know how to enable this feature and since it's not enabled by default for new Zsh users.

I have not written tests for this, because I have no idea how to test this. I looked at other PRs for Zsh completions, but they don't seem to have included tests either. I guess no one really know how to test them.

Copy link

@nhooyr nhooyr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@MikeMcQuaid MikeMcQuaid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me so far! Haven't reviewed #7767 yet but it may be worth thinking about that, too.

Library/Homebrew/cmd/update-report.rb Outdated Show resolved Hide resolved
@marlonrichert
Copy link
Contributor Author

Makes sense to me so far! Haven't reviewed #7767 yet but it may be worth thinking about that, too.

@MikeMcQuaid I'm not sure what that PR is trying to do and how it relates to this one, but I'm open to any changes you want to suggest.

@MikeMcQuaid
Copy link
Member

@MikeMcQuaid I'm not sure what that PR is trying to do and how it relates to this one, but I'm open to any changes you want to suggest.

@marlonrichert It's a different approach. Try it out (it's been committed to master). I think it provides a sufficient enough speedup that this PR may now be unnecessary.

@marlonrichert
Copy link
Contributor Author

@MikeMcQuaid OK, thanks. I’ll try it out.

@marlonrichert
Copy link
Contributor Author

@MikeMcQuaid I just tested master and although completion for Homebrew commands is now faster, there's still a slight noticeable delay. Whereas with this PR, after doing brew update, completion for commands, formulas and casks is instantaneous. Plus, #7767 does nothing to speed up completion for Homebrew formulas or casks.

So, I hope you will still consider merging this. #7767 has not made it obsolete.


comp_cachename=brew_formulae
if _cache_invalid $comp_cachename; then
list=( $(brew search) )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about having the output of this be cached in HOMEBREW_CACHE like all_commands_list.txt on brew update instead? That would enable it to be used in Bash completion too. CC @alebcay for thoughts.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think ideally the formula list would be generated with Ruby and cached in HOMEBREW_CACHE and then consumed by whatever completions we'd like. We could still store a copy of this list in the Zsh completions cache if that makes the completion execution marginally faster.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__brew_installed_formulae and __brew_installed_casks could also be cached. Those are quite slow, too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think ideally the formula list would be generated with Ruby and cached in HOMEBREW_CACHE and then consumed by whatever completions we'd like. We could still store a copy of this list in the Zsh completions cache if that makes the completion execution marginally faster.

Homebrew's Zsh completion code could just read the completions straight out of HOMEBREW_CACHE, instead of calling brew or using Zsh's own caching mechanism. I need to test it, but I think it should be just as fast as using Zsh's completion cache.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Homebrew's Zsh completion code could just read the completions straight out of HOMEBREW_CACHE

I believe this is what we're doing right now after #7767 was merged. I left the existing completion cache code in since it would still be useful for the other Zsh completions (formulae and casks), and wouldn't hurt anything.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather not make the cmd/update.sh change given the discussion above, sorry. I'm open to other parts of this PR being merged. When cmd/update.sh fails: no-one can update Homebrew so I'm extremely reluctant to make changes there that aren't 100% necessary.

OK, I'll remove the cmd/update.sh changes from this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Please roll Library/Homebrew/utils/update-completions.zsh back into completions/zsh/_brew too. After that, though, I'm not sure what's actually changing here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What will be left are three things that speed up Zsh completions:

  • Enable Zsh completion caching for brew arguments by default. (Right now, it’s disabled by default.)
  • Use $(<file) instead of $(cat file). (The former is faster than the latter, because it does not spawn a new process, which the latter does.)
  • Don’t call _cache_invalid before calling _retrieve_cache. (The latter already calls the former. No need to call it twice.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, cool, sounds good 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MikeMcQuaid I made the changes. Please review.

* Move main completion cache handling to `brew update`.
* Enable completion caching by default.
Copy link
Member

@MikeMcQuaid MikeMcQuaid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense thanks @marlonrichert!

@MikeMcQuaid MikeMcQuaid merged commit 8546dc5 into Homebrew:master Jun 26, 2020
@BrewTestBot BrewTestBot added the outdated PR was locked due to age label Dec 27, 2020
@Homebrew Homebrew locked as resolved and limited conversation to collaborators Dec 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated PR was locked due to age
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants