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.
As mentioned in #282,
os_unfair_lockis generally faster all-around (slightly when uncontended, significantly when contended), since it does not need to maintain fairness among waiting threads.This branch is a transitional implementation purely in Swift, until eventually Swift supports availability propagation of enum cases to switch case scopes.Related: SR-4079Lockis an abstract base class of two implementations:UnfairLockandPthreadLock. When constructing a lock throughLock.Type.make, it picks the implementation with#availableruntime checks.It turns out that virtual dispatch (subclassing) is still pretty fast, and is certainly faster than the implementation of the #282 trick in pure Swift, which requires per-instance thunks.
Benchmark Source.
Existentials are rather slow, by the way.
Inspecting the generated object code with
-Owmoreveals that Swift has inlined the vtable. So I guess even if SR-4079 got fixed someday, it would hardly be faster thanLock.