You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 25, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Feb 25, 2026. It is now read-only.
[Feature, HighPerformance] Public length and indexer properties for RefEnumerable<T> #4043
I'd like to implement a Reverse extension method for RefEnumerable<T> that mimics the one for Span<T>, but since the Length property on RefEnumerable<T> is non-public, and there's no indexer, this can get complicated.
Describe the solution
The following additional APIs will be available on RefEnumerable<T>:
public partial readonly ref struct RefEnumerable<T>
{
+ public int Length { get; }+ public ref T this[int index] { get; }
}
Though of course the same needs to be added to ReadOnlyRefEnumerable<T> as well:
public partial readonly ref struct ReadOnlyRefEnumerable<T>
{
+ public int Length { get; }+ public ref readonly T this[int index] { get; }
}
My rationale here is that adding these two makes sense to align the API surface with Span<T> and ReadOnlySpan<T> as well, which is what I did in #3353 anyway with all the various new APIs on these two types (eg. Clear(), Fill(T), etc.). My reservation with this kind of APIs was just that customers could now potentially shoot themselves in the foot with respect to performance. As in, this would essentially introduce two arguably similar looking ways to index a Span2D<T> element, ie:
// Okrefintitem=span[y,x];// Don't do thisrefintitem=span.GetRow(y)[x];
The latter would be much slower though as it'd perform redundant checks (in this case) and go through the entire enumerator just to get a single item. But then again, I'm not sure this is actually enough to reject these two APIs, given that:
The indexer pattern would still be more intuitive to use, so I'd expect most folks to just keep using that.
It's not like the HighPerformance package doesn't have plenty of ways to shoot yourself in the foot anyway. The whole package is written with the assumption that devs using it would know what their doing, and have read the docs.
We can explicitly call this out in the docs if needed, in a small note box or something, just for good measure
These two APIs would in fact enable some new scenarios that aren't possible today, like @DaZombieKiller mentioned
So yeah, all this to say the (adjusted) proposal looks good to me! 🚀
@DaZombieKiller I'll assign you then if you want to take care of implementing this 😄
@Sergio0694 What are your thoughts on exposing Step publicly too? It'd allow an implementation of Reverse that avoids bounds checking like the Span<T> implementation does:
public partial readonly ref struct RefEnumerable<T>
{
+ public int Step { get; }+ public int Length { get; }+ public ref T this[int index] { get; }
}
public partial readonly ref struct ReadOnlyRefEnumerable<T>
{
+ public int Step { get; }+ public int Length { get; }+ public ref readonly T this[int index] { get; }
}
Span2D<T> doesn't expose its Stride value, so I left Step out of the OP's proposal, but since RefEnumerable<T> is a bit lower level it might make more sense here.
Activity
michael-hawker commentedon May 25, 2021
Thanks for the proposal @DaZombieKiller, sounds like you want to implement it too? @Sergio0694 any feedback on the proposal?
DaZombieKiller commentedon May 26, 2021
Yes, I would be more than happy to handle the implementation for this.
Sergio0694 commentedon May 26, 2021
The proposed API surface makes sense to me:
public partial readonly ref struct RefEnumerable<T> { + public int Length { get; } + public ref T this[int index] { get; } }Though of course the same needs to be added to
ReadOnlyRefEnumerable<T>as well:public partial readonly ref struct ReadOnlyRefEnumerable<T> { + public int Length { get; } + public ref readonly T this[int index] { get; } }My rationale here is that adding these two makes sense to align the API surface with
Span<T>andReadOnlySpan<T>as well, which is what I did in #3353 anyway with all the various new APIs on these two types (eg.Clear(),Fill(T), etc.). My reservation with this kind of APIs was just that customers could now potentially shoot themselves in the foot with respect to performance. As in, this would essentially introduce two arguably similar looking ways to index aSpan2D<T>element, ie:The latter would be much slower though as it'd perform redundant checks (in this case) and go through the entire enumerator just to get a single item. But then again, I'm not sure this is actually enough to reject these two APIs, given that:
HighPerformancepackage doesn't have plenty of ways to shoot yourself in the foot anyway. The whole package is written with the assumption that devs using it would know what their doing, and have read the docs.So yeah, all this to say the (adjusted) proposal looks good to me! 🚀
@DaZombieKiller I'll assign you then if you want to take care of implementing this 😄
DaZombieKiller commentedon May 26, 2021
@Sergio0694 What are your thoughts on exposing
Steppublicly too? It'd allow an implementation ofReversethat avoids bounds checking like theSpan<T>implementation does:This would change the API surface to:
public partial readonly ref struct RefEnumerable<T> { + public int Step { get; } + public int Length { get; } + public ref T this[int index] { get; } } public partial readonly ref struct ReadOnlyRefEnumerable<T> { + public int Step { get; } + public int Length { get; } + public ref readonly T this[int index] { get; } }Span2D<T>doesn't expose itsStridevalue, so I leftStepout of the OP's proposal, but sinceRefEnumerable<T>is a bit lower level it might make more sense here.Expose public indexer and length properties on RefEnumerable<T> and R…
michael-hawker commentedon Jul 7, 2021
Thanks @DaZombieKiller for adding this feature! 🎉🎉🎉
DaZombieKiller commentedon Jul 7, 2021
No problem! Happy to have contributed