I need to distribute some massive work like checksums/encryption between CPU cores.
Here is my simple test:
require 'digest/sha2'
N = 1_500_000
def workload
N.times do |n|
Digest::SHA2.base64digest(n.to_s)
end
end
puts 'in the main thread'
t_start = Time.now
workload
puts "Total: %.3f" % (Time.now - t_start)
puts
worker1 = Ractor.new do
Ractor.receive
print '['; workload; ']'
end
worker2 = Ractor.new do
Ractor.receive
print '['; workload; ']'
end
puts 'in 2 ractors'
t_start = Time.now
worker1.send 'start'
worker2.send 'start'
print '='
print worker1.take
print worker2.take
puts
puts "Total: %.3f" % (Time.now - t_start)
I can clearly see how 2 CPU cores are loaded using Ractors but performance is so strange:
On Linux VPS:
7.7s — workload in the main thread
10s (+30%) — in 1 Ractor (same workload) — expected this to be close to 7.7
32s — in 2 Ractors (each have same workload) — expected this to be close to 7.7
4-core CPU 2.6GHz
CentOS 7.9
ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-linux]
On Windows 10:
6.5s — in the main thread
9s (+38%) — in 1 Ractor
11s — in 2 Ractors — not so bad, seems to be faster than same 2 workloads in the main thread.
2-core CPU 3.6GHz
ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x64-mingw32]
Is it really the current state of the Ractors performance or am I missing something?