Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PGO: Profiled SequenceEqual #96571

Merged
merged 3 commits into from
Jan 7, 2024
Merged

PGO: Profiled SequenceEqual #96571

merged 3 commits into from
Jan 7, 2024

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented Jan 6, 2024

Extends #96311 to cover memcmp idiom (SpanHelpers.SequenceEqual):

bool result = SequenceEqual(dst, src, len);

is optimized to:

bool result;
if (len == 10) // PGO data
    result = SequenceEqual(dst, src, 10); // unrolled
else
    result = SequenceEqual(dst, src, len); // fallback

Benchmark 1:

public class Benchmarks
{
    // mutable fields (to hide from jit)
    string s1 = 10000.ToString();
    string s2 = 10001.ToString();

    [Benchmark]
    public bool Compare() => s1 == s2; // ends up calling SpanHelpers.SequenceEqual under the hood
}
Method Toolchain Mean Ratio
Compare \Core_Root\corerun.exe 1.8656 ns 1.00
Compare \Core_Root_PR\corerun.exe 0.7309 ns 0.39

Benchmark 2:

public class Benchmarks
{
    public static IEnumerable<object[]> Data()
    {
        yield return new object[] { new int[2], new int[2] };
        yield return new object[] { new int[10], new int[10] };
        yield return new object[] { new int[16], new int[16] };
        yield return new object[] { new int[32], new int[32] };
    }

    [Benchmark]
    [ArgumentsSource(nameof(Data))]
    public bool Compare(int[] a, int[] b) => a.AsSpan().SequenceEqual(b);
}
Method Toolchain a b Mean Ratio
Compare \Core_Root\corerun.exe Int32[2] Int32[2] 1.8737 ns 1.03
Compare \Core_Root_PR\corerun.exe Int32[2] Int32[2] 0.4867 ns 0.27
Compare \Core_Root\corerun.exe Int32[10] Int32[10] 1.8131 ns 1.00
Compare \Core_Root_PR\corerun.exe Int32[10] Int32[10] 0.8914 ns 0.49
Compare \Core_Root\corerun.exe Int32[16] Int32[16] 1.5324 ns 0.85
Compare \Core_Root_PR\corerun.exe Int32[16] Int32[16] 0.8889 ns 0.49
Compare \Core_Root\corerun.exe Int32[32] Int32[32] 1.8264 ns 1.01
Compare \Core_Root_PR\corerun.exe Int32[32] Int32[32] 0.9248 ns 0.51

@ghost ghost assigned EgorBo Jan 6, 2024
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Jan 6, 2024
@ghost
Copy link

ghost commented Jan 6, 2024

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

Issue Details

Extends #96311 to cover memcmp idiom (SpanHelpers.SequenceEqual):

bool result = SequenceEqual(dst, src, len);

is optimized to:

bool result;
if (len == 10) // PGO data
    result = SequenceEqual(dst, src, 10); // unrolled
else
    result = SequenceEqual(dst, src, len); // fallback

Benchmark:

public static IEnumerable<object[]> Data()
{
    yield return new object[] { new int[2], new int[2] };
    yield return new object[] { new int[10], new int[10] };
    yield return new object[] { new int[16], new int[16] };
    yield return new object[] { new int[32], new int[32] };
}

[Benchmark]
[ArgumentsSource(nameof(Data))]
public bool Compare(int[] a, int[] b) => a.AsSpan().SequenceEqual(b);
Method Toolchain a b Mean Ratio
Compare \Core_Root\corerun.exe Int32[2] Int32[2] 1.8737 ns 1.03
Compare \Core_Root_PR\corerun.exe Int32[2] Int32[2] 0.4867 ns 0.27
Compare \Core_Root\corerun.exe Int32[10] Int32[10] 1.8131 ns 1.00
Compare \Core_Root_PR\corerun.exe Int32[10] Int32[10] 0.8914 ns 0.49
Compare \Core_Root\corerun.exe Int32[16] Int32[16] 1.5324 ns 0.85
Compare \Core_Root_PR\corerun.exe Int32[16] Int32[16] 0.8889 ns 0.49
Compare \Core_Root\corerun.exe Int32[32] Int32[32] 1.8264 ns 1.01
Compare \Core_Root_PR\corerun.exe Int32[32] Int32[32] 0.9248 ns 0.51
Author: EgorBo
Assignees: EgorBo
Labels:

area-CodeGen-coreclr

Milestone: -

@EgorBo
Copy link
Member Author

EgorBo commented Jan 6, 2024

/azp run runtime-coreclr pgo, runtime-coreclr libraries-pgo, runtime-coreclr pgostress

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@EgorBo
Copy link
Member Author

EgorBo commented Jan 6, 2024

@AndyAyersMS @dotnet/jit-contrib PTAL

@EgorBo
Copy link
Member Author

EgorBo commented Jan 6, 2024

Leaving Memset idiom for future, because it's currently not a call, but GT_BLK so it will need a bit more changes + tracking IL offset for it.

@EgorBo EgorBo merged commit c1de4ba into dotnet:main Jan 7, 2024
129 checks passed
@EgorBo EgorBo deleted the profiled-memcmp branch January 7, 2024 15:45
@github-actions github-actions bot locked and limited conversation to collaborators Feb 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants