Skip to content

Commit ffc95e6

Browse files
committed
Minor code tweaks
1 parent 7ab524d commit ffc95e6

File tree

1 file changed

+53
-51
lines changed

1 file changed

+53
-51
lines changed

Microsoft.Toolkit.HighPerformance/Enumerables/RefEnumerable{T}.cs

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,29 @@ public ref struct RefEnumerable<T>
2929
/// The <see cref="Span{T}"/> instance pointing to the first item in the target memory area.
3030
/// </summary>
3131
/// <remarks>The <see cref="Span{T}.Length"/> field maps to the total available length.</remarks>
32-
private readonly Span<T> span;
32+
internal readonly Span<T> Span;
3333
#else
3434
/// <summary>
3535
/// The target <see cref="object"/> instance, if present.
3636
/// </summary>
37-
private readonly object? instance;
37+
internal readonly object? Instance;
3838

3939
/// <summary>
40-
/// The initial offset within <see cref="instance"/>.
40+
/// The initial offset within <see cref="Instance"/>.
4141
/// </summary>
42-
private readonly IntPtr offset;
42+
internal readonly IntPtr Offset;
4343

4444
/// <summary>
4545
/// The total available length for the sequence.
4646
/// </summary>
47-
private readonly int length;
47+
internal readonly int Length;
4848
#endif
4949

5050
/// <summary>
5151
/// The distance between items in the sequence to enumerate.
5252
/// </summary>
5353
/// <remarks>The distance refers to <typeparamref name="T"/> items, not byte offset.</remarks>
54-
private readonly int step;
54+
internal readonly int Step;
5555

5656
/// <summary>
5757
/// The current position in the sequence.
@@ -68,8 +68,9 @@ public ref struct RefEnumerable<T>
6868
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6969
internal RefEnumerable(ref T reference, int length, int step)
7070
{
71-
this.span = MemoryMarshal.CreateSpan(ref reference, length);
72-
this.step = step;
71+
Span = MemoryMarshal.CreateSpan(ref reference, length);
72+
Step = step;
73+
7374
this.position = -1;
7475
}
7576
#else
@@ -83,10 +84,11 @@ internal RefEnumerable(ref T reference, int length, int step)
8384
[MethodImpl(MethodImplOptions.AggressiveInlining)]
8485
internal RefEnumerable(object? instance, IntPtr offset, int length, int step)
8586
{
86-
this.instance = instance;
87-
this.offset = offset;
88-
this.length = length;
89-
this.step = step;
87+
Instance = instance;
88+
Offset = offset;
89+
Length = length;
90+
Step = step;
91+
9092
this.position = -1;
9193
}
9294
#endif
@@ -101,9 +103,9 @@ internal RefEnumerable(object? instance, IntPtr offset, int length, int step)
101103
public bool MoveNext()
102104
{
103105
#if SPAN_RUNTIME_SUPPORT
104-
return ++this.position < this.span.Length;
106+
return ++this.position < this.Span.Length;
105107
#else
106-
return ++this.position < this.length;
108+
return ++this.position < this.Length;
107109
#endif
108110
}
109111

@@ -114,9 +116,9 @@ public readonly ref T Current
114116
get
115117
{
116118
#if SPAN_RUNTIME_SUPPORT
117-
ref T r0 = ref this.span.DangerousGetReference();
119+
ref T r0 = ref this.Span.DangerousGetReference();
118120
#else
119-
ref T r0 = ref RuntimeHelpers.GetObjectDataAtOffsetOrPointerReference<T>(this.instance, this.offset);
121+
ref T r0 = ref RuntimeHelpers.GetObjectDataAtOffsetOrPointerReference<T>(this.Instance, this.Offset);
120122
#endif
121123

122124
// Here we just offset by shifting down as if we were traversing a 2D array with a
@@ -125,7 +127,7 @@ public readonly ref T Current
125127
// being inspected. We can perform all the indexing operations in this type as nint,
126128
// as the maximum offset is guaranteed never to exceed the maximum value, since on
127129
// 32 bit architectures it's not possible to allocate that much memory anyway.
128-
nint offset = (nint)(uint)this.position * (nint)(uint)this.step;
130+
nint offset = (nint)(uint)this.position * (nint)(uint)this.Step;
129131
ref T ri = ref Unsafe.Add(ref r0, offset);
130132

131133
return ref ri;
@@ -139,21 +141,21 @@ public readonly void Clear()
139141
{
140142
#if SPAN_RUNTIME_SUPPORT
141143
// Fast path for contiguous items
142-
if (this.step == 1)
144+
if (this.Step == 1)
143145
{
144-
this.span.Clear();
146+
this.Span.Clear();
145147

146148
return;
147149
}
148150

149-
ref T r0 = ref this.span.DangerousGetReference();
150-
int length = this.span.Length;
151+
ref T r0 = ref this.Span.DangerousGetReference();
152+
int length = this.Span.Length;
151153
#else
152-
ref T r0 = ref RuntimeHelpers.GetObjectDataAtOffsetOrPointerReference<T>(this.instance, this.offset);
153-
int length = this.length;
154+
ref T r0 = ref RuntimeHelpers.GetObjectDataAtOffsetOrPointerReference<T>(this.Instance, this.Offset);
155+
int length = this.Length;
154156
#endif
155157

156-
RefEnumerableHelper.Clear(ref r0, (nint)(uint)length, (nint)(uint)this.step);
158+
RefEnumerableHelper.Clear(ref r0, (nint)(uint)length, (nint)(uint)this.Step);
157159
}
158160

159161
/// <summary>
@@ -166,18 +168,18 @@ public readonly void Clear()
166168
public readonly void CopyTo(Span<T> destination)
167169
{
168170
#if SPAN_RUNTIME_SUPPORT
169-
if (this.step == 1)
171+
if (this.Step == 1)
170172
{
171-
this.span.CopyTo(destination);
173+
this.Span.CopyTo(destination);
172174

173175
return;
174176
}
175177

176-
ref T sourceRef = ref this.span.DangerousGetReference();
177-
int length = this.span.Length;
178+
ref T sourceRef = ref this.Span.DangerousGetReference();
179+
int length = this.Span.Length;
178180
#else
179-
ref T sourceRef = ref RuntimeHelpers.GetObjectDataAtOffsetOrPointerReference<T>(this.instance, this.offset);
180-
int length = this.length;
181+
ref T sourceRef = ref RuntimeHelpers.GetObjectDataAtOffsetOrPointerReference<T>(this.Instance, this.Offset);
182+
int length = this.Length;
181183
#endif
182184
if ((uint)destination.Length < (uint)length)
183185
{
@@ -186,7 +188,7 @@ public readonly void CopyTo(Span<T> destination)
186188

187189
ref T destinationRef = ref destination.DangerousGetReference();
188190

189-
RefEnumerableHelper.CopyTo(ref sourceRef, ref destinationRef, (nint)(uint)length, (nint)(uint)this.step);
191+
RefEnumerableHelper.CopyTo(ref sourceRef, ref destinationRef, (nint)(uint)length, (nint)(uint)this.Step);
190192
}
191193

192194
/// <summary>
@@ -197,9 +199,9 @@ public readonly void CopyTo(Span<T> destination)
197199
public readonly bool TryCopyTo(Span<T> destination)
198200
{
199201
#if SPAN_RUNTIME_SUPPORT
200-
int length = this.span.Length;
202+
int length = this.Span.Length;
201203
#else
202-
int length = this.length;
204+
int length = this.Length;
203205
#endif
204206

205207
if (destination.Length >= length)
@@ -222,18 +224,18 @@ public readonly bool TryCopyTo(Span<T> destination)
222224
internal readonly void CopyFrom(ReadOnlySpan<T> source)
223225
{
224226
#if SPAN_RUNTIME_SUPPORT
225-
if (this.step == 1)
227+
if (this.Step == 1)
226228
{
227-
source.CopyTo(this.span);
229+
source.CopyTo(this.Span);
228230

229231
return;
230232
}
231233

232-
ref T destinationRef = ref this.span.DangerousGetReference();
233-
int destinationLength = this.span.Length;
234+
ref T destinationRef = ref this.Span.DangerousGetReference();
235+
int destinationLength = this.Span.Length;
234236
#else
235-
ref T destinationRef = ref RuntimeHelpers.GetObjectDataAtOffsetOrPointerReference<T>(this.instance, this.offset);
236-
int destinationLength = this.length;
237+
ref T destinationRef = ref RuntimeHelpers.GetObjectDataAtOffsetOrPointerReference<T>(this.Instance, this.Offset);
238+
int destinationLength = this.Length;
237239
#endif
238240
ref T sourceRef = ref source.DangerousGetReference();
239241
int sourceLength = source.Length;
@@ -243,7 +245,7 @@ internal readonly void CopyFrom(ReadOnlySpan<T> source)
243245
ThrowArgumentExceptionForDestinationTooShort();
244246
}
245247

246-
RefEnumerableHelper.CopyFrom(ref sourceRef, ref destinationRef, (nint)(uint)sourceLength, (nint)(uint)this.step);
248+
RefEnumerableHelper.CopyFrom(ref sourceRef, ref destinationRef, (nint)(uint)sourceLength, (nint)(uint)this.Step);
247249
}
248250

249251
/// <summary>
@@ -254,9 +256,9 @@ internal readonly void CopyFrom(ReadOnlySpan<T> source)
254256
public readonly bool TryCopyFrom(ReadOnlySpan<T> source)
255257
{
256258
#if SPAN_RUNTIME_SUPPORT
257-
int length = this.span.Length;
259+
int length = this.Span.Length;
258260
#else
259-
int length = this.length;
261+
int length = this.Length;
260262
#endif
261263

262264
if (length >= source.Length)
@@ -280,21 +282,21 @@ public readonly bool TryCopyFrom(ReadOnlySpan<T> source)
280282
public readonly void Fill(T value)
281283
{
282284
#if SPAN_RUNTIME_SUPPORT
283-
if (this.step == 1)
285+
if (this.Step == 1)
284286
{
285-
this.span.Fill(value);
287+
this.Span.Fill(value);
286288

287289
return;
288290
}
289291

290-
ref T r0 = ref this.span.DangerousGetReference();
291-
int length = this.span.Length;
292+
ref T r0 = ref this.Span.DangerousGetReference();
293+
int length = this.Span.Length;
292294
#else
293-
ref T r0 = ref RuntimeHelpers.GetObjectDataAtOffsetOrPointerReference<T>(this.instance, this.offset);
294-
int length = this.length;
295+
ref T r0 = ref RuntimeHelpers.GetObjectDataAtOffsetOrPointerReference<T>(this.Instance, this.Offset);
296+
int length = this.Length;
295297
#endif
296298

297-
RefEnumerableHelper.Fill(ref r0, (nint)(uint)length, (nint)(uint)this.step, value);
299+
RefEnumerableHelper.Fill(ref r0, (nint)(uint)length, (nint)(uint)this.Step, value);
298300
}
299301

300302
/// <summary>
@@ -311,9 +313,9 @@ public readonly void Fill(T value)
311313
public readonly T[] ToArray()
312314
{
313315
#if SPAN_RUNTIME_SUPPORT
314-
int length = this.span.Length;
316+
int length = this.Span.Length;
315317
#else
316-
int length = this.length;
318+
int length = this.Length;
317319
#endif
318320

319321
// Empty array if no data is mapped

0 commit comments

Comments
 (0)