Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Use BitOperations in more callsites (CoreCLR) #22630

Merged
merged 10 commits into from
Mar 2, 2019

Conversation

grant-d
Copy link

@grant-d grant-d commented Feb 15, 2019

Some more missed optimizations from previous PRs.

cc @tannergooding, @jkotas

int count = 0;
for (; n != 0; n = n >> 4)
count += nibblebits[n & 0x0f];
return count;
Copy link
Author

@grant-d grant-d Feb 15, 2019

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);
Copy link
Author

@grant-d grant-d Feb 15, 2019

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

@grant-d grant-d changed the title Use BitOps in callsites (WIP) Perf: Use BitOps in more callsites (WIP) Feb 15, 2019
@grant-d grant-d changed the title Perf: Use BitOps in more callsites (WIP) [WIP] Use BitOperations in more callsites Feb 27, 2019
}
else
{
// legacy etw session
sessionList.Add(new SessionInfo(bitcount((uint)SessionMask.All) + 1, etwSessionId));
val = BitOperations.PopCount((uint)SessionMask.All);
Copy link
Author

@grant-d grant-d Feb 27, 2019

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; }}

@grant-d
Copy link
Author

grant-d commented Feb 27, 2019

@dotnet-bot test OSX10.12 x64 Checked Innerloop Build and Test please
@dotnet-bot test Windows_NT x64 Checked CoreFX Tests please
@dotnet-bot test Windows_NT x64 Release CoreFX Tests please

@grant-d grant-d changed the title [WIP] Use BitOperations in more callsites Use BitOperations in more callsites (CoreCLR) Feb 27, 2019
@jkotas
Copy link
Member

jkotas commented Feb 27, 2019

Thanks! Could you please copy the EventProvider.cs file to CoreFX and make sure that build -framework netfx builds fine? I think this will need downlevel implementations of these new operations in the StubEnvironment to make corefx build with this.

@jkotas
Copy link
Member

jkotas commented Mar 2, 2019

@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?

@grant-d
Copy link
Author

grant-d commented Mar 2, 2019

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.
LGTM

@grant-d
Copy link
Author

grant-d commented Mar 2, 2019

@dotnet-bot test Windows_NT x64 Checked CoreFX Tests please

@jkotas jkotas merged commit 3c0f075 into dotnet:master Mar 2, 2019
Dotnet-GitSync-Bot pushed a commit to Dotnet-GitSync-Bot/corefx that referenced this pull request Mar 2, 2019
* Missed perf improvement in EventProvider

* Add missing methods to StubEnvironment

Signed-off-by: dotnet-bot <[email protected]>
Dotnet-GitSync-Bot pushed a commit to Dotnet-GitSync-Bot/corert that referenced this pull request Mar 2, 2019
* Missed perf improvement in EventProvider

* Add missing methods to StubEnvironment

Signed-off-by: dotnet-bot <[email protected]>
Dotnet-GitSync-Bot pushed a commit to Dotnet-GitSync-Bot/mono that referenced this pull request Mar 2, 2019
* Missed perf improvement in EventProvider

* Add missing methods to StubEnvironment

Signed-off-by: dotnet-bot <[email protected]>
marek-safar pushed a commit to mono/mono that referenced this pull request Mar 2, 2019
* Missed perf improvement in EventProvider

* Add missing methods to StubEnvironment

Signed-off-by: dotnet-bot <[email protected]>
jkotas pushed a commit to dotnet/corert that referenced this pull request Mar 2, 2019
* Missed perf improvement in EventProvider

* Add missing methods to StubEnvironment

Signed-off-by: dotnet-bot <[email protected]>
jkotas pushed a commit to dotnet/corefx that referenced this pull request Mar 2, 2019
* Missed perf improvement in EventProvider

* Add missing methods to StubEnvironment

Signed-off-by: dotnet-bot <[email protected]>
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
* Missed perf improvement in EventProvider

* Add missing methods to StubEnvironment


Commit migrated from dotnet/coreclr@3c0f075
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants