🔥

[Bug #19288] Ractor で JSON をパースしたときのパフォーマンスが遅いというバグ報告

2025/02/10に公開

[Bug #19288] Ractor JSON parsing significantly slower than linear parsing

  • Ractor で JSON をパースしたときのパフォーマンスが遅いというバグ報告
require 'json'
require 'benchmark'

CONCURRENT = 5
RACTORS = false
ELEMENTS = 100_000

data = CONCURRENT.times.map do
  ELEMENTS.times.map do
    {
      rand => rand,
      rand => rand,
      rand => rand,
      rand => rand
    }.to_json
  end
end

ractors = CONCURRENT.times.map do
  Ractor.new do
    Ractor.receive.each { JSON.parse(_1) }
  end
end

result = Benchmark.measure do
  if RACTORS
    Ractor.make_shareable(data)

    CONCURRENT.times do |i|
      ractors[i].send(data[i], move: false)
    end

    ractors.each(&:take)
  else
    # Linear without any threads
    data.each do |piece|
      piece.each { JSON.parse(_1) }
    end
  end
end

puts result
Ractor:
  8.264577   1.211798   9.476375 (  3.401614)
no Ractor:
  2.353667   0.003042   2.356709 (  2.357060)
  • 原因は JSON をパースする際の内部処理でロックを利用しているぽい?
  • 2年前のチケットなんですが現時点でもまだ影響があるみたいですね?
  • 最近また話題になっていました
  • あと主題とは違うんですが Ractor の利用例や安定化の話などがちらっとでていましtね
  • 2年前と比べてなにかしら状況が変わったのかしら
GitHubで編集を提案

Discussion