Rewrite promise implementation - #97
Merged
Merged
Conversation
Remove usage of `Concurrent::Event` in specs and replace it with `Queue` so that this spec no longer depends on concurrent-ruby.
This is a large rewrite of the promise implementation, replacing the comparable slow `Concurrent::IVar` with a lightweight custom implementation, and specifying a few edge cases. 1. `IVar` used several `Mutex` and events internally, and synchronized more than necessary. The new implementation uses a single `Mutex`, reducing contention and improving performance. 2. Do not reject a promise on a timeout. Waiting on a promise with a timeout that expires will not change the promise's state, allowing subsequent waits to continue where the last one left off. Because the previous simple implementation was driven by exceptions, every exception, including a timeout, rejected the promise. Now, it is possible to wait with different timeouts. 3. Honor the timeout of all threads waiting on the same promise, not only of the thread running its `#then` blocks. Before, all waiting happened on the `Mutex` synchronization, but that does not have a timeout at all. A long-running then-block being run on one thread could block other threads waiting on the same promise even if they had short timeouts. The new implementation separates between running and claiming/waiting to run, so threads with short timeouts are not blocked. Furthermore, because a timeout no longer affects the state, longer timeouts can still drive a promise to completion. Technically, the new promise implementation is no longer based on `Concurrent::IVar` and does not inherit from it anymore. A promise could have been used in other concurrent-ruby contexts, or any other `Concurrent::IVar` as a dependency. This is no longer possible! Lately, these changes reduce contention and improve the overall performance quite a bit. Additionally, a lot less objects are allocated. Which methods shall become public or not is still under consideration.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #97 +/- ##
==========================================
+ Coverage 97.23% 97.35% +0.11%
==========================================
Files 28 28
Lines 1121 1171 +50
==========================================
+ Hits 1090 1140 +50
Misses 31 31 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
This is a large rewrite of the promise implementation, replacing the comparable slow
Concurrent::IVarwith a lightweight custom implementation and specifying a few edge cases.IVarused severalMutexand events internally and synchronized more than necessary. The new implementation uses a singleMutex, reducing contention and improving performance.Do not reject a promise on a timeout. Waiting on a promise with a timeout that expires will not change the promise's state, allowing subsequent waits to continue where the last one left off. Because the previous simple implementation was driven by exceptions, every exception, including a timeout, rejected the promise. Now, it is possible to wait with different timeouts.
Honor the timeout of all threads waiting on the same promise, not only of the thread running its
#thenblocks.Before, all waiting happened on the
Mutexsynchronization, but that does not have a timeout at all. A long-running then-block being run on one thread could block other threads waiting on the same promise even if they had short timeouts.The new implementation separates between running and claiming/waiting to run, so threads with short timeouts are not blocked. Furthermore, because a timeout no longer affects the state, longer timeouts can still drive a promise to completion.
Technically, the new promise implementation is no longer based on
Concurrent::IVarand does not inherit from it anymore. A promise could have been used in other concurrent-ruby contexts or any otherConcurrent::IVaras a dependency. This is no longer possible!Lately, these changes reduce contention and improve the overall performance quite a bit. Additionally, a lot fewer objects are allocated.
Which methods shall become public or not is still under consideration.