perf(sync): defer semaphore wake-state allocation - #1044
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
Optimizes non-cancellable semaphore acquisition by deferring wake-state allocation until suspension while preserving permit and cancellation semantics.
Changes:
- Adds lazy wake-state construction and exception-safe suspension.
- Expands allocation, race, and permit-conservation tests.
- Adds semaphore benchmarks and documentation.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
include/elio/sync/semaphore.hpp |
Implements deferred wake-state allocation. |
tests/unit/test_sync_cancellation.cpp |
Adds semaphore regression coverage. |
examples/microbench.cpp |
Adds semaphore performance benchmarks. |
wiki/Performance-Tuning.md |
Documents performance behavior. |
wiki/API-Reference.md |
Documents allocation and exception semantics. |
CHANGELOG.md |
Records the optimization. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Make the no-token
sync::semaphore::acquire()ready path allocation-free while preserving permit accounting, FIFO publication for queued waiters, cancellation arbitration, and dequeue-to-schedule lifetime.A no-token waiter now creates shared wake state only after
await_ready()observes no permit. Construction occurs before taking the semaphore queue lock, and the existing locked count recheck decides whether to consume a concurrently released permit or publish the waiter. Token-aware acquires retain eager state and theirnoexceptcancellation-versus-grant path.Type of Change
Related Issues
Closes #1039
Related to #1040
Changes Made
Core Changes
std::shared_ptr<wake_state>state in semaphore-private manual storage while retaining the 56-byte waiter layout.noexceptcancellation path.noexcepttoken helper.API Changes (if applicable)
Normal
co_await semaphore.acquire()source and permit semantics are unchanged. The low-level no-token suspend exception boundary changes.Before:
After:
Allocation happens before locking the queue, consuming a permit, or publishing a waiter, so
std::bad_allocleaves permit and queue state unchanged. A permit released betweenawait_ready()andawait_suspend()may be consumed by the locked recheck after one unused allocation. Token-aware suspension remainsnoexcept.Migration Guide (if breaking change)
No migration is required for normal
co_await semaphore.acquire()use. Low-level integrations that require the exact no-tokenawait_suspendmember to benoexceptmust allow or handlestd::bad_alloc. Token-aware code retains the existing non-throwing suspend surface.Testing
Unit Tests
Integration Tests
Sanitizer Testing
Test Results
Deterministic coverage verifies ready=0 allocations, parked=1 allocation/publication, release-before-suspend accounting, a legal pre-publication permit steal, strong
bad_allocsafety with empty and released permits, eager token state, public true/false helper compatibility, cancellation/grant behavior, and popped permit recovery.Pinned Release testing used long-lived frames, fixed CPU affinity, interleaved baseline/candidate order, and same-source binaries:
The park/unlink benchmark isolates waiter creation/publication/removal from
release()wake-vector allocation. The forced handoff benchmark includes that existing common cost. No timing threshold is enforced in CI.Checklist
Code Quality
Documentation
Testing
Compatibility
co_awaituse remains compatible; the low-level exception change is documentedCHANGELOG.mdPerformance (if applicable)
Screenshots / Diagrams
Not applicable.
Additional Notes
The private manual storage defers construction of the existing shared wake-state lease; it does not replace it with frame-local state. A parked waiter still has an independent lifetime across dequeue and scheduling.
The implementation deliberately does not add a lock/unlock precheck before allocation and does not allocate while holding the semaphore mutex. Already-published FIFO ordering remains unchanged; an unpublished waiter has no FIFO entitlement over a later ready acquire.
Reviewer Guidance
Areas requiring special attention:
noexceptcompatibility.Questions for reviewers: