Skip to content

Commit 5bed300

Browse files
committed
Fabulous performance optimization 3-4X for the worst-case of a constant array, which also makes sense for all other cases and doesn't make a special case for constant array.
1 parent be7be7c commit 5bed300

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

HPCsharp/SelectionRadix.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,16 @@ private static void RadixSelectiontMsdUIntInner(uint[] a, int first, int length,
3838
if (binLength == 0) continue; // skip empty bins
3939
if (k >= startOfBin[kthBin] && k <= (startOfBin[kthBin + 1] - 1)) break;
4040
}
41-
int _current_ob = first, _current_ib = startOfBin[kthBin]; // _ob = outside of bin, _ib = inside of bin
41+
int _current_ob = first, _current_ib = startOfBin[kthBin], found_ob; // _ob = outside of bin, _ib = inside of bin
4242
while (true) // process elements outside the bin that k is in, which are to the left of that bin
4343
{
4444
// Look for the element that belongs in the bin that k is in, to move into that bin
45-
for (; _current_ob < startOfBin[kthBin]; _current_ob++)
46-
if (((a[_current_ob] >> shiftRightAmount) & bitMask) == kthBin) break;
45+
for (found_ob = 0; _current_ob < startOfBin[kthBin]; _current_ob++)
46+
if (((a[_current_ob] >> shiftRightAmount) & bitMask) == kthBin) { found_ob = 1; break; }
4747
// Look for the first location in the bin that k is in, which has an element that does not belong in that bin
48-
for (; _current_ib < startOfBin[kthBin + 1]; _current_ib++)
49-
if (((a[_current_ib] >> shiftRightAmount) & bitMask) != kthBin) break;
48+
if (found_ob == 1)
49+
for (; _current_ib < startOfBin[kthBin + 1]; _current_ib++)
50+
if (((a[_current_ib] >> shiftRightAmount) & bitMask) != kthBin) break;
5051

5152
if (_current_ib >= startOfBin[kthBin + 1] || _current_ob >= startOfBin[kthBin]) break; // The bin that k is in is full or all the element outside the bin to the left have been exhausted
5253
a[_current_ib++] = a[_current_ob++]; // Move the element that belongs in the bin into the bin
@@ -55,11 +56,12 @@ private static void RadixSelectiontMsdUIntInner(uint[] a, int first, int length,
5556
while (true) // process elements outside the bin that k is in, which are to the right of that bin
5657
{
5758
// Look for the element that belongs in the bin that k is in, to move into that bin
58-
for (; _current_ob <= last; _current_ob++)
59-
if (((a[_current_ob] >> shiftRightAmount) & bitMask) == kthBin) break;
59+
for (found_ob = 0; _current_ob <= last; _current_ob++)
60+
if (((a[_current_ob] >> shiftRightAmount) & bitMask) == kthBin) { found_ob = 1; break; }
6061
// Look for the first location in the bin that k is in, which has an element that does not belong in that bin
61-
for (; _current_ib < startOfBin[kthBin + 1]; _current_ib++)
62-
if (((a[_current_ib] >> shiftRightAmount) & bitMask) != kthBin) break;
62+
if (found_ob == 1)
63+
for (; _current_ib < startOfBin[kthBin + 1]; _current_ib++)
64+
if (((a[_current_ib] >> shiftRightAmount) & bitMask) != kthBin) break;
6365

6466
if (_current_ib >= startOfBin[kthBin + 1] || _current_ob > last) break; // The bin that k is in is full or all the element outside the bin to the right have been exhausted
6567
a[_current_ib++] = a[_current_ob++]; // Move the element that belongs in the bin into the bin

0 commit comments

Comments
 (0)