-
Notifications
You must be signed in to change notification settings - Fork 5k
Implement IEnumerator<T> on ref struct enumerators #106309
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
base: main
Are you sure you want to change the base?
Conversation
Note regarding the
|
1 similar comment
Note regarding the
|
src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Text/SpanLineEnumerator.cs
Outdated
Show resolved
Hide resolved
@@ -222,7 +222,7 @@ public static void ValidateStrides(ReadOnlySpan<nint> strides, ReadOnlySpan<nint | |||
/// <param name="length">The length of the TensorSpan we are iterating over.</param> | |||
public static void AdjustIndexes(int curIndex, nint addend, Span<nint> curIndexes, scoped ReadOnlySpan<nint> length) | |||
{ | |||
if (addend <= 0 || curIndex < 0) | |||
if (addend <= 0 || curIndex < 0 || length[curIndex] <= 0) |
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.
What's hitting this? Will this affect behavior of other public API?
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 removes Access violation when length[curIndex] is zero. Now code throws an Access violation for an empty tensor.
That was incorrect behavior. I add tests for empty tensor.
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.
src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/ReadOnlyTensorSpan.cs
Show resolved
Hide resolved
.../System.Text.RegularExpressions/src/System/Text/RegularExpressions/Regex.EnumerateMatches.cs
Outdated
Show resolved
Hide resolved
Calling MoveNext after it's already returned false is a programmer error. It is not something we need to optimize for.
…untime into RefStructEnumerators
Removed redundant assertions for match properties after calling Assert.False(enumerator.MoveNext()).
Updated test methods in ReadOnlyTensorSpanTests.cs to replace instances of TensorSpan with ReadOnlyTensorSpan. This ensures tests validate the behavior of ReadOnlyTensorSpan, including exception handling and constructor functionality. Adjustments made for handling empty and multi-dimensional spans appropriately.
- Updated `Slice` method in `ReadOnlyTensorSpan<T>` to use `ThrowHelper.ThrowIndexOutOfRangeException()` for consistent error handling. - Removed unnecessary `using System.Reflection;` directive from `TensorSpanTests.cs`. - Adjusted `TensorSpan_SliceTest` to call `TensorSpan_TestEnumerator(spanInt);` after defining `spanInt` and updated exception checks. - Introduced a `value` variable in `TensorSpan_TestEnumerator<T>` to accurately restore the enumerator's current value for improved assertions.
static void TestGI<TEnumerator>(TEnumerator enumerator) where TEnumerator : IEnumerator<int>, allows ref struct | ||
{ | ||
Assert.False(enumerator.MoveNext()); | ||
try { _ = enumerator.Current; } catch (Exception) { } |
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.
What are all of these try/catches trying to validate? Is it trying to test that an exception gets thrown? If so, there should be asserts validating that.
/// <summary>Gets the element at the current position of the enumerator.</summary> | ||
T IEnumerator<T>.Current | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
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.
These shouldn't need AggressiveInlining on them
@@ -484,7 +486,8 @@ internal Enumerator(ReadOnlyTensorSpan<T> span) | |||
_span = span; | |||
_items = -1; | |||
_curIndexes = new nint[_span.Rank]; | |||
_curIndexes[_span.Rank - 1] = -1; | |||
if (_span.Rank > 0) | |||
_curIndexes[_span.Rank - 1] = -1; |
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.
_curIndexes[_span.Rank - 1] = -1; | |
{ | |
_curIndexes[_span.Rank - 1] = -1; | |
} |
_items = -1; | ||
_curIndexes.Clear(); | ||
if (_span.Rank > 0) | ||
_curIndexes[_span.Rank - 1] = -1; |
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.
_curIndexes[_span.Rank - 1] = -1; | |
{ | |
_curIndexes[_span.Rank - 1] = -1; | |
} |
/// <summary>Gets the element at the current position of the enumerator.</summary> | ||
T IEnumerator<T>.Current | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
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.
These also should not need AggressiveInlining.
Close #105276