r/ruby • • 2d ago

Question GVL and concurrent programming

Ruby cannot perform real concurrent processing on multicore microprocessors without using ractors because of the GVL. It will only simulate multiple simultaneous processes with threads, queues, mutexes, etc. in a single processor core, no true parallel processing. How does this limitation affect true parallel programming in Ruby, that is, how much more complicated is it in producing true multicore parallel programming? I am still working on learning Ruby and this is part of my self-inflicted learning process in parallel with Huw Collingworth Book of Ruby.

11 Upvotes

27 comments sorted by

View all comments

11

u/schneems Puma maintainer 2d ago

real concurrent processing

Very pedantic, but it can do things concurrently. It cannot do them in parallel. You can have concurrency on a single CPU that implements time slicing, you cannot have parallelism on a single CPU.

true parallel programming

I think you knew, just being pedanic. The two terms are often used interchangably, but used strictly they have distinct different meaning.

You CAN have parallelism via multiple processes. Therefore you CAN saturate all cores. But it has tradeoffs. Like https://www.heroku.com/blog/upgrade-to-puma-7-and-unlock-the-power-of-fair-scheduled-keep-alive/ you end up having problems with coordination between truly parallel units of work that are as separated like a process (versus a thread https://www.schneems.com/2017/10/23/wtf-is-a-thread/). We have async, but it's concurrent and not parallel, unless you make an IO call. It can saturate one core, but you need ractors or processes to saturate multiple.

how much more complicated is it in producing true multicore parallel programming?

Most developers don't need it directly. We use frameworks and libraries like Puma and scale out horizontally with multiple small processes. Parallel and concurrent programming in any language is usually hard mode. Ruby is no different. Rust probably is the safest version of this, but it comes at a large ergonomic cost because you have to prove your code is good to the compiler. FWIW I like Rust. I recommend learning at least a little of it for any programmer.

2

u/KimJongIlLover 2d ago

What about elixir? Offers true concurrency but doesn't give you the rust stare.

1

u/Edthal01 2d ago

As I understood: Parallel in separate cores more complicated than concurrent in single core.

4

u/schneems Puma maintainer 2d ago

That's usually true because parallelism means things happening at literally the same time, and most people think about code sequentially and are bad at remembering external events that might affect what they're doing. Race conditions and deadlocks and friends are very difficult to find, and sometimes.

Concurrent programming is easier but can still have surprises. It also depends on the concurrency model. Technically the threading model with MRI's GIL/GVL means that only one Ruby thread can be executing at any given time, but writing code where the interpreter could switch to run another thread inbewtween EVERY virtual machine instruction ALSO has some difficult to reason about edge cases and bugs. In practice I would treat both as equally difficult. There are some patterns like "actor model" and "producer consumer loops" that can make changes easier to reason about, but they're still all more difficult to work with than regular, single threaded, sequential code. (this is true in all languages, not just Ruby).

1

u/yxhuvud 2d ago

They also didn't specify the actual needs in terms of sharing, so the simplest solution is to just spin up multiple workers and not share memory.