Skip to content

Commit f7974ea

Browse files
authored
JIT_CountProfile: Avoid BSR instruction for low counts (#110258)
The JIT_CountProfile32 and JIT_CountProfile64 functions used BitScanReverse functions to compute the log2 of counters, and then compare that to a threshold. This changes it so that the counter is directly compared to `1<<threshold` instead.
1 parent 1fc8222 commit f7974ea

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

src/coreclr/vm/jithelpers.cpp

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3681,20 +3681,17 @@ HCIMPL1(void, JIT_CountProfile32, volatile LONG* pCounter)
36813681
LONG delta = 1;
36823682
DWORD threshold = g_pConfig->TieredPGO_ScalableCountThreshold();
36833683

3684-
if (count > 0)
3684+
if (count >= (LONG)(1 << threshold))
36853685
{
3686-
DWORD logCount = 0;
3686+
DWORD logCount;
36873687
BitScanReverse(&logCount, count);
36883688

3689-
if (logCount >= threshold)
3689+
delta = 1 << (logCount - (threshold - 1));
3690+
const unsigned rand = HandleHistogramProfileRand();
3691+
const bool update = (rand & (delta - 1)) == 0;
3692+
if (!update)
36903693
{
3691-
delta = 1 << (logCount - (threshold - 1));
3692-
const unsigned rand = HandleHistogramProfileRand();
3693-
const bool update = (rand & (delta - 1)) == 0;
3694-
if (!update)
3695-
{
3696-
return;
3697-
}
3694+
return;
36983695
}
36993696
}
37003697

@@ -3711,20 +3708,17 @@ HCIMPL1(void, JIT_CountProfile64, volatile LONG64* pCounter)
37113708
LONG64 delta = 1;
37123709
DWORD threshold = g_pConfig->TieredPGO_ScalableCountThreshold();
37133710

3714-
if (count > 0)
3711+
if (count >= (LONG64)(1LL << threshold))
37153712
{
3716-
DWORD logCount = 0;
3713+
DWORD logCount;
37173714
BitScanReverse64(&logCount, count);
37183715

3719-
if (logCount >= threshold)
3716+
delta = 1LL << (logCount - (threshold - 1));
3717+
const unsigned rand = HandleHistogramProfileRand();
3718+
const bool update = (rand & (delta - 1)) == 0;
3719+
if (!update)
37203720
{
3721-
delta = 1LL << (logCount - (threshold - 1));
3722-
const unsigned rand = HandleHistogramProfileRand();
3723-
const bool update = (rand & (delta - 1)) == 0;
3724-
if (!update)
3725-
{
3726-
return;
3727-
}
3721+
return;
37283722
}
37293723
}
37303724

0 commit comments

Comments
 (0)