Skip to content

[3.3] bundled_gems.rb: Add a fast path #11221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 22, 2024

Conversation

casperisfine
Copy link
Contributor

[Bug #20641] Gem::BUNDLED_GEMS.warning? adds a lot of extra work on top of require. When the call end up atually loading code the overhead is somewhat marginal.

However it's not uncommon for code to go some late require in some paths, so it's expected that calling require with something already required is somewhat fast, and bundled_gems.rb breaks this assumption.

To avoid this, we can have a fast path that in most case allow to short-circuit all the heavy computations. If we extract the feature basename and it doesn't match any of the bundled gems we care about we can return very early.

With this change require 'date' is now only 1.33x slower on Ruby 3.3.3, than it was on Ruby 3.2.2, whereas before this change it was at least 100x slower.

cc @k0kubun

[Bug #20641] `Gem::BUNDLED_GEMS.warning?` adds a lot of extra
work on top of `require`. When the call end up atually loading code
the overhead is somewhat marginal.

However it's not uncommon for code to go some late `require` in some
paths, so it's expected that calling `require` with something already
required is somewhat fast, and `bundled_gems.rb` breaks this assumption.

To avoid this, we can have a fast path that in most case allow to
short-circuit all the heavy computations. If we extract the feature
basename and it doesn't match any of the bundled gems we care about
we can return very early.

With this change `require 'date'` is now only 1.33x slower on Ruby
3.3.3, than it was on Ruby 3.2.2, whereas before this change it
was at least 100x slower.
@casperisfine casperisfine requested a review from k0kubun as a code owner July 22, 2024 15:28
@k0kubun k0kubun merged commit 4667f8e into ruby:ruby_3_3 Jul 22, 2024
97 checks passed
@k0kubun k0kubun deleted the ruby_3_3-speedup-bundler-gems.rb branch July 22, 2024 23:35
@hsbt hsbt added the Backport label Sep 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants