Skip to content

Commit 205eedf

Browse files
author
msftbot[bot]
authored
Loop codegen improvements (#3632)
## PR Type What kind of change does this PR introduce? <!-- Please uncomment one or more that apply to this PR. --> - Optimization <!-- - Bugfix --> <!-- - Feature --> <!-- - Code style update (formatting) --> <!-- - Refactoring (no functional changes, no api changes) --> <!-- - Build or CI related changes --> <!-- - Documentation content changes --> <!-- - Sample app changes --> <!-- - Other... Please describe: --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying, or link to a relevant issue. --> Some `for` loops have unoptimal codegen involving the indexing at each iteration. For instance, as a very simple test, this method simply sets all items in an input `Span<int>` to `0`: ```csharp public static void M1(Span<int> span) { ref int r0 = ref MemoryMarshal.GetReference(span); int length = span.Length; for (int i = 0; i < length; i++) { Unsafe.Add(ref r0, i) = 0; } } ``` ```asm C.M1(System.Span`1<Int32>) L0000: mov rax, [rcx] L0003: mov edx, [rcx+8] L0006: xor ecx, ecx L0008: test edx, edx L000a: jle short L001c L000c: movsxd r8, ecx L000f: xor r9d, r9d L0012: mov [rax+r8*4], r9d L0016: inc ecx L0018: cmp ecx, edx L001a: jl short L000c L001c: ret ``` Here the loop starts at `L000c`, and at every iteration it takes the loop counter, extends it to native int, then uses it to index from the initial reference (that `[rax+r8*4]` offset calculation), and then writes to it. This is unnecessary logic, in cases such as this. ## What is the new behavior? <!-- Describe how was this issue resolved or changed? --> Refactored some loops to operate within a target address range, with all indexing out of the loop body. ```csharp public static void M2(Span<int> span) { ref int r0 = ref MemoryMarshal.GetReference(span); ref int r1 = ref Unsafe.Add(ref r0, span.Length); while (Unsafe.IsAddressLessThan(ref r0, ref r1)) { r0 = 0; r0 = ref Unsafe.Add(ref r0, 1); } } ``` ```asm C.M2(System.Span`1<Int32>) L0000: mov rax, [rcx] L0003: mov edx, [rcx+8] L0006: movsxd rdx, edx L0009: lea rdx, [rax+rdx*4] L000d: cmp rax, rdx L0010: jae short L001f L0012: xor ecx, ecx L0014: mov [rax], ecx L0016: add rax, 4 L001a: cmp rax, rdx L001d: jb short L0012 L001f: ret ``` Here instead we pre-calculate the target address just once, outside the loop, and then just iterate until the initial, moving reference reaches that point. This allows the actual loop to be more compact and with no indexing logic needed. We just read directly from the moving reference, and then increment it by a fixed amount at the end of each iteration. Not groundbreaking, but better 🚀 ## PR Checklist Please check if your PR fulfills the following requirements: - [X] Tested code with current [supported SDKs](../readme.md#supported) - [ ] ~~Pull Request has been submitted to the documentation repository [instructions](..\contributing.md#docs). Link: <!-- docs PR link -->~~ - [ ] ~~Sample in sample app has been added / updated (for bug fixes / features)~~ - [ ] ~~Icon has been created (if new sample) following the [Thumbnail Style Guide and templates](https://github.com/windows-toolkit/WindowsCommunityToolkit-design-assets)~~ - [X] Tests for the changes have been added (for bug fixes / features) (if applicable) - [X] Header has been added to all new source files (run *build/UpdateHeaders.bat*) - [X] Contains **NO** breaking changes
2 parents c9d0a57 + 15d32ce commit 205eedf

File tree

6 files changed

+74
-42
lines changed

6 files changed

+74
-42
lines changed

Microsoft.Toolkit.HighPerformance/Helpers/ParallelHelper.ForEach.IInAction.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,14 @@ public void Invoke(int i)
143143
end = Math.Min(high, this.memory.Length);
144144

145145
ref TItem r0 = ref MemoryMarshal.GetReference(this.memory.Span);
146+
ref TItem rStart = ref Unsafe.Add(ref r0, low);
147+
ref TItem rEnd = ref Unsafe.Add(ref r0, end);
146148

147-
for (int j = low; j < end; j++)
149+
while (Unsafe.IsAddressLessThan(ref rStart, ref rEnd))
148150
{
149-
ref TItem rj = ref Unsafe.Add(ref r0, (nint)(uint)j);
151+
Unsafe.AsRef(this.action).Invoke(in rStart);
150152

151-
Unsafe.AsRef(this.action).Invoke(rj);
153+
rStart = ref Unsafe.Add(ref rStart, 1);
152154
}
153155
}
154156
}

Microsoft.Toolkit.HighPerformance/Helpers/ParallelHelper.ForEach.IInAction2D.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,14 @@ public void Invoke(int i)
145145

146146
for (int y = lowY; y < stopY; y++)
147147
{
148-
ref TItem r0 = ref span.DangerousGetReferenceAt(y, 0);
148+
ref TItem rStart = ref span.DangerousGetReferenceAt(y, 0);
149+
ref TItem rEnd = ref Unsafe.Add(ref rStart, width);
149150

150-
for (int x = 0; x < width; x++)
151+
while (Unsafe.IsAddressLessThan(ref rStart, ref rEnd))
151152
{
152-
ref TItem ryx = ref Unsafe.Add(ref r0, (nint)(uint)x);
153+
Unsafe.AsRef(this.action).Invoke(in rStart);
153154

154-
Unsafe.AsRef(this.action).Invoke(ryx);
155+
rStart = ref Unsafe.Add(ref rStart, 1);
155156
}
156157
}
157158
}

Microsoft.Toolkit.HighPerformance/Helpers/ParallelHelper.ForEach.IRefAction.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,14 @@ public void Invoke(int i)
143143
end = Math.Min(high, this.memory.Length);
144144

145145
ref TItem r0 = ref MemoryMarshal.GetReference(this.memory.Span);
146+
ref TItem rStart = ref Unsafe.Add(ref r0, low);
147+
ref TItem rEnd = ref Unsafe.Add(ref r0, end);
146148

147-
for (int j = low; j < end; j++)
149+
while (Unsafe.IsAddressLessThan(ref rStart, ref rEnd))
148150
{
149-
ref TItem rj = ref Unsafe.Add(ref r0, (nint)(uint)j);
151+
Unsafe.AsRef(this.action).Invoke(ref rStart);
150152

151-
Unsafe.AsRef(this.action).Invoke(ref rj);
153+
rStart = ref Unsafe.Add(ref rStart, 1);
152154
}
153155
}
154156
}

Microsoft.Toolkit.HighPerformance/Helpers/ParallelHelper.ForEach.IRefAction2D.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,14 @@ public void Invoke(int i)
152152

153153
for (int y = lowY; y < stopY; y++)
154154
{
155-
ref TItem r0 = ref span.DangerousGetReferenceAt(y, 0);
155+
ref TItem rStart = ref span.DangerousGetReferenceAt(y, 0);
156+
ref TItem rEnd = ref Unsafe.Add(ref rStart, width);
156157

157-
for (int x = 0; x < width; x++)
158+
while (Unsafe.IsAddressLessThan(ref rStart, ref rEnd))
158159
{
159-
ref TItem ryx = ref Unsafe.Add(ref r0, (nint)(uint)x);
160+
Unsafe.AsRef(this.action).Invoke(ref rStart);
160161

161-
Unsafe.AsRef(this.action).Invoke(ref ryx);
162+
rStart = ref Unsafe.Add(ref rStart, 1);
162163
}
163164
}
164165
}

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)