perf/sorbet: skip sorbet-runtime dispatch when runtime disabled (~6-8% faster)#22978
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes Homebrew’s Sorbet runtime-disabled path by reducing overhead in frequently-called T.* helpers, aiming to speed up common brew commands when $HOMEBREW_SORBET_RUNTIME is unset (the default for most users).
Changes:
- Updates
Library/Homebrew/standalone/sorbet.rbto avoid unnecessary dispatch into sorbet-runtime forT.cast,T.let,T.bind, andT.assert_type!when runtime checking is disabled. - Keeps the runtime-enabled (
$HOMEBREW_SORBET_RUNTIMEset) behavior unchanged.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When
$HOMEBREW_SORBET_RUNTIMEis unset (every user except CI and developers runningbrew tests/test-bot),standalone/sorbet.rbprependsTNoChecksso thatT.let/T.cast/T.bind/T.assert_type!default tochecked: false. Each call still pays two keyword-argument dispatches, though:TNoChecks#letand thensuperinto sorbet-runtime, which immediately returns viareturn value unless checked.These methods are called often enough for that dispatch to be measurable: for example,
EagerInitializeExtension#initializeruns sixT.lets on everyPathnameallocation. In a stackprof profile ofbrew upgrade --dry-runtaken with$HOMEBREW_SORBET_RUNTIMEunset,TNoChecks#letalone accounted for ~4% of samples.Changes
Make the
TNoChecksoverrides return the value directly instead of callingsuper, eliminating the second dispatch and sorbet-runtime's disabled-check branch entirely.Behavior is unchanged:
$HOMEBREW_SORBET_RUNTIMEset, as used bybrew testsandbrew test-bot) is untouched.checked: trueexplicitly, so nothing relied on opting back into validation while the runtime is disabled.checked:keyword remains accepted so any explicitchecked:call sites keep working.Benchmarks (Hyperfine, 10 runs each, warmup 2,
HOMEBREW_SORBET_RUNTIMEunset, clean trees, on the top 3 commands from the last 365 days of analytics):brew upgrade --dry-run(3rd most-run, ~30 outdated)brew install <installed formula>(2nd most-run)brew config(4th most-run)No new unit tests: the changed branch only loads when
$HOMEBREW_SORBET_RUNTIMEis unset, andbrew testsalways sets it, so the no-op path cannot be exercised from the test suite.brewcommands to reproduce the bug?brew lgtm(style, typechecking and tests) locally?Claude Code was used to profile representative commands with stackprof, identify the double dispatch, implement the change and run the benchmarks. Verified by smoke-testing commands with
$HOMEBREW_SORBET_RUNTIMEboth set and unset, checking there are no explicitchecked: truecall sites in the codebase, clean-tree before/after Hyperfine benchmarks and runningbrew lgtmlocally.