Cache RBS rewrites with bootsnap & add dsl --only-bootsnap-rbs-cache#2617
Open
Cache RBS rewrites with bootsnap & add dsl --only-bootsnap-rbs-cache#2617
dsl --only-bootsnap-rbs-cache#2617Conversation
7cbcfaa to
7ce6d57
Compare
Set up bootsnap with a dedicated cache directory and load require-hooks so the RBS-to-sig rewrite is baked into the cached bytecode. The first run is slow (rewrite + compile every file); subsequent runs against the same cache directory skip the rewrite entirely. Cache directory defaults to `tmp/cache/bootsnap-tapioca-rbs` (override with `TAPIOCA_BOOTSNAP_CACHE_DIR`); kept separate from the host app's regular bootsnap cache so rewritten iseqs don't leak into other processes. `BOOTSNAP_READONLY=1` flows through to bootsnap for consumers reading a pre-populated cache. `Bootsnap.log_stats!` prints hit/miss counts at exit so cache effectiveness is visible.
6e4b544 to
4676c1e
Compare
KaanOzkan
commented
May 8, 2026
| # | ||
| # The host app must also skip its own Bootsnap.setup under this flag, | ||
| # otherwise it'll override our settings during Rails boot. | ||
| if ENV["TAPIOCA_RBS_CACHE"] == "1" |
Contributor
Author
There was a problem hiding this comment.
Rewriter is loaded before any CLI parsing so went with a ENV variable.
`--only-bootsnap-rbs-cache` boots the app and loads DSL extensions/compilers (populating the RBS rewrite cache), then exits before any RBI work. Run it once in a CI prime step so downstream jobs read from a warm cache. The flag is mutually exclusive with `--verify`.
`bootsnap` is an optional dependency. `lib/tapioca/rbs/rewriter.rb` only requires it under `TAPIOCA_RBS_CACHE=1`, so it's not in the Gemfile and Sorbet can't resolve `Bootsnap.setup`/`Bootsnap.log_stats!`. Add a minimal shim under `sorbet/rbi/shims/bootsnap.rbi` declaring just the methods we call.
When `TAPIOCA_RBS_CACHE=1`, tapioca configures bootsnap with its own dedicated cache directory before loading the host app. If the host later runs `Bootsnap.setup` itself (Rails apps do this in `config/boot.rb`), the host's call clobbers tapioca's settings and silently breaks the RBS rewrite cache. Prepend `Tapioca::RBS::BootsnapGuard` so any subsequent `Bootsnap.setup` raises `HostBootsnapSetupError` pointing at the fix (gate the host's setup on `TAPIOCA_RBS_CACHE`). Add a CLI test asserting the raise.
Add a `Rewriting RBS comments to Sorbet signatures` section under Usage explaining what the rewrite does and why it exists, plus subsections covering `TAPIOCA_RBS_CACHE=1` for caching rewrites via bootsnap, the `--only-bootsnap-rbs-cache` priming workflow for CI, and how to coordinate a host app's own `Bootsnap.setup`.
4676c1e to
5e30f4c
Compare
amomchilov
approved these changes
May 9, 2026
Comment on lines
+48
to
+49
| readonly_env = ENV["BOOTSNAP_READONLY"] | ||
| readonly = !readonly_env.nil? && !["0", "false"].include?(readonly_env) |
Contributor
There was a problem hiding this comment.
I suggest copying Bootsnap's logic for this directly: https://github.com/rails/bootsnap/blob/ff7f97c77123113474fea6ae46459fea5c92ec9f/lib/bootsnap.rb#L190-L193
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.
Motivation
tapioca dslrewrites RBS comments to Sorbetsig {}blocks on every loaded file viarequire-hooks. On Shopify's monolith this dominates wall time (locally ~16 min). Caching the post-rewrite bytecode in bootsnap drops it to ~4 min (4.2x).Implementation
Opt-in via
TAPIOCA_RBS_CACHE=1. The rewriter sets up bootsnap with a dedicated cache directory (defaulttmp/cache/bootsnap-tapioca-rbs, override viaTAPIOCA_BOOTSNAP_CACHE_DIR) and forwardsBOOTSNAP_READONLYto bootsnap'sreadonly:arg. After setup,Tapioca::RBS::BootsnapGuardis prepended onBootsnap.singleton_classso the host's ownBootsnap.setupraises instead of silently clobbering the cache dir.--only-bootsnap-rbs-cachepopulates the cache and exits before any RBI work (CI prime/consumer pattern, mutually exclusive with--verify).bootsnapstays an optional runtime dep; a Sorbet shim covers the static surface.Tests
CLI tests in
spec/tapioca/cli/dsl_spec.rbcover the flag's early-exit, the--verifymutex, and the guard's raise.