ruby-concurrency / ruby-concurrency/concurrent-ruby

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

Open
#1,118 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
5.8k
Forks
420
Avg merge
20h 45m
Merged PRs (30d)
4

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

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.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, ruby
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.