-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Use BitOperations in more callsites (CoreCLR) #22630
Conversation
int count = 0; | ||
for (; n != 0; n = n >> 4) | ||
count += nibblebits[n & 0x0f]; | ||
return count; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ran the following unit
to ensure that these methods are in fact equivalent:
[Fact]
public static void Foo()
{
for (int i = 0; i < 32; i++)
{
uint n = 11u << i; // 0x1011, to make it more interesting
int expected = BitOps.PopCount(n); // new
int actual = bitcount(n); // old
Assert.Equal(expected, actual);
expected = BitOps.TrailingZeroCount(n); // new
actual = bitindex(n); // old
Assert.Equal(expected, actual);
}
}
// Original source
static int[] nibblebits = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
private static int bitcount(uint n)
{
int count = 0;
for (; n != 0; n = n >> 4)
count += nibblebits[n & 0x0f];
return count;
}
private static int bitindex(uint n)
{
//Debug.Assert(bitcount(n) == 1);
int idx = 0;
while ((n & (1 << idx)) == 0)
idx++;
return idx;
}
} | ||
private static int bitindex(uint n) | ||
{ | ||
Debug.Assert(bitcount(n) == 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this assert
is already taken care of in the preceding if
statement at the callsite
} | ||
else | ||
{ | ||
// legacy etw session | ||
sessionList.Add(new SessionInfo(bitcount((uint)SessionMask.All) + 1, etwSessionId)); | ||
val = BitOperations.PopCount((uint)SessionMask.All); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like this is just a constant, val = 4;
?
public static SessionMask All
{
get { return new SessionMask(MASK); }
}
internal const uint MASK = 0x0fU; // 0000_1111
internal struct SessionMask
{
private uint m_mask;
public SessionMask(uint mask = 0)
{ m_mask = mask & MASK; }
…
}
@dotnet-bot test OSX10.12 x64 Checked Innerloop Build and Test please |
Thanks! Could you please copy the |
@grant-d I have pushed a portable implementations of PopCount and TrailingZeroCount to your branch, so that this lands in CoreFX without needing further adjustments. Could you please review them? |
Thanks for doing that @jkotas. It have reviewed and it looks good. Maybe there's an opportunity in a later PR to optimize that loop. |
@dotnet-bot test Windows_NT x64 Checked CoreFX Tests please |
* Missed perf improvement in EventProvider * Add missing methods to StubEnvironment Signed-off-by: dotnet-bot <[email protected]>
* Missed perf improvement in EventProvider * Add missing methods to StubEnvironment Signed-off-by: dotnet-bot <[email protected]>
* Missed perf improvement in EventProvider * Add missing methods to StubEnvironment Signed-off-by: dotnet-bot <[email protected]>
* Missed perf improvement in EventProvider * Add missing methods to StubEnvironment Signed-off-by: dotnet-bot <[email protected]>
* Missed perf improvement in EventProvider * Add missing methods to StubEnvironment Signed-off-by: dotnet-bot <[email protected]>
* Missed perf improvement in EventProvider * Add missing methods to StubEnvironment Signed-off-by: dotnet-bot <[email protected]>
* Missed perf improvement in EventProvider * Add missing methods to StubEnvironment Commit migrated from dotnet/coreclr@3c0f075
Some more missed optimizations from previous PRs.
cc @tannergooding, @jkotas