Get the latest tech news

Unlocking Ractors: class instance variables in Ruby


In a previous post about ractors, I explained why I think it’s really unlikely you’d ever be able to run an entire application inside a ractor, but that they could still be situationally very useful to move CPU-bound work out of the main thread, and to unlock some parallel algorithm.

It’s a fine solution when the operation you are protecting is a relatively slow one, but in our case, reading an instance variable is extremely cheap, so any kind of lock, even an uncontended one, will be disproportionally costly and ruin performance. Multithreaded programming is tricky, but even more so when allowing multiple threads to read and write the same memory, because processors have all sorts of caches, hence a variable doesn’t only reside in one place in your RAM. And this code is now riddled with race conditions because regular and complex shapes are radically different, even in the happy path case where we’re adding a new instance variable, we might turn@fields from an array into a Hash.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of ruby

ruby

Photo of Unlocking Ractors

Unlocking Ractors

Related news:

News photo

What Is the Difference Between a Block, a Proc, and a Lambda in Ruby? (2013)

News photo

Coding agent in 94 lines of Ruby

News photo

Ruby 3.5 Feature: Namespace on read