- Ractors in Ruby are useful for moving CPU-bound work out of the main thread but have implementation bugs and performance issues due to the Ruby VM's global lock.
- Class instance variables and class variables are a major contention point for Ractors, leading to significant performance degradation.
- A benchmark shows that using Ractors for parallel computation can be 8 times slower than single-threaded execution due to VM lock contention.
- Secondary Ractors can read but not write class instance variables, and the values must be shareable to avoid isolation errors.
- Proposed solutions to reduce lock contention include finer-grained locks, read-write locks, and lock-free approaches using atomic operations.
- Instance variables in Ruby are managed through shapes and fields arrays, with complex shapes handling cases where instance variables are frequently added or removed.
- A lock-free solution involves delegating instance variable management to a separate GC-managed object, allowing atomic updates without locks.
- This approach improves performance significantly, making Ractors nearly 3 times faster than single-threaded execution in benchmarks.
- The solution also incidentally fixes issues with Ruby's new Namespace feature by allowing per-namespace instance variable management.
- Memory usage is not expected to increase significantly due to the overhead of malloc being comparable to that of a Ruby object.