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
prefix.sh: support API mode #14622
prefix.sh: support API mode #14622
Conversation
Review period will end on 2023-02-15 at 04:35:14 UTC. |
Library/Homebrew/prefix.sh
Outdated
-z "${HOMEBREW_NO_INSTALL_FROM_API}" && | ||
-f "${HOMEBREW_CACHE}/api/formula.json" ]] | ||
then | ||
formula_exists="$(ruby -e "require 'json'; puts 1 if JSON.parse(File.read('${HOMEBREW_CACHE}/api/formula.json')).any? { |f| f['name'] == '${formula}' }" 2>/dev/null)" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use a heredoc for readability?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure
-f "${HOMEBREW_CACHE}/api/formula.json" ]] | ||
then | ||
formula_exists="$( | ||
ruby -rjson <<RUBY 2>/dev/null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure we're guaranteed a ruby
here, are we? Not a dealbreaker though, since I suppose we can still fallback to --prefix.rb
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But, if so (or even alternatively to avoid having to fire up ruby
), perhaps we can consider doing
[[ -h "${HOMEBREW_PREFIX}/opt/${formula}" ]]
somewhere earlier to handle the case where the formula is installed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're not, though that but would also affect brew formulae
. And yes, if it's not present it will fall back.
Maybe could do what _create_lock
does and fallback to Python and then something else.
In fact the consequences are worse for brew formulae
as it breaks shell completion etc so it may be worth improving that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or maybe just hardcode a PATH to portable-ruby vendor directory somewhere. System Ruby 2.6 outside of macOS is pretty much dead at this point so there's usually a portable Ruby installed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But, if so (or even alternatively to avoid having to fire up
ruby
), perhaps we can consider doing[[ -h "${HOMEBREW_PREFIX}/opt/${formula}" ]]somewhere earlier to handle the case where the formula is installed.
Oops missed this somehow. Decent idea actually since that'll be even faster than parsing a large JSON. The only difference is the deleted formulae case, which could be argued as an improvement.
Doesn't fix brew formulae
though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're not, though that but would also affect
brew formulae
.
I think it'd be nice to have a shared .sh
file with a function that both of these can use to do this JSON operation?
Or maybe just hardcode a PATH to portable-ruby vendor directory somewhere.
Let's not unless we get a bug report.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or, better still: extract all the Ruby code into a .rb
file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fine with me but would be nice to share some code here. Can you use hyperfine
to demonstrate the speedup, too? Want to make sure this actually meaningfully improves performance for the amount of somewhat gnarly code we're adding here.
-f "${HOMEBREW_CACHE}/api/formula.json" ]] | ||
then | ||
formula_exists="$( | ||
ruby -rjson <<RUBY 2>/dev/null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or, better still: extract all the Ruby code into a .rb
file.
Worst case before this PR - though this is connection dependent so may be several seconds for some people.
Best case before this PR:
After this PR:
|
@Bo98 performance looks great! |
Review period skipped due to |
@Bo98 another option that occurred to me for both places would be that we could consider having something in Ruby-land dump to a |
Yes, that's a very good idea. The remaining bottleneck compared to non-API is also the JSON parsing. |
@Bo98 and just shelling out to Ruby (which takes |
I've been thinking about this too. But I think I picked it up from an idea @Rylan12 mentioned in another PR. |
Yeah, I think this was a suggestion about improving autocompletions: basically to also write every formula/cask name to a text file when downloading the API stuff. Then using that text file to easily get formula completions in bash without needing to run |
Will look at the txt idea as a second pass as the improvements here are significant enough to not sit on (I'm not sure when 4.0.0 is and I might not be able to revisit this tonight - maybe tomorrow night). |
I noticed we already do this in
formulae.sh
:brew/Library/Homebrew/cmd/formulae.sh
Lines 14 to 22 in 2a417f1
So it makes sense to do in
prefix.sh
if we want to keep the speed advantages there too.If the JSON file is corrupt or missing, it will still fallback to prefix.rb as before where a download will occur.