Optimize ZQueue#takeBetween #4584
Merged
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.
Currently, the
takeBetweenoperator onZQueuestarts out by callingtakeUpTo, which has an optimized implementation that attempts to immediately take the specified number of elements from the underlying queue. However, if the elements are not immediately available we fall back to repeatedly taking individual elements from the queue. This causes us to lose the benefits of taking multiple elements at once if they are not immediately available.This PR change the implementation of
takeBetweento calltakeonce if additional elements are required to suspend until new elements are available but then to recursively calltakeBetweento attempt to take multiple queue elements again in case more elements are available by the time we resume.