Repository navigation
Delegate contains_requirable_file? to the stub in Bundler::StubSpecification - #9997
Merged
Merged
Conversation
…ication Bundler::StubSpecification delegated most BasicSpecification methods to its stub, but not contains_requirable_file?, so the call fell through method_missing to _remote_specification and loaded the gem's full gemspec from disk. Gem::Specification.find_active_stub_by_path calls it on every activated spec until one matches, so a lookup that matches nothing loaded the full gemspec of every gem in the bundle.
Contributor
Author
|
This is one of a set of boot-time improvements described in #9998. |
hsbt
approved these changes
Nov 4, 2026
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.
What was the end-user or developer problem that led to this PR?
Under Bundler,
Gem::Specification.find_active_stub_by_pathloads the full gemspec of every gem in the bundle when nothing matches.Bundler::StubSpecificationdelegates mostBasicSpecificationmethods to its underlying stub (activated?,full_require_paths,raw_require_paths,matches_for_glob, ...), but notcontains_requirable_file?. That call falls throughmethod_missingto_remote_specification, which reads the gem's full gemspec from disk.SpecificationRecord#find_active_stub_by_pathcalls it on every activated spec until one matches.RubyGems' own
Kernel#requiredoesn't reach this under Bundler (unresolved_depsis empty afterbundler/setup), so the cost lands on gems that callfind_active_stub_by_paththemselves. ffi-compiler'sLoader.finddid up to 1.4.2 (ffi/ffi-compiler#34), so every gem loading a native library through it (llhttp-ffi, scrypt, ...) paid it at require time. ffi-compiler 1.4.3 stopped calling it (ffi/ffi-compiler#35), and its maintainer suggested raising the cost here.Measured in a Rails application with 450 activated gems (445
Bundler::StubSpecification), Ruby 3.4.12, RubyGems 3.6.9, Bundler 4.0.16, macOS arm64, 3 runs:find_active_stub_by_path("libllhttp-ext.bundle")(no match), first call: 69–154 ms and 94,076 objects allocated._remote_specificationran for 445 of 445 Bundler stubs, every one from the block infind_active_stub_by_path.What is your fix for the problem, implemented in this PR?
Delegate
contains_requirable_file?to the stub, like the neighbouring methods.Gem::StubSpecification#contains_requirable_file?answers from the stub's own data (raw_require_paths,extension_dir) and file checks.With the same change applied as a monkey patch in the application above, 7 lookups (
libllhttp-ext.bundle,rack,rack/utils,nokogiri,nokogiri/nokogiri.bundle,does/not/exist,json/ext/parser.so) went from 54–96 ms and 93,921 objects to 13 ms and 13,340 objects, and returned the same spec for each path.One behaviour note: the stub's
ignored?check now runs on the stub rather than on the loaded full spec.Gem::StubSpecification#missing_extensions?returns false without loading the full spec on JRuby, for default gems, for gems without extensions, and whengem.build_completeexists; otherwise it loads the full spec, as every call did before.The new spec in
spec/bundler/stub_specification_spec.rbasserts thatcontains_requirable_file?answers without calling_remote_specification; it fails without the change (expected: 0 times, received: 1 time).This seems a fit for the "bundler: performance" label.
Make sure the following tasks are checked