Skip to content

Commit 3e2cdbb

Browse files
committed
Codegen tweaks in [ReadOnly]Span2D<T> types
1 parent 9be7008 commit 3e2cdbb

File tree

2 files changed

+54
-28
lines changed

2 files changed

+54
-28
lines changed

Microsoft.Toolkit.HighPerformance/Memory/ReadOnlySpan2D{T}.cs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -628,15 +628,18 @@ public void CopyTo(Span<T> destination)
628628
nint width = (nint)(uint)this.width;
629629

630630
ref T destinationRef = ref MemoryMarshal.GetReference(destination);
631-
nint offset = 0;
632631

633632
for (int i = 0; i < height; i++)
634633
{
635-
ref T sourceRef = ref DangerousGetReferenceAt(i, 0);
634+
ref T sourceStart = ref DangerousGetReferenceAt(i, 0);
635+
ref T sourceEnd = ref Unsafe.Add(ref sourceStart, width);
636636

637-
for (nint j = 0; j < width; j += 1, offset += 1)
637+
while (Unsafe.IsAddressLessThan(ref sourceStart, ref sourceEnd))
638638
{
639-
Unsafe.Add(ref destinationRef, offset) = Unsafe.Add(ref sourceRef, j);
639+
destinationRef = sourceStart;
640+
641+
sourceStart = ref Unsafe.Add(ref sourceStart, 1);
642+
destinationRef = ref Unsafe.Add(ref destinationRef, 1);
640643
}
641644
}
642645
#endif
@@ -682,12 +685,16 @@ public void CopyTo(Span2D<T> destination)
682685

683686
for (int i = 0; i < height; i++)
684687
{
685-
ref T sourceRef = ref DangerousGetReferenceAt(i, 0);
688+
ref T sourceStart = ref DangerousGetReferenceAt(i, 0);
689+
ref T sourceEnd = ref Unsafe.Add(ref sourceStart, width);
686690
ref T destinationRef = ref destination.DangerousGetReferenceAt(i, 0);
687691

688-
for (nint j = 0; j < width; j += 1)
692+
while (Unsafe.IsAddressLessThan(ref sourceStart, ref sourceEnd))
689693
{
690-
Unsafe.Add(ref destinationRef, j) = Unsafe.Add(ref sourceRef, j);
694+
destinationRef = sourceStart;
695+
696+
sourceStart = ref Unsafe.Add(ref sourceStart, 1);
697+
destinationRef = ref Unsafe.Add(ref destinationRef, 1);
691698
}
692699
}
693700
#endif
@@ -928,15 +935,18 @@ public bool TryGetSpan(out ReadOnlySpan<T> span)
928935
nint width = (nint)(uint)this.width;
929936

930937
ref T destinationRef = ref array.DangerousGetReference();
931-
nint offset = 0;
932938

933939
for (int i = 0; i < height; i++)
934940
{
935-
ref T sourceRef = ref DangerousGetReferenceAt(i, 0);
941+
ref T sourceStart = ref DangerousGetReferenceAt(i, 0);
942+
ref T sourceEnd = ref Unsafe.Add(ref sourceStart, width);
936943

937-
for (nint j = 0; j < width; j += 1, offset += 1)
944+
while (Unsafe.IsAddressLessThan(ref sourceStart, ref sourceEnd))
938945
{
939-
Unsafe.Add(ref destinationRef, offset) = Unsafe.Add(ref sourceRef, j);
946+
destinationRef = sourceStart;
947+
948+
sourceStart = ref Unsafe.Add(ref sourceStart, 1);
949+
destinationRef = ref Unsafe.Add(ref destinationRef, 1);
940950
}
941951
}
942952
}

Microsoft.Toolkit.HighPerformance/Memory/Span2D{T}.cs

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -691,11 +691,14 @@ public void Clear()
691691

692692
for (int i = 0; i < height; i++)
693693
{
694-
ref T r0 = ref DangerousGetReferenceAt(i, 0);
694+
ref T rStart = ref DangerousGetReferenceAt(i, 0);
695+
ref T rEnd = ref Unsafe.Add(ref rStart, width);
695696

696-
for (nint j = 0; j < width; j += 1)
697+
while (Unsafe.IsAddressLessThan(ref rStart, ref rEnd))
697698
{
698-
Unsafe.Add(ref r0, j) = default!;
699+
rStart = default!;
700+
701+
rStart = ref Unsafe.Add(ref rStart, 1);
699702
}
700703
}
701704
#endif
@@ -738,15 +741,18 @@ public void CopyTo(Span<T> destination)
738741
nint width = (nint)(uint)this.width;
739742

740743
ref T destinationRef = ref MemoryMarshal.GetReference(destination);
741-
nint offset = 0;
742744

743745
for (int i = 0; i < height; i++)
744746
{
745-
ref T sourceRef = ref DangerousGetReferenceAt(i, 0);
747+
ref T sourceStart = ref DangerousGetReferenceAt(i, 0);
748+
ref T sourceEnd = ref Unsafe.Add(ref sourceStart, width);
746749

747-
for (nint j = 0; j < width; j += 1, offset += 1)
750+
while (Unsafe.IsAddressLessThan(ref sourceStart, ref sourceEnd))
748751
{
749-
Unsafe.Add(ref destinationRef, offset) = Unsafe.Add(ref sourceRef, j);
752+
destinationRef = sourceStart;
753+
754+
sourceStart = ref Unsafe.Add(ref sourceStart, 1);
755+
destinationRef = ref Unsafe.Add(ref destinationRef, 1);
750756
}
751757
}
752758
#endif
@@ -792,12 +798,16 @@ public void CopyTo(Span2D<T> destination)
792798

793799
for (int i = 0; i < height; i++)
794800
{
795-
ref T sourceRef = ref DangerousGetReferenceAt(i, 0);
801+
ref T sourceStart = ref DangerousGetReferenceAt(i, 0);
802+
ref T sourceEnd = ref Unsafe.Add(ref sourceStart, width);
796803
ref T destinationRef = ref destination.DangerousGetReferenceAt(i, 0);
797804

798-
for (nint j = 0; j < width; j += 1)
805+
while (Unsafe.IsAddressLessThan(ref sourceStart, ref sourceEnd))
799806
{
800-
Unsafe.Add(ref destinationRef, j) = Unsafe.Add(ref sourceRef, j);
807+
destinationRef = sourceStart;
808+
809+
sourceStart = ref Unsafe.Add(ref sourceStart, 1);
810+
destinationRef = ref Unsafe.Add(ref destinationRef, 1);
801811
}
802812
}
803813
#endif
@@ -868,11 +878,14 @@ public void Fill(T value)
868878

869879
for (int i = 0; i < height; i++)
870880
{
871-
ref T r0 = ref DangerousGetReferenceAt(i, 0);
881+
ref T rStart = ref DangerousGetReferenceAt(i, 0);
882+
ref T rEnd = ref Unsafe.Add(ref rStart, width);
872883

873-
for (nint j = 0; j < width; j += 1)
884+
while (Unsafe.IsAddressLessThan(ref rStart, ref rEnd))
874885
{
875-
Unsafe.Add(ref r0, j) = value;
886+
rStart = value;
887+
888+
rStart = ref Unsafe.Add(ref rStart, 1);
876889
}
877890
}
878891
#endif
@@ -1078,15 +1091,18 @@ public bool TryGetSpan(out Span<T> span)
10781091
nint width = (nint)(uint)this.width;
10791092

10801093
ref T destinationRef = ref array.DangerousGetReference();
1081-
nint offset = 0;
10821094

10831095
for (int i = 0; i < height; i++)
10841096
{
1085-
ref T sourceRef = ref DangerousGetReferenceAt(i, 0);
1097+
ref T sourceStart = ref DangerousGetReferenceAt(i, 0);
1098+
ref T sourceEnd = ref Unsafe.Add(ref sourceStart, width);
10861099

1087-
for (nint j = 0; j < width; j += 1, offset += 1)
1100+
while (Unsafe.IsAddressLessThan(ref sourceStart, ref sourceEnd))
10881101
{
1089-
Unsafe.Add(ref destinationRef, offset) = Unsafe.Add(ref sourceRef, j);
1102+
destinationRef = sourceStart;
1103+
1104+
sourceStart = ref Unsafe.Add(ref sourceStart, 1);
1105+
destinationRef = ref Unsafe.Add(ref destinationRef, 1);
10901106
}
10911107
}
10921108
}

0 commit comments

Comments
 (0)