Project

General

Profile

Bug #22553 » ractor_repro.rb

jamescook83 (James Cook), 11/15/2026 11:41 AM

 
# ruby ractor_repro.rb [mode] [classes]
#
# mode:
# class object.class
# frozen? object.frozen?
# hash object.hash
# own_objects object.class, on objects each Ractor creates
# bind Kernel#class through bind_call
# warm object.class, after the main Ractor calls it once per class
#
# classes: number of distinct classes (default 2000). Each Ractor makes 2000 calls either way.
Warning[:experimental] = false

MODE = (ARGV[0] || "class").to_sym
COUNT = Integer(ARGV[1] || 2000)
CLASSES = Ractor.make_shareable(Array.new(COUNT) { Class.new })
OBJECTS = Ractor.make_shareable(CLASSES.map(&:new) * (2000 / COUNT))

def call_class = OBJECTS.each { |object| object.class }
def call_frozen = OBJECTS.each { |object| object.frozen? }
def call_hash = OBJECTS.each { |object| object.hash }
def call_class_on_own_objects = (CLASSES.map(&:new) * (2000 / COUNT)).each { |object| object.class }
def call_class_with_bind_call
kernel_class = Kernel.instance_method(:class)
OBJECTS.each { |object| kernel_class.bind_call(object) }
end

OBJECTS.each { |object| object.class } if MODE == :warm

ractors = Array.new(8) do
Ractor.new do
case MODE
when :class, :warm then call_class
when :frozen? then call_frozen
when :hash then call_hash
when :own_objects then call_class_on_own_objects
when :bind then call_class_with_bind_call
end
end
end
ractors.each(&:value)
puts "ok"
(2-2/2)