Ruby & the Multicore CPU: Part Five - Ruby 3.0 Concurrency & Parallelism
Ruby 3.0 is scheduled for release Christman 2020. The information below is subject to change.
Ruby 3 is proposing two new concurrency abstractions. Guilds for CPU bottlenecks, and asynchronous Fibers for IO bottlenecks. Conscious of the difficulties and general resistance of programmers to significant language version migrations, Matz is trying to ensure that the new features described below are as backwards-compatible as possible. That leads to a few compromises, but it looks like we will get some useful new tools!
Guilds
A Guild (or maybe it will be called Isolate) is a specified collection of objects, a bit like a module. Objects within a guild can communicate and share information much like all objects can in 2.7 and below. Everything in the same guild shares a common mutex, so effectively, the GIL which was once VM-wide is moved down a level to the guild.
Other guilds will have their own, separate mutexes, and therefore can run in parallel on a separate CPU core.
Guilds can only communicate with objects from a different guild via Channels. These channels can only send immutable objects. Ruby 3 will be adding new methods of deep-freezing objects and collections to facilitate this. This provides an actor-like, safe method of concurrency.
Code execution would require isolated blocks. These are code blocks that cannot reference external local variables or any global variables.
Asynchronous Fibers
I’m not sure that the nomenclature for the improved threading model has been confirmed yet. However, the proposal is for new, improved, lightweight non-blocking threads that take advantage of a reactor model to improve threaded performance. Most of the implementation details are hidden from the programmer.
This is yet to be confirmed, put the current leading proposal for the syntax is:
fiber1 = Fiber.new do
puts 'old, blocking fiber'
end
fiber2 = Fiber do
puts 'new, non blocking fiber'
end
Blue Gene image By Argonne National Laboratory’s Flickr page - originally posted to Flickr as Blue Gene / PFrom Argonne National LaboratoryUploaded using F2ComButton, CC BY-SA 2.0, https://commons.wikimedia.org/w/index.php?curid=6412306
Gold Quantum image National Institute of Standards and Technology / Public domain
- Introduction
- Concurrency vs. Parallelism
- Processes
- Threads
- Fibers
- Synchronicity
- Ruby up to 2.7
- MRI Ruby
- JRuby
- Rubinius
- TruffleRuby
- Current Concurrency Paradigms
- Queues and Jobs
- Communicating Sequential Processes
- Actor Model
- Reactor Model
- Ruby 3.0 Concurrency and Parallelism