:cat2: `bundle exec ruby bin/benchmark-parse-ractor.rb --ractors=8 core sig stdlib --scale=10`
diff --git a/bin/benchmark-parse-ractor.rb b/bin/benchmark-parse-ractor.rb index 5a60cbd..d1b9d7e 100644 --- a/bin/benchmark-parse-ractor.rb +++ b/bin/benchmark-parse-ractor.rb
@@ -6,6 +6,8 @@ require "optparse" +Ractor.new{} + label = nil #: String? num_ractors = 4 #: Integer scale = 1 #: Integer @@ -25,10 +27,6 @@ opts.on("--scale=N", Integer, "Scale the number of files by N times (default: 1)") do |v| scale = v end - - opts.on("--batch=N", Integer, "Number of files to send per ractor (default: 30)") do |v| - batch = v - end end.parse!(ARGV) file_names = [] @@ -50,76 +48,55 @@ files[file] = RBS::Buffer.new(content: content, name: Pathname(file)).finalize end -# # class RactorParser -# # attr_reader :workers +def ractor_map(objects, num_ractors, &block) + port = Ractor::Port.new -# # attr_reader :result_port, :queue_port + block = Ractor.shareable_proc(&block) -# # def initialize(count) -# # @count = count + ractors = num_ractors.times.map do + Ractor.new(port, block, name: "worker-#{_1}") do |port, block| + results = [] + port << Ractor.current -# # @result_port = Ractor::Port.new() -# # @queue_port = Ractor::Port.new() + while obj = Ractor.receive + if obj == :result + break + else + results << block[obj] + port << Ractor.current + end + end -# # @workers = count.times.map do |i| -# # Ractor.new(result_port, queue_port, name: "worker-#{i}") do |result_port, queue_port| -# # while buffer = Ractor.receive -# # case buffer -# # when :start -# # queue_port << Ractor.current -# # else -# # # ms = Benchmark.realtime do -# # buffer.each do |buf| -# # result = RBS::Parser.parse_signature(buf) -# # # out_port << [Ractor.current, result] -# # # out_port << [Ractor.current, result] -# # # Ractor.shareable?(result) or raise "Parsing result of #{buffer.name} is not shareable" -# # # Ractor.make_shareable(result) or raise -# # result_port << result -# # end -# # # end - -# # # puts "[#{Ractor.current.name}] Parsed #{buffer.size} files in #{(ms * 1000).round} ms" -# # queue_port << Ractor.current -# # # out_port.send([Ractor.current, buffer], move: true) -# # # out_port.send([Ractor.current, Ractor.make_shareable(result)], move: false) -# # end -# # end -# # end -# # end -# # end - -# def parse(buffers, batch) -# results = [] - -# workers.each do |worker| -# worker.send :start -# end - -# input_thread = Thread.new do -# buffers.each_slice(batch) do |bufs| -# ractor = queue_port.receive -# ractor.send bufs -# end -# end - -# while buffers.size > results.size -# sig = result_port.receive -# results << sig -# end - -# input_thread.join - -# results -# end -# end - -class SingleParser - def parse(files) - files.map do |buffer| - RBS::Parser.parse_signature(buffer) + results end end + + t = Thread.new do + objects.each do |obj| + ractor = port.receive + ractor.send(obj) + end + + ractors.each do |ractor| + ractor.send(:result) + end + end + + t.join + + results = [] + + ractors.each do |ractor| + results.concat(ractor.value) + end + + results +end + +def single_parse(files) + files.map do |buffer| + RBS::Parser.parse_signature(buffer) + end end bufs = files.values.dup @@ -129,23 +106,23 @@ puts "Benchmarking with #{bufs.size} files (scale=#{scale})" Benchmark.ips do |x| - single = SingleParser.new - - (2..10).reverse_each do |i| - x.report("map #{i} ractors") do - RBS::RactorPool.map(bufs, i) { RBS::Parser.parse_signature(_1) } - end - x.report("each #{i} ractors") do - pool = RBS::RactorPool.new(i) { RBS::Parser.parse_signature(_1) } - pool.each(bufs) { nil } - end - end - - # (2..10).reverse_each do |i| - # end - x.report("serial") do - single.parse(bufs) + buf, dirs, decls = single_parse(bufs) + RBS::Source::RBS.new(buf, dirs, decls) end + + x.report("ractor_map(..., #{num_ractors})") do + ractor_map(bufs, num_ractors) do |buf| + buf, dirs, decls = RBS::Parser.parse_signature(buf) + RBS::Source::RBS.new(buf, dirs, decls) + end + end + + # x.report("RactorPool.map(..., #{num_ractors})") do + # RBS::RactorPool.map(bufs, num_ractors) do |buf| + # buf, dirs, decls = RBS::Parser.parse_signature(buf) + # RBS::Source::RBS.new(buf, dirs, decls) + # end + # end end
diff --git a/bin/ractor-resolve.rb b/bin/ractor-resolve.rb index 5ab09b5..144a8c2 100644 --- a/bin/ractor-resolve.rb +++ b/bin/ractor-resolve.rb
@@ -1,6 +1,8 @@ require "rbs" require "benchmark" +RBS.logger_level = Logger::FATAL + env_loader = RBS::EnvironmentLoader.new # env_loader.add(library: "pathname", version: nil) @@ -180,6 +182,7 @@ seq = Benchmark.realtime do buffers.each do |buffer| + # binding.irb buffer, dirs, decls = RBS::Parser.parse_signature(buffer) srcs << RBS::Source::RBS.new(buffer, dirs, decls) end @@ -197,7 +200,7 @@ # send_port.send(nil) # end -srcs.each do Ractor.make_shareable(_1) or raise end +# srcs.each do Ractor.make_shareable(_1) or raise end # s = Benchmark.realtime do # t = Thread.new do @@ -214,16 +217,25 @@ # end # puts "Sending #{srcs.size} sources to ractor time: #{s}s" +# GC.disable + lll = Benchmark.realtime do - RBS::RactorPool.map(srcs, size) do |src| + srcs = RBS::RactorPool.map(srcs, size) do |src| buffer, dirs, decls = RBS::Parser.parse_signature(src.buffer) # Ractor.shareable?(decls) or raise - RBS::Source::RBS.new(buffer, dirs, decls).freeze - # src + src = RBS::Source::RBS.new(buffer, dirs, decls) + # src = Ractor.make_shareable(src) or raise + # Ractor.shareable?(src) or raise + # Ractor.make_shareable(src) or raise + # RBS::Source::RBS.new(buffer, dirs, src.declarations) + src + # nil end end puts "Ractor parse time: #{lll}s" +exit + # GC.disable # lo = Benchmark.realtime do @@ -277,7 +289,7 @@ # puts "Load #{srcs.size} files in #{lo}ms in #{size} ractors" -exit +# exit # srcs.clear
diff --git a/lib/rbs/ractor_pool.rb b/lib/rbs/ractor_pool.rb index ea2ee10..e4e75d6 100644 --- a/lib/rbs/ractor_pool.rb +++ b/lib/rbs/ractor_pool.rb
@@ -16,16 +16,22 @@ Ractor.new(block, worker_port, result_port, STOP, name: "RBS::RactorPool(worker_ractor=#{_1})") do |block, worker_port, result_port, stop| worker_port << Ractor.current + results = [] #: Array[untyped] + loop do task = Ractor.receive if stop.equal?(task) break else - result = block[task] - result_port.send(result) - worker_port << Ractor.current + task.each do |t| + result = block[t] + results << result + worker_port << Ractor.current + end end end + + results.freeze end end @@ -38,40 +44,32 @@ } end - def map(objects) + def each(objects) + map(objects) + nil + end + + def map(objects, &block) results = [] #: Array[untyped] - each(objects) do |result| - results << result + thread = Thread.start do + objects.each_slice(100) do |obj| + ractor = worker_port.receive + ractor.send(obj) + end + + ractors.each { _1 << RactorPool::STOP } + end + + # thread.join + + ractors.each do |ractor| + results.concat(ractor.value) end results end - def each(objects, &block) - count = objects.size - - thread = Thread.start do - index = 0 - objects.each do |obj| - ractor = worker_port.receive - ractor.send(obj) - index += 1 - end - - # ractors.each { _1 << RactorPool::STOP } - end - - while count > 0 - yield result_port.receive - count -= 1 - end - - thread.join() - - nil - end - def self.map(objects, size, &block) pool = RactorPool.new(size, &block) pool.map(objects)