Bug #22220
closedPerformance regression when requiring `aws-sdk-ec2` in Ruby 4.0.6
Description
Ruby 4.0.6 introduced a large performance regression when loading aws-sdk-ec2 (~30s on my machine, compared to <1s for ruby 4.0.5). Script to reproduce:
for v in 4.0.5 4.0.6; do
docker run --rm ruby:$v-slim bash -c '
gem install --no-document aws-sdk-ec2 -v 1.402.0 >/dev/null 2>&1
ruby -e "t = Process.clock_gettime(Process::CLOCK_MONOTONIC)
require \"aws-sdk-ec2\"
printf(\"ruby %s: %.2fs\n\", RUBY_VERSION, Process.clock_gettime(Process::CLOCK_MONOTONIC) - t)"'
done
It appears to be related to Shape.new and/or Class.new compile times; minimal reproduction:
for v in 4.0.5 4.0.6; do
docker run --rm ruby:$v-slim ruby -e '
src = "class Shape\n def self.new(**) = nil\nend\nmodule M\n" +
(1..16_000).map { |i| %( K_#{i} = Shape.new(name: "k#{i}")) }.join("\n") + "\nend\n"
File.write("/tmp/bench.rb", src)
t = Process.clock_gettime(Process::CLOCK_MONOTONIC)
RubyVM::InstructionSequence.compile_file("/tmp/bench.rb")
printf("ruby %s: %.2fs\n", RUBY_VERSION, Process.clock_gettime(Process::CLOCK_MONOTONIC) - t)'
done
Updated by luke-gru (Luke Gruber) 25 days ago
Seems caused by 1f473ed633a364f8c89b69a76adfb22a4bf21f96
Updated by luke-gru (Luke Gruber) 21 days ago
Updated by viralpraxis (Iaroslav Kurbatov) 21 days ago
Possibly fixed by https://github.com/ruby/ruby/pull/18181
Updated by nobu (Nobuyoshi Nakada) 14 days ago
- Backport changed from 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN to 3.3: DONTNEED, 3.4: DONTNEED, 4.0: REQUIRED
Updated by viralpraxis (Iaroslav Kurbatov) 14 days ago
- Status changed from Open to Closed
Applied in changeset git|5c9bb97ca8bec233745af117a396430db47725f6.
Optimize DCE
[Bug #22220]
ref: https://bugs.ruby-lang.org/issues/22220
1f473ed633 split the walk into a counting pass and a deletion pass -- but
the counting pass only stops at leave, so it scans to the end of the
ISEQ on every call.
opt_new calls in here once per Klass.new, making
aws-sdk-ec2's ~16k code-generated Class.new take ~30s to compile.
The solution is to merge the passes back into one and defer decision instead.