I recently converted a large codebase from Ruby 2.1.4 to Ruby 3.4.7 and upon testing these changes under windows 10 x86 architecture I am noticing that the require method on most ruby gems is significantly slower than with Ruby 2.1.4.

Here is the script I'm using to time each require call:

require 'rubygems' def time_require(path) t0 = Process.clock_gettime(Process::CLOCK_MONOTONIC) begin require path dt = Process.clock_gettime(Process::CLOCK_MONOTONIC) - t0 puts "[ok] require '#{path}' took #{(dt * 1000).round(3)} ms" rescue LoadError => e dt = Process.clock_gettime(Process::CLOCK_MONOTONIC) - t0 warn "[fail] require '#{path}' after #{(dt * 1000).round(3)} ms -> #{e.message}" end end REQUIRES = %w[ bigdecimal crack diff/lcs hashdiff hoe io/console json launchy magic_encoding multi power_assert psych rack rack/protection rake rdoc rspec rspec/core rspec/expectations rspec/mocks rspec/support ruby2ruby ruby_parser inline zip safe_yaml sexp sexp_processor sinatra test/unit tilt uuidtools webmock ] overall_t0 = Process.clock_gettime(Process::CLOCK_MONOTONIC) REQUIRES.each { |path| time_require(path) } overall_dt = Process.clock_gettime(Process::CLOCK_MONOTONIC) - overall_t0 puts "[overall] requiring #{REQUIRES.size} gems took #{(overall_dt * 1000).round(3)} ms total"

Under Ruby 2.1.4:

C:\testruby3>"C:\Program Files\Ruby214\bin\ruby.exe" test.rb [ok] require 'bigdecimal' took 69.686 ms [ok] require 'crack' took 298.303 ms [ok] require 'diff/lcs' took 14.817 ms [ok] require 'hashdiff' took 18.027 ms [ok] require 'hoe' took 146.456 ms [ok] require 'io/console' took 1.484 ms [ok] require 'json' took 24.252 ms [ok] require 'launchy' took 119.803 ms [ok] require 'magic_encoding' took 3.182 ms [ok] require 'multi' took 3.587 ms [ok] require 'power_assert' took 32.756 ms [ok] require 'psych' took 0.953 ms [ok] require 'rack' took 52.601 ms [ok] require 'rack/protection' took 8.56 ms [ok] require 'rake' took 0.017 ms [ok] require 'rdoc' took 3.2 ms [ok] require 'rspec' took 127.332 ms [ok] require 'rspec/core' took 0.016 ms [ok] require 'rspec/expectations' took 70.952 ms [ok] require 'rspec/mocks' took 70.315 ms [ok] require 'rspec/support' took 0.021 ms [ok] require 'ruby2ruby' took 16.208 ms [ok] require 'ruby_parser' took 68.057 ms [ok] require 'inline' took 14.346 ms [ok] require 'zip' took 91.287 ms [ok] require 'safe_yaml' took 2.23 ms [ok] require 'sexp' took 0.013 ms [ok] require 'sexp_processor' took 0.008 ms [ok] require 'sinatra' took 1981.75 ms [ok] require 'test/unit' took 86.812 ms [ok] require 'tilt' took 0.016 ms [ok] require 'uuidtools' took 12.065 ms [ok] require 'webmock' took 197.38 ms [overall] requiring 33 gems took 3555.404 ms total

Under Ruby 3.4.7:

C:\testruby3>"C:\Ruby34\bin\ruby.exe" test.rb [ok] require 'bigdecimal' took 1103.666 ms [ok] require 'crack' took 2138.823 ms [ok] require 'diff/lcs' took 281.71 ms [ok] require 'hashdiff' took 349.441 ms [ok] require 'hoe' took 1339.24 ms [ok] require 'io/console' took 47.06 ms [ok] require 'json' took 165.733 ms [ok] require 'launchy' took 1614.602 ms [ok] require 'magic_encoding' took 231.905 ms [ok] require 'multi' took 241.455 ms [ok] require 'power_assert' took 586.14 ms [ok] require 'psych' took 0.014 ms [ok] require 'rack' took 320.881 ms [ok] require 'rack/protection' took 328.701 ms [ok] require 'rake' took 3.877 ms [ok] require 'rdoc' took 47.048 ms [ok] require 'rspec' took 1884.187 ms [ok] require 'rspec/core' took 0.017 ms [ok] require 'rspec/expectations' took 544.222 ms [ok] require 'rspec/mocks' took 623.471 ms [ok] require 'rspec/support' took 0.018 ms [ok] require 'ruby2ruby' took 560.83 ms [ok] require 'ruby_parser' took 1350.577 ms [ok] require 'inline' took 585.545 ms [ok] require 'zip' took 2425.174 ms [ok] require 'safe_yaml' took 1346.15 ms [ok] require 'sexp' took 0.009 ms [ok] require 'sexp_processor' took 0.005 ms [ok] require 'sinatra' took 2501.594 ms [ok] require 'test/unit' took 1614.02 ms [ok] require 'tilt' took 0.019 ms [ok] require 'uuidtools' took 1119.12 ms [ok] require 'webmock' took 2648.623 ms [overall] requiring 33 gems took 26021.443 ms total

I am using the bundle install command to get these gems from rubygems.org after specifying a gemfile.

Has anyone else experienced this slowness under Ruby 3?

rjlam2004's user avatar

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.