ruby-changes:61541
From: David <ko1@a...>
Date: Fri, 5 Jun 2020 07:34:23 +0900 (JST)
Subject: [ruby-changes:61541] a18e81d797 (master): [rubygems/rubygems] Fix performance regression in `require`
https://git.ruby-lang.org/ruby.git/commit/?id=a18e81d797 From a18e81d797135de6e143a600e4f4d2b00ab23bf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= <deivid.rodriguez@r...> Date: Tue, 19 May 2020 14:00:00 +0200 Subject: [rubygems/rubygems] Fix performance regression in `require` Our check for `-I` paths should not go through all activated gems. https://github.com/rubygems/rubygems/commit/00d98eb8a3 diff --git a/lib/rubygems.rb b/lib/rubygems.rb index ef744c4..9bf1c63 100644 --- a/lib/rubygems.rb +++ b/lib/rubygems.rb @@ -594,9 +594,19 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} https://github.com/ruby/ruby/blob/trunk/lib/rubygems.rb#L594 end ## + # The number of paths in the `$LOAD_PATH` from activated gems. Used to + # prioritize `-I` and `ENV['RUBYLIB`]` entries during `require`. + + def self.activated_gem_paths + @activated_gem_paths ||= 0 + end + + ## # Add a list of paths to the $LOAD_PATH at the proper place. def self.add_to_load_path(*paths) + @activated_gem_paths = activated_gem_paths + paths.size + insert_index = load_path_insert_index if insert_index diff --git a/lib/rubygems/core_ext/kernel_require.rb b/lib/rubygems/core_ext/kernel_require.rb index ed24111..7625ce1 100644 --- a/lib/rubygems/core_ext/kernel_require.rb +++ b/lib/rubygems/core_ext/kernel_require.rb @@ -47,7 +47,7 @@ module Kernel https://github.com/ruby/ruby/blob/trunk/lib/rubygems/core_ext/kernel_require.rb#L47 load_path_insert_index = Gem.load_path_insert_index break unless load_path_insert_index - $LOAD_PATH[0...load_path_insert_index].each do |lp| + $LOAD_PATH[0...load_path_insert_index - Gem.activated_gem_paths].each do |lp| safe_lp = lp.dup.tap(&Gem::UNTAINT) begin if File.symlink? safe_lp # for backward compatibility diff --git a/lib/rubygems/test_case.rb b/lib/rubygems/test_case.rb index a63c99f..9b83f03 100644 --- a/lib/rubygems/test_case.rb +++ b/lib/rubygems/test_case.rb @@ -388,6 +388,7 @@ class Gem::TestCase < Minitest::Test https://github.com/ruby/ruby/blob/trunk/lib/rubygems/test_case.rb#L388 Gem::Security.reset Gem.loaded_specs.clear + Gem.instance_variable_set(:@activated_gem_paths, 0) Gem.clear_default_specs Bundler.reset! diff --git a/test/rubygems/test_require.rb b/test/rubygems/test_require.rb index 1c86a4e..c6be26a 100644 --- a/test/rubygems/test_require.rb +++ b/test/rubygems/test_require.rb @@ -382,6 +382,17 @@ class TestGemRequire < Gem::TestCase https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_require.rb#L382 assert_equal 0, times_called end + def test_second_gem_require_does_not_resolve_path_manually_before_going_through_standard_require + a1 = util_spec "a", "1", nil, "lib/test_gem_require_a.rb" + install_gem a1 + + assert_require "test_gem_require_a" + + stub(:gem_original_require, ->(path) { assert_equal "test_gem_require_a", path }) do + require "test_gem_require_a" + end + end + def test_realworld_default_gem testing_ruby_repo = !ENV["GEM_COMMAND"].nil? skip "this test can't work under ruby-core setup" if testing_ruby_repo || java_platform? -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/