rubinius/rubinius — Issue #1452: Low-level #== is 3x slower on RBX than JRuby
Author: chuckremes
Created: 2011-12-15 04:20:09+00:00
State: open

While optimizing the perf of some code, I had an inkling that there might be a problem with comparing Fixnums via #==. I wrote a short benchmark to see if there was an issue.

Looks like RBX has a problem with #super, for one. Secondly, the Fixnum#== performance is nearly 3x slower on than on JRuby.

```ruby

# Test the performance of testing for equality in a class that includes
# a module and calls super on it. See if there is any difference between
# arg == Const and Const == arg for the runtimes.
#

require 'rubygems'
require 'benchmark'

A = 1
B = 2
C = 3
D = 4
E = 5
F = 6
G = 7
H = 8
I = 9

module Foo1
def compare_it(val)
A == val ||
B == val
end
end # Foo1

module Foo2
def compare_it(val)
val == A ||
val == B
end
end # Foo2

class Bar1
include Foo1

def compare_it(val)
super ||
C == val ||
D == val ||
E == val ||
F == val ||
G == val ||
H == val ||
I == val
end
end # Bar1

class Bar2
include Foo2

def compare_it(val)
super ||
val == C ||
val == D ||
val == E ||
val == F ||
val == G ||
val == H ||
val == I
end
end # Bar2

@bar1 = Bar1.new
@bar2 = Bar2.new

Iterations = 50_000_000

Benchmark.bmbm("== order comparison for speed".size) do |x|
x.report("bar1 A in super:") do
Iterations.times { @bar1.compare_it(1) }
end

x.report("bar2 A in super:") do
Iterations.times { @bar2.compare_it(1) }
end

x.report("bar1 C:") do
Iterations.times { @bar1.compare_it(3) }
end

x.report("bar2 C:") do
Iterations.times { @bar2.compare_it(3) }
end

x.report("bar1 I:") do
Iterations.times { @bar1.compare_it(9) }
end

x.report("bar2 I:") do
Iterations.times { @bar2.compare_it(9) }
end

end
```

Results are:

```ruby
Chuck-Remess-Mac-Pro:perf cremes$ rbx equals_bench.rb
Rehearsal ----------------------------------------------------------------
bar1 A in super: 20.960131 0.018440 20.978571 ( 21.133859)
bar2 A in super: 20.961552 0.015698 20.977250 ( 21.065954)
bar1 C: 20.079118 0.013177 20.092295 ( 20.149448)
bar2 C: 20.834044 0.017719 20.851763 ( 20.891548)
bar1 I: 25.436169 0.013837 25.450006 ( 25.513277)
bar2 I: 25.074366 0.012612 25.086978 ( 25.121529)
----------------------------------------------------- total: 133.436863sec

user system total real
bar1 A in super: 18.532734 0.016796 18.549530 ( 18.572636)
bar2 A in super: 19.075991 0.012712 19.088703 ( 19.120353)
bar1 C: 20.570250 0.008504 20.578754 ( 20.598505)
bar2 C: 21.011757 0.007455 21.019212 ( 21.043022)
bar1 I: 25.435906 0.015563 25.451469 ( 25.484010)
bar2 I: 25.324436 0.011867 25.336303 ( 25.381997)
Chuck-Remess-Mac-Pro:perf cremes$ rvm jruby
Chuck-Remess-Mac-Pro:perf cremes$ ruby --server equals_bench.rb
Rehearsal ----------------------------------------------------------------
bar1 A in super: 7.706000 0.000000 7.706000 ( 7.705000)
bar2 A in super: 7.392000 0.000000 7.392000 ( 7.393000)
bar1 C: 9.950000 0.000000 9.950000 ( 9.950000)
bar2 C: 10.285000 0.000000 10.285000 ( 10.285000)
bar1 I: 17.860000 0.000000 17.860000 ( 17.860000)
bar2 I: 17.611000 0.000000 17.611000 ( 17.611000)
------------------------------------------------------ total: 70.804001sec

user system total real
bar1 A in super: 7.981000 0.000000 7.981000 ( 7.981000)
bar2 A in super: 7.816000 0.000000 7.816000 ( 7.816000)
bar1 C: 10.179000 0.000000 10.179000 ( 10.179000)
bar2 C: 10.234000 0.000000 10.234000 ( 10.235000)
bar1 I: 17.381000 0.000000 17.381000 ( 17.381000)
bar2 I: 17.510000 0.000000 17.510000 ( 17.509000)
```