Sitelet https://github.com/jruby/jruby/issues/7763
Skip to content

Java subclass (instantiation) performance degraded in 9.3 #7763

Description

@kares

A feature which originated in 9.3 (#6422) seems to have caused a 20x performance degradation in micro-benchmarks compared to 9.2.

This has been observed in production as threads pile up due synchronization and the extra overhead each <init> call has.

The following script demonstrates a Ruby sub-class of a Java class which will have poor class.new performance :

puts RUBY_DESCRIPTION + "\n\n"

require 'benchmark'

class RubyList < java.util.ArrayList

  def initialize(*args) # method's actual logic is irrelevant here
    # the idea is simply that more instructions will slow-down execution more
    foo = args.first.is_a?(NilClass) && args.shift
    bar = args.first.is_a?(String) ? args.shift : self.class.name

    if foo
      super_args = args.dup.to_java(:string)
    elsif bar
      super_args = [bar, *args]
    else
      super_args = args
    end

    super(super_args)
  end

end

time = Benchmark.realtime do
  threads = []
  1000.times do |i|
    threads << Thread.start do
      ('a'..'z').each { |c| 1000.times { RubyList.new(i.to_s, c) } }
    end
  end
  threads.each(&:join)
end

puts "1 RubyList.new(...) elapsed real-time: #{time}"

time = Benchmark.realtime do
  threads = []
  1000.times do |i|
    threads << Thread.start do
      ('a'..'z').each { |c| 1000.times { RubyList.new } }
    end
  end
  threads.each(&:join)
end

puts "2 RubyList.new elapsed real-time: #{time}"
$ jruby slow_java_super.rb 
jruby 9.2.20.0 (2.5.8) 2021-11-02 1a3255440b OpenJDK 64-Bit Server VM 11.0.14.1+1 on 11.0.14.1+1 +jit [linux-x86_64]

1 RubyList.new(...) elapsed real-time: 3.4137503600068158
2 RubyList.new elapsed real-time: 2.6153695250031888
$ jruby slow_java_super.rb 
jruby 9.3.10.0 (2.6.8) 2023-02-01 107b2e6697 OpenJDK 64-Bit Server VM 11.0.14.1+1 on 11.0.14.1+1 +jit [x86_64-linux]

1 RubyList.new(...) elapsed real-time: 54.96447179099778
2 RubyList.new elapsed real-time: 54.98580553600186

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions