ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:118637] [Ruby master Bug#20641] `lib/bundled_gems.rb` makes `Kernel.require` over 100x slower
@ 2024-07-19 12:03 byroot (Jean Boussier) via ruby-core
  2024-07-19 15:08 ` [ruby-core:118638] " byroot (Jean Boussier) via ruby-core
  2024-09-02  9:39 ` [ruby-core:119002] " k0kubun (Takashi Kokubun) via ruby-core
  0 siblings, 2 replies; 3+ messages in thread
From: byroot (Jean Boussier) via ruby-core @ 2024-07-19 12:03 UTC (permalink / raw)
  To: ruby-core; +Cc: byroot (Jean Boussier)

Issue #20641 has been reported by byroot (Jean Boussier).

----------------------------------------
Bug #20641: `lib/bundled_gems.rb` makes `Kernel.require` over 100x slower
https://bugs.ruby-lang.org/issues/20641

* Author: byroot (Jean Boussier)
* Status: Open
* Backport: 3.1: DONTNEED, 3.2: DONTNEED, 3.3: REQUIRED
----------------------------------------
I just discovered this while profiling Active Record's test suite, and I noticed 40% of the runtime was in `$LOAD_PATH.resolve_feature_path`, so much I thought it was a profiler bug.

But it turns out it's real. Various APIs do call `require` late, for instance Psych calls `require 'date'` every single time it parses a date: https://github.com/ruby/psych/blob/be0ba74e5613c20f213403e15914d24944c2652d/lib/psych/scalar_scanner.rb#L64

I've put together a quick benchmark:

```ruby
# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  gem "benchmark-ips"
end

Benchmark.ips do |x|
  x.report(RUBY_VERSION) { require "erb" }
  x.save! "/tmp/require.bench"
  x.compare!
end
```

And the difference is massive:

```
Calculating -------------------------------------
               3.2.2      6.450M (± 1.2%) i/s -     32.822M in   5.089538s

Comparison:
               3.2.2:  6449899.4 i/s
               3.3.3:    46996.8 i/s - 137.24x  slower
```

And that's with a small `$LOAD_PATH`, the bigger the application, the worse it is, if I add:

```ruby
100.times do |i|
  $LOAD_PATH.unshift("/tmp/empty-#{i}")
end
```

```
Calculating -------------------------------------
               3.3.3      6.198k (± 6.1%) i/s -     30.968k in   5.018955s

Comparison:
               3.2.2:  3380939.6 i/s
               3.3.3:     6198.4 i/s - 545.46x  slower
```

I'm looking at a way to speed this up, but this is big enough that I believe we should backport the fix.

cc @hsbt 



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [ruby-core:118638] [Ruby master Bug#20641] `lib/bundled_gems.rb` makes `Kernel.require` over 100x slower
  2024-07-19 12:03 [ruby-core:118637] [Ruby master Bug#20641] `lib/bundled_gems.rb` makes `Kernel.require` over 100x slower byroot (Jean Boussier) via ruby-core
@ 2024-07-19 15:08 ` byroot (Jean Boussier) via ruby-core
  2024-09-02  9:39 ` [ruby-core:119002] " k0kubun (Takashi Kokubun) via ruby-core
  1 sibling, 0 replies; 3+ messages in thread
From: byroot (Jean Boussier) via ruby-core @ 2024-07-19 15:08 UTC (permalink / raw)
  To: ruby-core; +Cc: byroot (Jean Boussier)

Issue #20641 has been updated by byroot (Jean Boussier).


Some extra context. This codepath is only active if using Bundler >= 2.5.0.


----------------------------------------
Bug #20641: `lib/bundled_gems.rb` makes `Kernel.require` over 100x slower
https://bugs.ruby-lang.org/issues/20641#change-109164

* Author: byroot (Jean Boussier)
* Status: Open
* Backport: 3.1: DONTNEED, 3.2: DONTNEED, 3.3: REQUIRED
----------------------------------------
I just discovered this while profiling Active Record's test suite, and I noticed 40% of the runtime was in `$LOAD_PATH.resolve_feature_path`, so much I thought it was a profiler bug.

But it turns out it's real. Various APIs do call `require` late, for instance Psych calls `require 'date'` every single time it parses a date: https://github.com/ruby/psych/blob/be0ba74e5613c20f213403e15914d24944c2652d/lib/psych/scalar_scanner.rb#L64

I've put together a quick benchmark:

```ruby
# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  gem "benchmark-ips"
end

Benchmark.ips do |x|
  x.report(RUBY_VERSION) { require "erb" }
  x.save! "/tmp/require.bench"
  x.compare!
end
```

And the difference is massive:

```
Calculating -------------------------------------
               3.2.2      6.450M (± 1.2%) i/s -     32.822M in   5.089538s

Comparison:
               3.2.2:  6449899.4 i/s
               3.3.3:    46996.8 i/s - 137.24x  slower
```

And that's with a small `$LOAD_PATH`, the bigger the application, the worse it is, if I add:

```ruby
100.times do |i|
  $LOAD_PATH.unshift("/tmp/empty-#{i}")
end
```

```
Calculating -------------------------------------
               3.3.3      6.198k (± 6.1%) i/s -     30.968k in   5.018955s

Comparison:
               3.2.2:  3380939.6 i/s
               3.3.3:     6198.4 i/s - 545.46x  slower
```

I'm looking at a way to speed this up, but this is big enough that I believe we should backport the fix.

cc @hsbt 



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [ruby-core:119002] [Ruby master Bug#20641] `lib/bundled_gems.rb` makes `Kernel.require` over 100x slower
  2024-07-19 12:03 [ruby-core:118637] [Ruby master Bug#20641] `lib/bundled_gems.rb` makes `Kernel.require` over 100x slower byroot (Jean Boussier) via ruby-core
  2024-07-19 15:08 ` [ruby-core:118638] " byroot (Jean Boussier) via ruby-core
@ 2024-09-02  9:39 ` k0kubun (Takashi Kokubun) via ruby-core
  1 sibling, 0 replies; 3+ messages in thread
From: k0kubun (Takashi Kokubun) via ruby-core @ 2024-09-02  9:39 UTC (permalink / raw)
  To: ruby-core; +Cc: k0kubun (Takashi Kokubun)

Issue #20641 has been updated by k0kubun (Takashi Kokubun).

Backport changed from 3.1: DONTNEED, 3.2: DONTNEED, 3.3: REQUIRED to 3.1: DONTNEED, 3.2: DONTNEED, 3.3: DONE

ruby_3_3 commit:4667f8ec10269b0b5deca459f098abbdf3bae4ec.

----------------------------------------
Bug #20641: `lib/bundled_gems.rb` makes `Kernel.require` over 100x slower
https://bugs.ruby-lang.org/issues/20641#change-109577

* Author: byroot (Jean Boussier)
* Status: Closed
* Backport: 3.1: DONTNEED, 3.2: DONTNEED, 3.3: DONE
----------------------------------------
I just discovered this while profiling Active Record's test suite, and I noticed 40% of the runtime was in `$LOAD_PATH.resolve_feature_path`, so much I thought it was a profiler bug.

But it turns out it's real. Various APIs do call `require` late, for instance Psych calls `require 'date'` every single time it parses a date: https://github.com/ruby/psych/blob/be0ba74e5613c20f213403e15914d24944c2652d/lib/psych/scalar_scanner.rb#L64

I've put together a quick benchmark:

```ruby
# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  gem "benchmark-ips"
end

Benchmark.ips do |x|
  x.report(RUBY_VERSION) { require "erb" }
  x.save! "/tmp/require.bench"
  x.compare!
end
```

And the difference is massive:

```
Calculating -------------------------------------
               3.2.2      6.450M (± 1.2%) i/s -     32.822M in   5.089538s

Comparison:
               3.2.2:  6449899.4 i/s
               3.3.3:    46996.8 i/s - 137.24x  slower
```

And that's with a small `$LOAD_PATH`, the bigger the application, the worse it is, if I add:

```ruby
100.times do |i|
  $LOAD_PATH.unshift("/tmp/empty-#{i}")
end
```

```
Calculating -------------------------------------
               3.3.3      6.198k (± 6.1%) i/s -     30.968k in   5.018955s

Comparison:
               3.2.2:  3380939.6 i/s
               3.3.3:     6198.4 i/s - 545.46x  slower
```

I'm looking at a way to speed this up, but this is big enough that I believe we should backport the fix.

cc @hsbt 



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-09-02  9:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-07-19 12:03 [ruby-core:118637] [Ruby master Bug#20641] `lib/bundled_gems.rb` makes `Kernel.require` over 100x slower byroot (Jean Boussier) via ruby-core
2024-07-19 15:08 ` [ruby-core:118638] " byroot (Jean Boussier) via ruby-core
2024-09-02  9:39 ` [ruby-core:119002] " k0kubun (Takashi Kokubun) via ruby-core

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).