ruby-concurrency / ruby-concurrency/concurrent-ruby

Proposal: Explore a CAS fast path for Semaphore with native atomics

Aperta
#1,118 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
Ruby
Stelle
5.8k
Fork
420
Merge medio
20h 45m
PR unite (30g)
4

Descrizione

Would you be open to a CAS fast path for Concurrent::Semaphore on CRuby with the existing C extension?

The current MutexSemaphore takes a mutex for every acquisition and release, even when permits are available. Using AtomicFixnum#compare_and_set could reduce this overhead.

Related design in Java

OpenJDK's Semaphore uses CAS to update permits, with AbstractQueuedSynchronizer handling queuing and blocking when acquisition cannot succeed. This provides a design precedent; performance on CRuby still needs its own evaluation.

Proposed approach

Use CAS when permits can be acquired immediately, and a mutex and condition variable when waiting is necessary. Enable this with the existing CRuby C extension, retaining the current implementations elsewhere. Preserve existing Ruby behavior, including large permit counts through a mutex-protected Integer fallback.

A prototype PR in my fork contains the implementation and tests; no new C code or runtime dependency is required.

Initial measurements

Apple M5 MacBook Pro, macOS 26.2, CRuby 4.0.2, YJIT disabled, CAtomicFixnum. The measured prototype was compared with the unchanged Mutex implementation. Results are median throughput over 7 samples after warmup, in millions of acquire/release pairs per second. Both implementations ran in the same process with alternating sample order.

Workload Existing Mutex Prototype Prototype / Mutex
One thread, one permit, acquire/release 2.153 3.130 1.45x
Eight threads, one permit, Thread.pass while acquired 0.253 0.241 0.95x

The waiting case was slower in this run and varied substantially; these results do not establish a waiting-path improvement. The prototype also changes waiter bookkeeping and notification behavior, so the gain cannot be attributed to CAS alone. Full samples and reproduction commands are in the fork PR.

Would this direction be of interest? I would appreciate feedback on the approach and any additional validation needed before preparing an upstream PR.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Start with lib/concurrent-ruby/concurrent/atomic/mutex_semaphore.rb and inspect the existing CRuby C extension, then review the linked prototype PR and measured commit. Compare the prototype's acquire/release and waiting behavior with the current implementation, and use its tests and reproduction commands to determine what validation is still needed.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
c, ruby
Ambito
backend
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Attiva
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.