Skip to content

Commit 6f2a968

Browse files
committed
[GR-18163] Avoid loading RubyGems needlessly for require 'psych'
PullRequest: truffleruby/2077
2 parents 6446003 + a04bfd0 commit 6f2a968

File tree

5 files changed

+37
-7
lines changed

5 files changed

+37
-7
lines changed

doc/contributor/profiling.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ The `--metrics-profile-require` option can be used to profile the time used for
8282

8383
For example, the `summary` view provides an overview of where time is spent:
8484
```
85-
$ jt ruby --experimental-options --cpusampler --cpusampler.Mode=roots --metrics-profile-require=summary -e 'require "rubygems"' |& grep "metrics "
85+
$ jt ruby --experimental-options --cpusampler --metrics-profile-require=summary -e 'require "rubygems"' |& grep "metrics "
8686
metrics execute | 1122ms 99.6% | 0.0% || 212ms 18.8% | 0.0% | (metrics)~1:0
8787
metrics parsing | 71ms 6.3% | 0.0% || 71ms 6.3% | 0.0% | (metrics)~1:0
8888
metrics translating | 60ms 5.3% | 0.0% || 60ms 5.3% | 0.0% | (metrics)~1:0
@@ -92,4 +92,4 @@ $ jt ruby --experimental-options --cpusampler --cpusampler.Mode=roots --metrics-
9292

9393
This feature can also be used to generate a flame graph with detailed require timings:
9494

95-
`$ jt profile --experimental-options --cpusampler --cpusampler.Mode=roots --metrics-profile-require=detail -e 'require "rubygems"'`
95+
`$ jt profile --metrics-profile-require=detail -e 'require "rubygems"'`

lib/mri/psych.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
org.jruby.ext.psych.PsychLibrary.new.load(JRuby.runtime, false)
1111
end
1212
else
13-
begin
14-
require "#{RUBY_VERSION[/\d+\.\d+/]}/psych.so"
15-
rescue LoadError
13+
# TruffleRuby: avoid requiring a non-existing file, which would trigger loading RubyGems
14+
# begin
15+
# require "#{RUBY_VERSION[/\d+\.\d+/]}/psych.so"
16+
# rescue LoadError
1617
require 'psych.so'
17-
end
18+
# end
1819
end
1920
require 'psych/nodes'
2021
require 'psych/streaming'

spec/tags/truffle/lazy_rubygems_tags.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ slow:RubyGems is loaded by a failing require
44
slow:Lazy RubyGems defines StringIO like RubyGems which requires it eagerly
55
slow:Lazy RubyGems works for require 'rubygems/package'
66
slow:Lazy RubyGems works for require 'rubygems/specification'
7+
slow:RubyGems is not loaded for default gems if there is no upgraded default gem

spec/truffle/lazy_rubygems_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,33 @@
1515
ruby_exe('p autoload? :Gem').should == "\"rubygems\"\n"
1616
end
1717

18+
# This spec needs no upgraded gems installed
19+
it "is not loaded for default gems if there is no upgraded default gem" do
20+
default_gems = Truffle::GemUtil::DEFAULT_GEMS.keys
21+
default_gems -= [
22+
'bundler', # explicitly requires RubyGems
23+
'dbm', 'gdbm', 'sdbm', # not available
24+
'rss', # rss/xmlparser.rb requires non-existing "xml/parser"
25+
]
26+
default_gems.delete('io')
27+
default_gems << 'io/console'
28+
default_gems.delete('rexml')
29+
default_gems << 'rexml/document'
30+
31+
code = <<-RUBY
32+
#{default_gems.inspect}.each do |name|
33+
require name
34+
unless autoload?(:Gem) == "rubygems"
35+
puts $LOADED_FEATURES
36+
abort "\#{name} loaded RubyGems"
37+
end
38+
end
39+
puts 'OK'
40+
RUBY
41+
ruby_exe(code, args: "2>&1").should == "OK\n"
42+
$?.success?.should == true
43+
end
44+
1845
it "is loaded when accessing Gem" do
1946
ruby_exe('Gem; puts $"').should include('/rubygems.rb')
2047
ruby_exe('Gem; p autoload? :Gem').should == "nil\n"

tool/jt.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1886,7 +1886,8 @@ def profile(*args)
18861886
}
18871887
end
18881888
raw_sh "#{repo}/stackcollapse-graalvm.rb", profile_data_file, out: flamegraph_data_file
1889-
raw_sh "#{repo}/flamegraph.pl", flamegraph_data_file, out: svg_filename
1889+
unit = args.any?(/^--cpusampler\.Period=/) ? [] : ['--countname', 'ms']
1890+
raw_sh "#{repo}/flamegraph.pl", *unit, flamegraph_data_file, out: svg_filename
18901891

18911892
app_open svg_filename
18921893
end

0 commit comments

Comments
 (0)