Ruby 3.0.0-preview1 released!

Ruby 3.0.0-preview1 just got released!
https://www.ruby-lang.org/en/news/2020/09/25/ruby-3-0-0-preview1-released/

I have done some benchmarks with 2.7.1 and 3.0.0-preview1.

Benchmark


Ruby Details:

CFLAGS

  • Ruby 2.7.1
-O3 -pipe -fno-plt -march=native -mtune=native -ggdb3 -Wall -Wextra -Wdeprecated-declarations -Wduplicated-cond -Wimplicit-function-declaration -Wimplicit-int -Wmisleading-indentation -Wpointer-arith -Wwrite-strings -Wimplicit-fallthrough=0 -Wmissing-noreturn -Wno-cast-function-type -Wno-constant-logical-operand -Wno-long-long -Wno-missing-field-initializers -Wno-overlength-strings -Wno-packed-bitfield-compat -Wno-parentheses-equality -Wno-self-assign -Wno-tautological-compare -Wno-unused-parameter -Wno-unused-value -Wsuggest-attribute=format -Wsuggest-attribute=noreturn -Wunused-variable
  • Ruby 3.0.0-preview1
-O3 -pipe -fno-plt -march=native -mtune=native -ggdb3 -Wall -Wextra -Werror=deprecated-declarations -Werror=duplicated-cond -Werror=implicit-function-declaration -Werror=implicit-int -Werror=misleading-indentation -Werror=pointer-arith -Werror=write-strings -Wimplicit-fallthrough=0 -Wmissing-noreturn -Wno-cast-function-type -Wno-constant-logical-operand -Wno-long-long -Wno-missing-field-initializers -Wno-overlength-strings -Wno-packed-bitfield-compat -Wno-parentheses-equality -Wno-self-assign -Wno-tautological-compare -Wno-unused-parameter -Wno-unused-value -Wsuggest-attribute=format -Wsuggest-attribute=noreturn -Wunused-variable

Benchmark Code:


System Details

Kernel
Linux version 5.8.8-xanmod1-1 (makepkg@archlinux) (gcc (GCC) 10.2.0, GNU ld (GNU Binutils) 2.35) #1 SMP PREEMPT Fri, 11 Sep 2020 06:25:53 +0000
CPU
Intel(R) Core(TM) i3-6006U CPU @ 2.00GHz
Memory (RAM)
     *-bank:1
          description: SODIMM DDR4 Synchronous 2400 MHz (0.4 ns)
          product: HMA851S6AFR6N-UH
          vendor: SK Hynix
          physical id: 1
          serial: 2955429E
          slot: DIMM B
          size: 4GiB
          width: 64 bits
          clock: 2400MHz (0.4ns)

System was idle at 0% CPU usage during benchmarks.

2 Likes

But the Ractor.new makes Ruby 3.0 way faster. On my dual core CPU (4 core with hyperthread), Ractor makes my code 2x faster:
This is a bad code without ractor, but just compare the time:

i = 0
10000000.times { |x| i += x }
a = i

i = 0
10000000.times { |x| i += x }
a += i

i = 0
10000000.times { |x| i += x }
a += i

i = 0
10000000.times { |x| i += x }
a += i

p a
time ~/.rvm/rubies/ruby-2.7.1/bin/ruby no-ractor.rb 
199999980000000

real	0m2.888s
user	0m2.881s
sys	0m0.006s

Now let’s use Ractor from Ruby 3.0:

ractors = 4.times.map do
	Ractor.new do
		i = 0
		10000000.times { |x| i += x }
		i
	end
end

p ractors.map(&:take).sum

Time with Ractor:

$ time ruby ractor.rb
199999980000000

real	0m1.549s
user	0m5.561s
sys	0m0.006s

Same output, just 2x faster (I assume because it’s 2 core processor).

Surely you can do other tricks to calculate the same thing:

x = 10000000 - 1
x * (x + 1) * 2  # => 199999980000000

But I put that in an inefficient loop just for the test.

2 Likes