Repository navigation
perf(lazy-cell): avoid locking on uncontended initialization - #414
Open
rokadepiyush49-rgb wants to merge 1 commit into
Open
rokadepiyush49-rgb wants to merge 1 commit into
rokadepiyush49-rgb wants to merge 1 commit into
Conversation
Force a LazyCell by taking two of three internal semaphore permits instead of locking a Mutex. The spare permit lets an uncontended release skip the wait-queue lock while still allowing only one caller to drive the stored future.
This branch has not been deployed
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.
Summary
Reduce fresh
LazyCellforcing cost by taking two of three internal semaphore permits instead of locking its stateMutex, following #412 forOnceCell. TheMutexguard always released through the semaphore's wait-queue lock; the spare permit lets an uncontended release skip it while still allowing only one caller to drive the stored future.On an Apple M4 (macOS), three alternating rounds of ten runs reduced the median for seven million
LazyCell::newconstructions and first forces from 309–317 ms to 115–127 ms, and forLazyCell::from_futurefrom 280–309 ms to 130–141 ms. Forcing an initialized cell stayed at about 300 ms for 85 million operations. An allocation counter observed one 64-byte allocation per fresh cell before this change and none after.Add
once::forcebenchmarks for both constructors and an initialized cell, plus regression coverage for a queued caller resuming the same pinned future after the driver is cancelled, poisoning observed by a queued caller, cancellation after a partial or full permit grant, and completion observed from another thread. The handoff case is also covered by the Miriunsafe_paths_testtarget.Fixes #399.
Design Notes
LazyCelluses the internal semaphore with a small permit guard rather than the publicSemaphore, solazy-cell = ["mutex"]is unchanged; depending onsemaphoreinstead would change which modules the feature enables. With the state in anUnsafeCell,Syncis implemented with the bounds theMutexpreviously produced (T: Send + Sync,Fut: Send,F: Send), andSendremains automatic.