Concurrence et GVL : le modèle mental de l'attente contre le calcul en Ruby Concurrency and the GVL: the mental model of waiting versus computing in Ruby

En MRI, le GVL n'autorise qu'un thread Ruby à la fois : les threads chevauchent l'attente mais ne parallélisent pas le calcul. Mon goulot est-il attente ou calcul ? Ruby 3.4.In MRI, the GVL lets only one Ruby thread run at a time: threads overlap waiting but don't parallelize computation. Is my bottleneck waiting or computing? Ruby 3.4.

AuteurAuthor
Ludovic Cleuet
ThèmeTopic
PerformancePerformance
PubliéPublished

En bref. Les threads Ruby chevauchent l’attente, pas le calcul.

Le modèle en une phrase

Le GVL n’autorise qu’un thread Ruby à la fois : il se libère pendant l’attente (I/O), jamais pendant le calcul.

In short. Ruby threads overlap waiting, not computing.

The model in one sentence

The GVL lets only one Ruby thread run at a time: it is released while waiting (I/O), never while computing.

4.times { sleep 0.5 }
4 threads { sleep 0.5 }
Attente contre calculWaiting versus computing≈ 2,0 s≈ 2,0 s2.02 sdurées mesurées sur une machine : l'ordre de grandeur compte, pas la décimaledurations measured on one machine: the order of magnitude matters, not the decimalvérifié avec Ruby 3.4checked with Ruby 3.4
  1. 4.times { sleep 0.5 } — Quatre attentes à la suite : environ 2 secondes.Four waits in a row: about 2 seconds.
  2. 4 threads { sleep 0.5 } — Le GVL se libère pendant l'attente : les quatre se chevauchent, environ 0,5 s.The GVL is released while waiting: the four overlap, about 0.5 s.
Le GVL n'autorise qu'un thread Ruby à la fois : les threads chevauchent l'attente mais ne parallélisent pas le calcul. Ton goulot est-il une attente ou un calcul ?The GVL lets only one Ruby thread run at a time: threads overlap waiting but do not parallelize computation. Is your bottleneck waiting or computing?

La preuve, avec Ruby 3.4

Quatre tâches, mesurées avec Benchmark.realtime :

io  = -> { sleep 0.5 }
cpu = -> { x = 0; 3_000_000.times { x += 1 }; x }

4.times { io.() }                                              # ≈ 2,02 s
4.times.map { Thread.new { io.() } }.each(&:join)              # ≈ 0,51 s

4.times { cpu.() }                                             # ≈ 0,53 s
4.times.map { Thread.new { cpu.() } }.each(&:join)             # ≈ 0,49 s
4.times.map { Ractor.new { x = 0; 3_000_000.times { x += 1 } } }.each(&:take)   # ≈ 0,17 s

L’attente se chevauche (2,02 s → 0,51 s), le calcul non (0,53 s → 0,49 s). Les Ractors, eux, parallélisent le calcul.

Ce que l’image cache

Ces durées sont celles d’une machine : l’ordre de grandeur compte, pas la décimale. Les Ractors sont expérimentaux en 3.4 (ils le signalent à l’exécution) et n’acceptent pas n’importe quel objet partagé. Une application réelle mélange attente et calcul : mesure ton goulot avant de choisir.

Pour aller plus loin

Le numéro ci-dessous détaille le GVL, les threads, les Ractors et les processus, avec des exercices.

The proof, with Ruby 3.4

Four tasks, measured with Benchmark.realtime:

io  = -> { sleep 0.5 }
cpu = -> { x = 0; 3_000_000.times { x += 1 }; x }

4.times { io.() }                                              # ≈ 2.02 s
4.times.map { Thread.new { io.() } }.each(&:join)              # ≈ 0.51 s

4.times { cpu.() }                                             # ≈ 0.53 s
4.times.map { Thread.new { cpu.() } }.each(&:join)             # ≈ 0.49 s
4.times.map { Ractor.new { x = 0; 3_000_000.times { x += 1 } } }.each(&:take)   # ≈ 0.17 s

Waiting overlaps (2.02 s → 0.51 s), computing doesn’t (0.53 s → 0.49 s). Ractors do parallelize computation.

What the picture hides

These durations are from one machine: the order of magnitude matters, not the decimal. Ractors are experimental in 3.4 (they warn at runtime) and don’t accept just any shared object. A real application mixes waiting and computing: measure your bottleneck before choosing.

Go deeper

The issue below details the GVL, threads, Ractors and processes, with exercises.

Questions fréquentesFrequently asked questions

Qu'est-ce que le GVL en Ruby ?What is the GVL in Ruby?

Le Global VM Lock de MRI : il n'autorise qu'un seul thread Ruby à exécuter du code à la fois, par processus.MRI's Global VM Lock: it lets only one Ruby thread execute code at a time, per process.

Les threads accélèrent-ils un calcul CPU ?Do threads speed up a CPU computation?

Non : mesuré sur quatre calculs, 0,53 s à la suite contre 0,49 s en quatre threads. Un seul thread tient le GVL à la fois.No: measured on four computations, 0.53 s in sequence versus 0.49 s with four threads. Only one thread holds the GVL at a time.

Pourquoi les threads aident-ils pour l'I/O ?Why do threads help with I/O?

Parce que le GVL se libère pendant l'attente : quatre sleep 0.5 prennent environ 2,0 s à la suite, environ 0,5 s en quatre threads.Because the GVL is released while waiting: four sleep 0.5 take about 2.0 s in sequence, about 0.5 s with four threads.

Comment paralléliser un vrai calcul en Ruby ?How do I parallelize real computation in Ruby?

Avec des Ractors (chacun a son propre GVL : environ 0,17 s dans la même mesure) ou des processus. Les Ractors restent expérimentaux.With Ractors (each has its own GVL: about 0.17 s in the same measurement) or processes. Ractors remain experimental.

Quelle question poser avant de choisir ?What question should I ask before choosing?

Mon goulot est-il de l'attente (threads, async) ou du calcul (Ractors, processus) ? Mesure avant de choisir, pas après.Is my bottleneck waiting (threads, async) or computing (Ractors, processes)? Measure before choosing, not after.

RubyGVLThreadsRactorModèle mental

← Tous les articlesAll articles