Skip to content

Commit 434f482

Browse files
committed
Use ref fields in [ReadOnly]SpanEnumerable<T>-s
1 parent 83dd209 commit 434f482

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

src/CommunityToolkit.HighPerformance/Enumerables/ReadOnlySpanEnumerable{T}.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,22 @@ public readonly Item Current
8181
[EditorBrowsable(EditorBrowsableState.Never)]
8282
public readonly ref struct Item
8383
{
84+
#if NET7_0_OR_GREATER
85+
/// <summary>
86+
/// The <typeparamref name="T"/> reference for the <see cref="Item"/> instance.
87+
/// </summary>
88+
private readonly ref readonly T reference;
89+
90+
/// <summary>
91+
/// The index of the current <see cref="Item"/> instance.
92+
/// </summary>
93+
private readonly int index;
94+
#else
8495
/// <summary>
8596
/// The source <see cref="ReadOnlySpan{T}"/> instance.
8697
/// </summary>
8798
private readonly ReadOnlySpan<T> span;
99+
#endif
88100

89101
#if NETSTANDARD2_1_OR_GREATER
90102
/// <summary>
@@ -95,7 +107,12 @@ public readonly ref struct Item
95107
[MethodImpl(MethodImplOptions.AggressiveInlining)]
96108
public Item(ref T value, int index)
97109
{
110+
#if NET7_0_OR_GREATER
111+
this.reference = ref value;
112+
this.index = index;
113+
#else
98114
this.span = MemoryMarshal.CreateReadOnlySpan(ref value, index);
115+
#endif
99116
}
100117
#else
101118
/// <summary>
@@ -124,7 +141,9 @@ public ref readonly T Value
124141
[MethodImpl(MethodImplOptions.AggressiveInlining)]
125142
get
126143
{
127-
#if NETSTANDARD2_1_OR_GREATER
144+
#if NET7_0_OR_GREATER
145+
return ref this.reference;
146+
#elif NETSTANDARD2_1_OR_GREATER
128147
return ref MemoryMarshal.GetReference(this.span);
129148
#else
130149
ref T r0 = ref MemoryMarshal.GetReference(this.span);
@@ -143,7 +162,9 @@ public int Index
143162
[MethodImpl(MethodImplOptions.AggressiveInlining)]
144163
get
145164
{
146-
#if NETSTANDARD2_1_OR_GREATER
165+
#if NET7_0_OR_GREATER
166+
return this.index;
167+
#elif NETSTANDARD2_1_OR_GREATER
147168
return this.span.Length;
148169
#else
149170
return this.index;

src/CommunityToolkit.HighPerformance/Enumerables/SpanEnumerable{T}.cs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,22 @@ public readonly Item Current
8686
[EditorBrowsable(EditorBrowsableState.Never)]
8787
public readonly ref struct Item
8888
{
89+
#if NET7_0_OR_GREATER
90+
/// <summary>
91+
/// The <typeparamref name="T"/> reference for the <see cref="Item"/> instance.
92+
/// </summary>
93+
private readonly ref T reference;
94+
95+
/// <summary>
96+
/// The index of the current <see cref="Item"/> instance.
97+
/// </summary>
98+
private readonly int index;
99+
#else
89100
/// <summary>
90101
/// The source <see cref="Span{T}"/> instance.
91102
/// </summary>
92103
private readonly Span<T> span;
104+
#endif
93105

94106
#if NETSTANDARD2_1_OR_GREATER
95107
/// <summary>
@@ -100,7 +112,12 @@ public readonly ref struct Item
100112
[MethodImpl(MethodImplOptions.AggressiveInlining)]
101113
public Item(ref T value, int index)
102114
{
115+
#if NET7_0_OR_GREATER
116+
this.reference = ref value;
117+
this.index = index;
118+
#else
103119
this.span = MemoryMarshal.CreateSpan(ref value, index);
120+
#endif
104121
}
105122
#else
106123
/// <summary>
@@ -121,15 +138,17 @@ public Item(Span<T> span, int index)
121138
}
122139
#endif
123140

124-
/// <summary>
125-
/// Gets the reference to the current value.
126-
/// </summary>
141+
/// <summary>
142+
/// Gets the reference to the current value.
143+
/// </summary>
127144
public ref T Value
128145
{
129146
[MethodImpl(MethodImplOptions.AggressiveInlining)]
130147
get
131148
{
132-
#if NETSTANDARD2_1_OR_GREATER
149+
#if NET7_0_OR_GREATER
150+
return ref this.reference;
151+
#elif NETSTANDARD2_1_OR_GREATER
133152
return ref MemoryMarshal.GetReference(this.span);
134153
#else
135154
ref T r0 = ref MemoryMarshal.GetReference(this.span);
@@ -148,7 +167,9 @@ public int Index
148167
[MethodImpl(MethodImplOptions.AggressiveInlining)]
149168
get
150169
{
151-
#if NETSTANDARD2_1_OR_GREATER
170+
#if NET7_0_OR_GREATER
171+
return this.index;
172+
#elif NETSTANDARD2_1_OR_GREATER
152173
return this.span.Length;
153174
#else
154175
return this.index;

0 commit comments

Comments
 (0)