Skip to content
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

Don't call Kernel#require in hot loop #678

Merged
merged 1 commit into from
Jul 19, 2024

Conversation

casperisfine
Copy link

Ref: https://bugs.ruby-lang.org/issues/20641

Even without the reference bug, require 'date' isn't cheap.

require "benchmark/ips"
require "yaml"
require "date"

100.times do |i|
  $LOAD_PATH.unshift("/tmp/does/not/exist/#{i}")
end
payload = 100.times.map { Date.today }.to_yaml

Benchmark.ips do |x|
  x.report("100 dates") { YAML.unsafe_load(payload) }
end

Before:

$ ruby /tmp/bench-yaml.rb
ruby 3.2.2 (2023-03-30 revision e51014f9c0) [arm64-darwin22]
Warming up --------------------------------------
           100 dates   416.000 i/100ms
Calculating -------------------------------------
           100 dates      4.309k (± 1.2%) i/s -     21.632k in   5.021003s

After:

$ ruby -Ilib /tmp/bench-yaml.rb
ruby 3.2.2 (2023-03-30 revision e51014f9c0) [arm64-darwin22]
Warming up --------------------------------------
           100 dates   601.000 i/100ms
Calculating -------------------------------------
           100 dates      5.993k (± 1.8%) i/s -     30.050k in   5.016079s

@tenderlove

Ref: https://bugs.ruby-lang.org/issues/20641

Even without the reference bug, `require 'date'` isn't cheap.

```ruby

require "benchmark/ips"
require "yaml"
require "date"

100.times do |i|
  $LOAD_PATH.unshift("/tmp/does/not/exist/#{i}")
end
payload = 100.times.map { Date.today }.to_yaml

Benchmark.ips do |x|
  x.report("100 dates") { YAML.unsafe_load(payload) }
end
```

Before:
```
$ ruby /tmp/bench-yaml.rb
ruby 3.2.2 (2023-03-30 revision e51014f9c0) [arm64-darwin22]
Warming up --------------------------------------
           100 dates   416.000 i/100ms
Calculating -------------------------------------
           100 dates      4.309k (± 1.2%) i/s -     21.632k in   5.021003s
```

After:
```
$ ruby -Ilib /tmp/bench-yaml.rb
ruby 3.2.2 (2023-03-30 revision e51014f9c0) [arm64-darwin22]
Warming up --------------------------------------
           100 dates   601.000 i/100ms
Calculating -------------------------------------
           100 dates      5.993k (± 1.8%) i/s -     30.050k in   5.016079s
```
@@ -4,6 +4,8 @@ module Psych
###
# Scan scalars for built in types
class ScalarScanner
autoload :Date, "date"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose this usage of autoload is debatable, I've seen it elsewhere, but I can change it for a require unless defined?(Date) if you prefer.

@tenderlove tenderlove merged commit 6bb8d08 into ruby:master Jul 19, 2024
66 of 67 checks passed
@casperisfine casperisfine deleted the require-unless-defined branch July 19, 2024 20:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants