Skip to content

Cache formula lookups during dependency expansion#22930

Merged
MikeMcQuaid merged 1 commit into
mainfrom
memoize-dependency-expansion
Jul 4, 2026
Merged

Cache formula lookups during dependency expansion#22930
MikeMcQuaid merged 1 commit into
mainfrom
memoize-dependency-expansion

Conversation

@p-linnane

@p-linnane p-linnane commented Jul 3, 2026

Copy link
Copy Markdown
Member

During a single dependency expansion pass, Dependency.expand resolved every dependency edge with a fresh dep.to_formula, and FormulaInstaller#expand_dependencies_for_formula re-ran dep.satisfied? for equal edges. Homebrew's factory cache is disabled on the install/upgrade path, so each to_formula reloads the formula, and a shared low-level dependency reached through many edges is loaded once per edge. That is the repeated Formulary::FromAPILoader: loading work reported in #22896, worst on Linux when the implicit bubblewrap dependency is unsatisfied and pulled into many subtrees.

This adds two caches scoped to one expand_dependencies_for_formula call and discarded afterwards: a formula cache so equal edges reuse one resolved Formula, and a satisfied cache so equal edges check dep.satisfied? once. Both key on the full dependency identity via hash/eql? (including UsesFromMacOSDependency bounds), so subclass state is never collapsed. Behaviour is unchanged: the caches only remove repeated work, and formula_cache raises unless a cache_key is also given, so it is only usable where expansion results are already memoized.


  • Have you followed our Contributing guidelines?
  • Have you checked for other open Pull Requests for the same change?
  • Have you explained what your changes do? Performance claims (e.g. "this is faster") must include Hyperfine benchmarks.
  • Have you explained why you'd like these changes included, not just what they do?
  • For bug fixes, have you given step-by-step brew commands to reproduce the bug?
  • Have you written new tests (excluding integration tests)? Here's an example.
  • Have you successfully run brew lgtm (style, typechecking and tests) locally?

  • AI was used to generate or assist with generating this PR.

Claude Code (Opus 4.8) implemented the per-expansion caching and its tests and reviewed the change for correctness. I reviewed the diff, confirmed the new specs fail without the caches and pass with them, and ran brew lgtm plus targeted brew tests --only=dependency_expansion / --only=formula_installer.


Copilot AI review requested due to automatic review settings July 3, 2026 04:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 improves dependency expansion performance by avoiding repeated dep.to_formula loads and repeated dep.satisfied? checks during a single expansion pass, addressing the Linux slowdown described in #22896 where formula factory caching is disabled on install/upgrade paths.

Changes:

  • Add an optional per-expansion formula_cache to Dependency.expand and use it for recursive expansion to reuse resolved Formula objects for equal dependency edges.
  • Add per-expansion caching in FormulaInstaller#expand_dependencies_for_formula so equal dependency edges only run dep.satisfied? once per expansion call.
  • Add unit tests covering both caching behavior and ensuring UsesFromMacOSDependency bounds remain part of dependency identity (no incorrect cache collapsing).

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
Library/Homebrew/dependency.rb Adds optional formula_cache: support to reuse dep.to_formula results during expansion (guarded by cache_key).
Library/Homebrew/formula_installer.rb Introduces per-expansion formula_cache and satisfied_cache to cut redundant work in installer dependency expansion.
Library/Homebrew/test/dependency_expansion_spec.rb Adds tests proving formula caching works for equal dependencies and does not collapse differing uses_from_macos bounds.
Library/Homebrew/test/formula_installer_spec.rb Adds tests ensuring dep.satisfied? is evaluated once per equal edge and keeps uses_from_macos bounds distinct.

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

Signed-off-by: Patrick Linnane <patrick@linnane.io>

@MikeMcQuaid MikeMcQuaid left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks again @p-linnane!

@MikeMcQuaid MikeMcQuaid added this pull request to the merge queue Jul 4, 2026
Merged via the queue into main with commit ea377d5 Jul 4, 2026
41 checks passed
@MikeMcQuaid MikeMcQuaid deleted the memoize-dependency-expansion branch July 4, 2026 12:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants