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.
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix Interleave to prevent eager MoveNext calls #696
Fix Interleave to prevent eager MoveNext calls #696
Changes from 9 commits
c9aa710
66d1228
336423a
61e4921
b790e53
c569479
32f02e4
6ff6d03
8a972a9
1a4270f
76e3516
6f61760
81479fb
00ef366
d70974b
9be11ad
7d0aa0d
205c371
08e90e3
4fdb16c
ba740cc
a7f2c6f
efd0f4c
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have
Acquire
for this so I would take advantage of it here rather than roll out a duplicate implementation that needs separate testing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIU,
Acquire
do not return aIDisposable
soInterleave
keep the responsibility of disposing the enumerators when the result sequence enumerator is disposed.Achieve this with a
using
for each sequence is not feasible since the number ofusing
is dynamic.The only way I know to achieve this in a
yield
context is to use sometry {} finally {}
blocks. Because thefinally
block is called when the enumerator is disposed.We indeed can put this behavior in a specialized
IDisposable
class. When disposed, this object dispose a bunch of elements.DisposableGroup<T>
(declared in SortedMerge and only used here) looks to achieve this, butDisposableGroup<T>
internally use a list where I use aLinkedList
.(Rem:
DisposableGroup<T>
constructor doesn't dispose the already acquired object in case of error in the input sequence, likeAcquire
do. But anyway, the only usage ofDisposableGroup<T>
useAcquire
).I have chosen to use a
LinkedList
sinceRemove
operation is O(1) there.If we use a list or an array, where the the
Remove
operation is O(N), enumerating N sequences of 1 element will cost O(N²).I know that it is an uncommon use case (it may be less uncommon with #697), anyway this implementation manage it.
Rem: Because of
DisposableGroup<T>
implementationSortedMerge
is O(N²) in the case described above.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would return an array of enumerators that are disposable. Question is whether there is something to be gained out of not calling
GetEnumerator()
on other sequences until you get to them, but that will only optimize the case of someone eventually taking fewer elements than sequences!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Acquire
will not remove the necessity of atry {} finally {}
(with disposal of the enumerators in the finally block), and then is just an alias ofToArray()
.Acquire
is used and if an error occur while acquiring (the only purpose ofAcquire
is to manage this case), elements will be disposed twice, which is useless.So
Acquire
here is not optimal and useless.A
ToDisposableLinkedList()
method that have the functionality ofAcquire
(on error, dispose already acquired elements) and provide aDisposableLinkedList
that have the functionality ofDisposableGroup<T>
(being disposable and dispose its content when disposed) is the way to go forInterleave
,SortedMerge
andTranspose
.It will be used like that:
The
using
here allow to remove thetry {} finally {}
from here:MoreLINQ/MoreLinq/Transpose.cs
Lines 92 to 96 in 9fc8486
and here:
MoreLINQ/MoreLinq/Interleave.cs
Lines 141 to 146 in 9fc8486
and improve the
DisposableGroup/Acquire
combo from here:MoreLINQ/MoreLinq/SortedMerge.cs
Line 101 in 9fc8486
I opened #724 to refactor
Interleave
,SortedMerge
andTranspose
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Acquire
itself does not need to be inside a try-finally block and so it's not the same asToArray()
. It doesn't mean it prevents you from having one. It is, for example, perfectly safe to return the array fromAcquire
as the return value of a function:Of course, the disposal becomes the responsibility of the caller and will need a try-catch. It does prevent that.
It is useless but it's not a problem since it is a requirement for
IDisposable.Dispose
to be idempotent (per docs):That's a fine choice but I am not debating the most optimal solution first. My point with
Acquire
was that it's carefully designed and tested and so might be best to keep using it instead of putting everything into question. This PR was about fixing one problem but by trying to be cleverly optimal at the same time, it may introduce others bugs (and I believe it does)! This means it takes longer to review as the scope of change expands. Instead, I find that it helps to make it work, make it right and then make it fast.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just push a simple (I hope) implementation of Interleave based on the array returned by
Acquire
.The
LinkedList
implementation is in #726Anyway it's a rebuild of current implementation since it's over-complicated (manage not used cases) and flawed (original issue #694).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@atifaziz with the implementation using
Acquire
in this PR (currently at a7f2c6f), calls toGetEnumerator
are made eagerly and the code below throw:I don't know it it's acceptable
It doesn't throw in #726.