Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Delegate contains_requirable_file? to the stub in Bundler::StubSpecification - #9997

Merged
hsbt merged 1 commit into
ruby:masterfrom
linyiru:bundler-stub-contains-requirable-file
Nov 4, 2026
Merged

hsbt merged 1 commit into
ruby:masterfrom
linyiru:bundler-stub-contains-requirable-file

Conversation

@linyiru

@linyiru linyiru commented Oct 31, 2026

Copy link
Copy Markdown
Contributor

What was the end-user or developer problem that led to this PR?

Under Bundler, Gem::Specification.find_active_stub_by_path loads the full gemspec of every gem in the bundle when nothing matches.

Bundler::StubSpecification delegates most BasicSpecification methods to its underlying stub (activated?, full_require_paths, raw_require_paths, matches_for_glob, ...), but not contains_requirable_file?. That call falls through method_missing to _remote_specification, which reads the gem's full gemspec from disk. SpecificationRecord#find_active_stub_by_path calls it on every activated spec until one matches.

RubyGems' own Kernel#require doesn't reach this under Bundler (unresolved_deps is empty after bundler/setup), so the cost lands on gems that call find_active_stub_by_path themselves. ffi-compiler's Loader.find did 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_specification ran for 445 of 445 Bundler stubs, every one from the block in find_active_stub_by_path.
  • A second application (296 gems, Ruby 4.0.9) saw RSS grow by 17.8 MB on the same lookup through ffi-compiler 1.4.2, with about 3.5 MB of Ruby objects kept alive (comment).

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 when gem.build_complete exists; otherwise it loads the full spec, as every call did before.

The new spec in spec/bundler/stub_specification_spec.rb asserts that contains_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

…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.
@linyiru

linyiru commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

This is one of a set of boot-time improvements described in #9998.

@hsbt
hsbt merged commit f20c282 into ruby:master Nov 4, 2026
114 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants