Repository navigation
Conversation
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
Tagging subscribers to this area: @dotnet/area-system-collections |
eb49efb to
e81a54b
Compare
| public IEnumerable<T> Reverse() | ||
| { | ||
| return new ReverseEnumerable(_root); | ||
| return ReverseIterator(this); |
There was a problem hiding this comment.
What does ReverseIterator accomplish that simply returning a new Enumerator(builder.Root, builder, reverse: true); doesn't do? It boxes instead of allocating I guess. But that shouldn't be a problem according to
There was a problem hiding this comment.
If I understood your question correctly, Enumerator implements IEnumerator<T>, but not IEnumerable<T>, so it cannot be returned directly from Reverse() since its return type is IEnumerable<T>.
Also, even if boxing wouldn't be a problem here, why introduce it in the first place when we can avoid it entirely by keeping the enumerator as a value type?
There was a problem hiding this comment.
I did indeed miss that it returned a IEnumerable<T>!
There was a problem hiding this comment.
Isn't ReverseEnumerable unused after this change then?
There was a problem hiding this comment.
Yup, It is still used here in
However, I believe we have an opportunity to eliminate
ReverseEnumerable entirely, since the existing Enumerator already supports reverse iteration. We could implement it directly instead (as example):
public IEnumerable<T> Reverse()
{
Requires.NotNull(_root, nameof(_root));
using var enumerator = _root.Reverse();
while (enumerator.MoveNext())
{
yield return enumerator.Current;
}
}Alternatively, at the very least, we could improve ReverseEnumerable to support the builder correctly as well, by passing the builder instance to the enumerator.
For example:
private sealed class ReverseEnumerable : IEnumerable<T>
{
private readonly Node _root;
+ private readonly Builder? _builder;
internal ReverseEnumerable(Node root)
{
Requires.NotNull(root, nameof(root));
_root = root;
}
+ internal ReverseEnumerable(Builder builder)
+ {
+ Requires.NotNull(builder, nameof(builder));
+ _builder = builder;
+ _root = builder.root;
+ }
public IEnumerator<T> GetEnumerator()
{
+ if (_builder is not null)
+ {
+ return GetBuilderEnumerator(_root, _builder);
+ }
+
return _root.Reverse();
}
+ private static IEnumerator<T> GetBuilderEnumerator(Node root, Builder builder)
+ {
+ using Enumerator enumerator = new Enumerator(builder.root, builder, reverse: true);
+ while (enumerator.MoveNext())
+ {
+ yield return enumerator.Current;
+ }
+ }
+
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}This would allow both ImmutableSortedSet<T> and Builder to use ReverseEnumerable correctly, while preserving the builder's mutation tracking.
Personally, I lean towards the first approach, and if we find it appropriate, I'll implement it in a separate follow-up PR.
krwq
left a comment
There was a problem hiding this comment.
I suggest we slightly simplify but we can do further analysis on enumerator in the follow up
e81a54b to
68902eb
Compare
68902eb to
3c65f30
Compare
Description
ImmutableSortedSet<T>.Builder.Reverse()enumerated_rootviaReverseEnumerablewithout passing the builder instance, bypassing version/mutation tracking and allowing concurrent mutations during enumeration without throwingInvalidOperationException.Reverse()to iterate via a static local iterator (ReverseIterator) that constructsEnumeratorwithbuilder.Root,builder, andreverse: true, matchingSortedSet<T>.Reverse()mutation tracking.ReverseThrowsOnMutationto verify that mutating the builder duringReverse()enumeration throwsInvalidOperationException.