SlideShare a Scribd company logo
Ractor’s speed


is not light-speed.
Satoshi Tagomori (@tagomoris)
Satoshi Tagomori (@tagomoris)


a.k.a. Moris (モリス)


Freelance technical consultant (Aug 2021 ~)
Ractor's speed is not light-speed
Ractor


A new experimental feature


of Ruby 3.0


to run Ruby code in parallel on CPUs
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
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)


• …
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
Isolated Proc (1)
Isolated Proc (2)
Ractor:


The Feature for Speed
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?
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
light speed
light speed
right_speed
https:/
/github.com/tagomoris/right_speed
https:/
/rubygems.org/gems/right_speed
$ rackup -s right_speed config.ru


-O Host=127.0.0.1


-O Port=8080


-O Workers=12
Default # of workers is equal to # of CPUs
Demonstration


Running apps on right_speed


* Rack app


* Sinatra app


* Ruby on Rails app
https:/
/github.com/tagomoris/demo-webapps
Demonstration


Traffic


* single client connection + thread


* multiple client connections + threads
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
SEGV at closing connections (?)
• SEGV caused when it handles 2 (or more) connections


• bugs#18024
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)
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
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
define_method (2)
• Ractor-safe methods with shareable/isolated blocks


• Need shorthand!
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)
Thank you!

More Related Content

Ractor's speed is not light-speed

  • 1. Ractor’s speed is not light-speed. Satoshi Tagomori (@tagomoris)
  • 2. Satoshi Tagomori (@tagomoris) a.k.a. Moris (モリス) Freelance technical consultant (Aug 2021 ~)
  • 4. Ractor A new experimental feature of Ruby 3.0 to run Ruby code in parallel on CPUs
  • 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
  • 16. $ rackup -s right_speed config.ru -O Host=127.0.0.1 -O Port=8080 -O Workers=12 Default # of workers is equal to # of CPUs
  • 17. Demonstration Running apps on right_speed * Rack app * Sinatra app * Ruby on Rails app https:/ /github.com/tagomoris/demo-webapps
  • 18. Demonstration Traffic * single client connection + thread * multiple client connections + threads
  • 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
  • 24. define_method (2) • Ractor-safe methods with shareable/isolated blocks • Need shorthand!
  • 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)