Skip to content

Commit 9be7008

Browse files
committed
Codegen tweaks to ParallelHelper
1 parent a20e824 commit 9be7008

File tree

4 files changed

+20
-14
lines changed

4 files changed

+20
-14
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
}

0 commit comments

Comments
 (0)