3.x: Add eager truncation to bounded replay() to avoid item retention #6532
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 PR adds the
eagerTruncate
option to thereplay
operator so that thehead
node will lose the item reference it holds upon truncation.The bounded buffers in
replay
implement a linked list that when truncated, moves the head reference forward along the links atomically. This allows late consumers to pick up the head and follow the links between them to get items replayed. However, the truncation may happen concurrently with a consumer working on some prior nodes so if the truncation wouldnull
out the value, the consumer reaching the same node would seenull
as well and fail.To avoid this type of retention, the head node has to be refreshed with a new node still pointing to the next node in the chain but without any value.
The reason this is not the default is that it requires an additional allocation for each new incoming value when the buffer is full, which would reduce performance in cases where the excess retention is not a problem.
Overloads to both the direct and function-variants of both
Flowable.replay()
andObservable.replay()
have been added. To avoid too many overloads, only one extra overload has been added extending the signature of the longest parameterized method per each bounds mode (size, time, time+size).Their unit test files have been cloned so that both the non-eager (original) behavior and the eager behavior is tested separately.
Fixes #6475