Ractor is a new experimental feature in Ruby 3.0 that allows Ruby code to run in parallel on CPUs. It manages objects per Ractor and can move objects between Ractors, making moved objects invisible to the original Ractor. It can share certain "shareable" objects like modules, classes, and frozen objects between Ractors. For web applications to fully utilize Ractors, an experimental application server called Right Speed was created that uses Rack and runs processing workers on Ractors. However, there are still problems to address like exceptions when closing connections and accessing non-shareable constants and instance variables across Ractors before Ractors can be ready for production use in web applications.
5. Talks about Ractor in this Kaigi
• “Parallel testing with Ractors: putting CPUs to work”
by Vinicius Stock at Day 1
• “Ractor’s speed is not light-speed” (this talk)
by Satoshi Tagomori at Day 2
• “Ruby, Ractor, QUIC”
by Yusuke Nakamura at Day 3
6. Ractor’s Features
• Can run Ruby code of multiple Ractors in parallel on CPU cores
• by managing objects per Ractor
• Can move objects between Ractors
• Moved objects become invisible from the original Ractor
• Can share “shareable” objects between Ractors
• Modules, Classes
• Application code (Proc)
• De
fi
nitions (constants), Settings/Con
fi
gurations (frozen objects)
• …
7. What does “shareable” mean?
• Should be marked by Ractor.make_shareable(obj)
• will be frozen (in most cases)
• Proc: Ractor.make_shareable() makes Proc isolated
• “Isolated” Proc is not frozen, but shareable
• Proc#binding raises ArgumentError if it’s isolated
• All referred values (from the Proc) should be shareable too
11. Question: How fast is Ruby w/ Ractor?
• xN faster if the laptop has N cpu cores?
• 8 cores on M1
• Is it true for Web applications?
• Is that faster than N processes by fork?
• Connection passing on memory (Ractor) vs RPC (fork)
• Does my webapp run faster w/ Ractor than the current deployment?
12. Needs: Experimental App Server using Ractor
• Rack application server
• Rack: protocol between server and application
• All servers are Rack server: webrick, unicorn, thin, puma, …
• Processing workers on Ractor
• Receive established connection, read request
• Run your Rack application
• Write response
• Speed: Appropriate performance, less overhead
19. Problems about WebApps on Ractor
• SEGV at closing connections(?)
• Module/class instance variable accesses
• Accessing un-frozen (un-shareable) constants
• Dynamically de
fi
ned methods w/ un-shareable Proc
20. SEGV at closing connections (?)
• SEGV caused when it handles 2 (or more) connections
• bugs#18024
21. Module/Class Instance Variable Accesses
• Reading module/class instance variables causes Ractor::IsolationError
• Many frameworks (Rack, Sinatra, Rails) are heavily using it
• @var in the context of class << self
• for settings in many cases
• Used for default instance or default options (e.g., JSON.dump)
• Reading it will be allowed bugs#17592
• in the future (not
fi
xed yet)
22. Accessing Un-frozen/shareable Constants
• Constants can be accessible from Ractors, if deeply-frozen
• Use the magic comment always!
# frozen_string_literal: true
• Want a variant for Hash/Array (in my idea)
# frozen_shareable_constant_literal: true
23. define_method (1)
• Methods can be called in Ractor, usually
• Methods de
fi
ned by de
fi
ne_method may cause errors
• Blocks are unshareable!
• Frameworks (RoR/Sinatra/…) do this everywhere
25. Right Things to Be Done
• Applications: Check Ractor-safe or not
• Frameworks, libraries: Need many patches
• To make it Ractor-safe
• Ruby runtime: Make Ractor production ready
• More Ractor-safe core libraries
• More Ractor features (e.g, bugs#18139, etc)
• More features about productivity? (constant, proc, etc)