From: "ko1 (Koichi Sasada) via ruby-core" Date: 2026-11-29T09:51:17+00:00 Subject: [ruby-core:127761] [Ruby Feature#21869] Add receive_all Method to Ractor API for Message Batching Issue #21869 has been updated by ko1 (Koichi Sasada). Sorry for the late reply again. I measured the PR (4cc333f667) on the current master (2dc7d694fc). I compared three ways to consume 1M messages: - receive: port.receive for each message - loop: a = [port.receive]; while m = port.receive(timeout: 0); a << m; end - receive_all: this proposal Seconds, median of 5 runs: | | receive | loop | receive_all | |-------------------------------------------|---------|-------|-------------| | backlog, fixnums | 0.045 | 0.047 | 0.021 | | backlog, frozen strings | 0.055 | 0.061 | 0.037 | | backlog, strings | 3.015 | 0.485 | 0.461 | | backlog, strings, 4 producers | 2.944 | 0.483 | 0.458 | | producer running, fixnums | 0.239 | 0.240 | 0.287 | | producer running, strings | 1.152 | 1.050 | 1.151 | | 4 producers running, strings | 6.118 | 0.672 | 0.657 | | producer running, write + flush per batch | 14.978 | 1.355 | 1.561 | "backlog" means all messages are queued before the consumer starts (same as the benchmark in the PR). "producer running" means the producers and the consumer run at the same time. To be honest, I couldn't see a performance improvement over the loop written in Ruby, except for a backlog of shareable objects (about 2x, 25ns per message). The 4x in the description comes from batching itself, and the loop gets it too. (receive is very slow with a backlog of unshareable strings. This is a GC problem on master and not related to this proposal. I'll look into it separately.) I also found two problems in the PR: 1. Messages are lost on a closed port. port << 1; port << 2; port.close; port.receive_all raises Ractor::ClosedError and both messages are gone (receive returns 1, 2 and then raises). 2. It doesn't return all arrived messages if the port already has a delivered one: port << :a; port << :b; port.receive; port << :c; port.receive_all #=> [:b]. So I couldn't find a performance reason. I can see two reasons to have it: 1. Doing something once per batch (flush, a bulk INSERT, sending one packet). But in practice we need a limit for that, and the loop above can do it too. 2. Receiving nil/false messages. The loop can't do it because receive(timeout: 0) returns nil on timeout. Do you have a real application for 1? I'd like to know how it will be used (how many messages per batch, whether a limit is needed) before deciding. ---------------------------------------- Feature #21869: Add receive_all Method to Ractor API for Message Batching https://bugs.ruby-lang.org/issues/21869#change-120310 * Author: synacker (Mikhail Milovidov) * Status: Open * Assignee: ractor ---------------------------------------- **Summary** The Ractor API provides an excellent mechanism for inter���thread communication, but it currently lacks a built���in message batching technique. I propose adding a receive_all method to enable batch processing of messages, which can significantly improve performance in high���load scenarios. **Motivation** In distributed queued systems, processing messages one���by���one (as with the current receive method) can introduce unnecessary overhead. Batch processing allows: Reduced context���switching overhead. More efficient I/O operations (e.g., fewer file writes). Better throughput in high���concurrency environments. **Proposed Solution** Add a receive_all method to the Ractor API that: Returns all available messages in the Ractor���s mailbox at once (as an array). **Demonstration Code** Below is a benchmark comparing individual receive vs. batch receive_all: ``` ruby require 'benchmark' class RactorsTest def initialize(count) @count = count @ractor1 = Ractor.new(count, 'output1.txt') do |count, filename| File.open(filename, 'w') do |file| while count.positive? message = receive file.write("Ractor 1 received message: #{message}\n") file.flush count -= 1 end end end @ractor2 = Ractor.new(count, 'output2.txt') do |count, filename| File.open(filename, 'w') do |file| while count.positive? messages = receive_all messages.each do |message| file.write("Ractor 2 received message: #{message}\n") end count -= messages.length file.flush end end end end def run1 @count.times do |i| @ractor1.send("Message #{i + 1}") end @ractor1.join end def run2 @count.times do |i| @ractor2.send("Message #{i + 1}") end @ractor2.join end end records = 1_000_000 test = RactorsTest.new(records) p [:once, Benchmark.realtime { test.run1 }.round(2)] p [:all, Benchmark.realtime { test.run2 }.round(2)] ``` **Benchmark Results** On my system, receive_all shows ~4x improvement over individual receive: **Key Observations:** Ractor1 (using receive): Processes each message individually, resulting in frequent I/O calls. Ractor2 (using receive_all): Processes all queued messages at once, minimizing I/O overhead -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/