Skip to content

Commit 054dd66

Browse files
Eliminate this = default shortcut when height * width = 0
1 parent 657c697 commit 054dd66

File tree

6 files changed

+47
-53
lines changed

6 files changed

+47
-53
lines changed

src/CommunityToolkit.HighPerformance/Memory/Internals/OverflowHelper.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ public static void EnsureIsInNativeIntRange(int height, int width, int pitch)
6464
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6565
public static int ComputeInt32Area(int height, int width, int pitch)
6666
{
67-
return checked(((width + pitch) * Max(unchecked(height - 1), 0)) + width);
67+
if (height <= 0 || width <= 0)
68+
return 0;
69+
70+
return checked(((width + pitch) * (height - 1)) + width);
6871
}
6972
}

src/CommunityToolkit.HighPerformance/Memory/Memory2D{T}.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -362,13 +362,6 @@ public unsafe Memory2D(MemoryManager<T> memoryManager, int offset, int height, i
362362
ThrowHelper.ThrowArgumentOutOfRangeExceptionForPitch();
363363
}
364364

365-
if (width == 0 || height == 0)
366-
{
367-
this = default;
368-
369-
return;
370-
}
371-
372365
int area = OverflowHelper.ComputeInt32Area(height, width, pitch);
373366
int remaining = length - offset;
374367

@@ -435,16 +428,8 @@ internal unsafe Memory2D(Memory<T> memory, int offset, int height, int width, in
435428
ThrowHelper.ThrowArgumentOutOfRangeExceptionForPitch();
436429
}
437430

438-
if (width == 0 || height == 0)
439-
{
440-
this = default;
441-
442-
return;
443-
}
444-
445-
int
446-
area = OverflowHelper.ComputeInt32Area(height, width, pitch),
447-
remaining = memory.Length - offset;
431+
int area = OverflowHelper.ComputeInt32Area(height, width, pitch);
432+
int remaining = memory.Length - offset;
448433

449434
if (area > remaining)
450435
{

src/CommunityToolkit.HighPerformance/Memory/ReadOnlyMemory2D{T}.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -382,13 +382,6 @@ public unsafe ReadOnlyMemory2D(MemoryManager<T> memoryManager, int offset, int h
382382
ThrowHelper.ThrowArgumentOutOfRangeExceptionForPitch();
383383
}
384384

385-
if (width == 0 || height == 0)
386-
{
387-
this = default;
388-
389-
return;
390-
}
391-
392385
int area = OverflowHelper.ComputeInt32Area(height, width, pitch);
393386
int remaining = length - offset;
394387

@@ -455,13 +448,6 @@ internal unsafe ReadOnlyMemory2D(ReadOnlyMemory<T> memory, int offset, int heigh
455448
ThrowHelper.ThrowArgumentOutOfRangeExceptionForPitch();
456449
}
457450

458-
if (width == 0 || height == 0)
459-
{
460-
this = default;
461-
462-
return;
463-
}
464-
465451
int area = OverflowHelper.ComputeInt32Area(height, width, pitch);
466452
int remaining = memory.Length - offset;
467453

src/CommunityToolkit.HighPerformance/Memory/ReadOnlySpan2D{T}.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,6 @@ public ReadOnlySpan2D(T[] array, int offset, int height, int width, int pitch)
209209
ThrowHelper.ThrowArgumentOutOfRangeExceptionForPitch();
210210
}
211211

212-
if (width == 0 || height == 0)
213-
{
214-
this = default;
215-
216-
return;
217-
}
218-
219212
int area = OverflowHelper.ComputeInt32Area(height, width, pitch);
220213
int remaining = array.Length - offset;
221214

@@ -459,13 +452,6 @@ internal ReadOnlySpan2D(ReadOnlySpan<T> span, int offset, int height, int width,
459452
ThrowHelper.ThrowArgumentOutOfRangeExceptionForPitch();
460453
}
461454

462-
if (width == 0 || height == 0)
463-
{
464-
this = default;
465-
466-
return;
467-
}
468-
469455
int area = OverflowHelper.ComputeInt32Area(height, width, pitch);
470456
int remaining = span.Length - offset;
471457

src/CommunityToolkit.HighPerformance/Memory/Span2D{T}.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -531,13 +531,6 @@ internal Span2D(Span<T> span, int offset, int height, int width, int pitch)
531531
ThrowHelper.ThrowArgumentOutOfRangeExceptionForPitch();
532532
}
533533

534-
if (width == 0 || height == 0)
535-
{
536-
this = default;
537-
538-
return;
539-
}
540-
541534
int area = OverflowHelper.ComputeInt32Area(height, width, pitch);
542535
int remaining = span.Length - offset;
543536

tests/CommunityToolkit.HighPerformance.UnitTests/Extensions/Test_SpanExtensions.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,45 @@ public void Test_SpanExtensions_CopyTo_RefEnumerable()
199199

200200
CollectionAssert.AreEqual(array, result);
201201
}
202+
203+
#if NET6_0_OR_GREATER
204+
[TestMethod]
205+
public void Test_SpanExtensions_AsSpan2d_Empty()
206+
{
207+
Span2D<int> empty2 = Span<int>.Empty.AsSpan2D(0, 0);
208+
Assert.IsTrue(empty2.IsEmpty);
209+
Assert.AreEqual(empty2.Length, 0);
210+
Assert.AreEqual(empty2.Width, 0);
211+
Assert.AreEqual(empty2.Height, 0);
212+
213+
Span2D<int> empty3 = Span<int>.Empty.AsSpan2D(4, 0);
214+
Assert.IsTrue(empty3.IsEmpty);
215+
Assert.AreEqual(empty3.Length, 0);
216+
Assert.AreEqual(empty3.Width, 0);
217+
Assert.AreEqual(empty3.Height, 4);
218+
219+
Span2D<int> empty4 = Span<int>.Empty.AsSpan2D(0, 7);
220+
Assert.IsTrue(empty4.IsEmpty);
221+
Assert.AreEqual(empty4.Length, 0);
222+
Assert.AreEqual(empty4.Width, 7);
223+
Assert.AreEqual(empty4.Height, 0);
224+
}
225+
226+
[TestMethod]
227+
public void Test_SpanExtensions_AsSpan2d_Copy()
228+
{
229+
int[,] array = new int[2, 3];
230+
int[] values1 = { 1, 2, 3, 4, 5, 6 };
231+
// Copy a span to a target row and column with valid lengths
232+
values1.AsSpan().AsSpan2D(2, 3).CopyTo(array);
233+
int[,] result = new[,]
234+
{
235+
{ 1, 2, 3 },
236+
{ 4, 5, 6 },
237+
};
238+
239+
CollectionAssert.AreEqual(array, result);
240+
_ = Assert.ThrowsException<ArgumentException>(() => values1.AsSpan().AsSpan2D(3, 2).CopyTo(array));
241+
}
242+
#endif
202243
}

0 commit comments

Comments
 (0)