Skip to content

Conversation

@cho-m
Copy link
Member

@cho-m cho-m commented Nov 17, 2025

  • 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 lgtm (style, typechecking and tests) with your changes locally?

brew list <name> outputs all files in Cellar and hits Ruby code making it slow to run in a bash script. Could possibly use brew list --formula | grep <name> but that has more overhead than just a directory check while doing essentially the same check.

For example, when checking ruby command (with ruby formula installed), this is a 10x difference (5s vs 0.5s):

Before:

time (brew which-formula --skip-update --explain ruby)
The program 'ruby' can be found in the following formulae:
  * jruby
  * portable-ruby
  * ruby@3.1
  * ruby@3.2
  * ruby@3.3
Try: brew install <selected formula>
( brew which-formula --skip-update --explain ruby; )  3.16s user 1.73s system 93% cpu 5.246 total

After:

time (brew which-formula --skip-update --explain ruby)
The program 'ruby' can be found in the following formulae:
  * jruby
  * portable-ruby
  * ruby@3.1
  * ruby@3.2
  * ruby@3.3
Try: brew install <selected formula>
( brew which-formula --skip-update --explain ruby; )  0.25s user 0.15s system 73% cpu 0.545 total

Copilot AI review requested due to automatic review settings November 17, 2025 18:53
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR optimizes the is_formula_installed() function in brew which-formula by replacing a slow brew list --formula command with a simple directory existence check, achieving a 10x performance improvement (5s → 0.5s).

  • Replaces brew list --formula "${name}" with [[ -d "${HOMEBREW_CELLAR}/${name}" ]]
  • Achieves significant performance improvement by avoiding Ruby code execution and file enumeration
  • Maintains functional equivalence since formulae are installed as directories in HOMEBREW_CELLAR

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

Nice!

@MikeMcQuaid MikeMcQuaid added this pull request to the merge queue Nov 17, 2025
Merged via the queue into main with commit 9654554 Nov 17, 2025
43 checks passed
@MikeMcQuaid MikeMcQuaid deleted the which-formula-simpler-check branch November 17, 2025 19:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants