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

JIT: Unroll more Equals/StartsWith/EndsWith on arm64 #109036

Merged
merged 6 commits into from
Nov 20, 2024

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented Oct 18, 2024

Seems like it's quite beneficial even on large sizes, at least on arm64:

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

public class Bench
{
    string s = "https://pkgs.dev.azure.com/dnceng/public/packaging/dotnet10/";

    [Benchmark]
    public bool Bestcase() => 
        s.StartsWith("https://pkgs.dev.azure.com/dnceng/public/pack", StringComparison.Ordinal);
    
    [Benchmark]
    public bool Bestcase_IgnoreCase() => 
        s.StartsWith("https://pkgs.dev.azure.com/dnceng/public/pack", StringComparison.OrdinalIgnoreCase);

    // Worst case: differs at 1st char:

    [Benchmark]
    public bool Worstcase() => 
        s.StartsWith("Xttps://pkgs.dev.azure.com/dnceng/public/pack", StringComparison.Ordinal);
    
    [Benchmark]
    public bool Worstcase_IgnoreCase() => 
        s.StartsWith("Xttps://pkgs.dev.azure.com/dnceng/public/pack", StringComparison.OrdinalIgnoreCase);
}
BenchmarkDotNet v0.14.0, Ubuntu 24.04 LTS (Noble Numbat)
Arm64
  Job-NEWBNL : .NET 10.0.0 (42.42.42.42424), Arm64 RyuJIT AdvSIMD
  Job-LSVZLO : .NET 10.0.0 (42.42.42.42424), Arm64 RyuJIT AdvSIMD
EnvironmentVariables=DOTNET_JitDisasm=Bestcase_IgnoreCase
Method Toolchain Mean Error Ratio
Bestcase Main 4.1458 ns 0.0071 ns 1.00
Bestcase PR 0.6151 ns 0.0007 ns 0.15
Bestcase_IgnoreCase Main 5.9848 ns 0.1685 ns 1.00
Bestcase_IgnoreCase PR 1.5489 ns 0.0044 ns 0.26
Worstcase Main 1.7136 ns 0.0003 ns 1.00
Worstcase PR 0.6572 ns 0.0013 ns 0.38
Worstcase_IgnoreCase Main 2.5230 ns 0.0015 ns 1.00
Worstcase_IgnoreCase PR 1.2213 ns 0.0016 ns 0.48

@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 Oct 18, 2024
@EgorBo
Copy link
Member Author

EgorBo commented Oct 19, 2024

@MihuBot

@EgorBo
Copy link
Member Author

EgorBo commented Oct 19, 2024

@EgorBot -arm64 --envvars DOTNET_JitDisasm:Bestcase_IgnoreCase

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

public class Bench
{
    string s = "https://pkgs.dev.azure.com/dnceng/public/packaging/dotnet10/";

    [Benchmark]
    public bool Bestcase() => 
        s.StartsWith("https://pkgs.dev.azure.com/dnceng/public/pack", StringComparison.Ordinal);
    
    [Benchmark]
    public bool Bestcase_IgnoreCase() => 
        s.StartsWith("https://pkgs.dev.azure.com/dnceng/public/pack", StringComparison.OrdinalIgnoreCase);

    // Worst case: differs at 1st char:

    [Benchmark]
    public bool Worstcase() => 
        s.StartsWith("Xttps://pkgs.dev.azure.com/dnceng/public/pack", StringComparison.Ordinal);
    
    [Benchmark]
    public bool Worstcase_IgnoreCase() => 
        s.StartsWith("Xttps://pkgs.dev.azure.com/dnceng/public/pack", StringComparison.OrdinalIgnoreCase);
}

@EgorBo
Copy link
Member Author

EgorBo commented Oct 20, 2024

@MihuBot -arm -dependsOn 108838

@EgorBo
Copy link
Member Author

EgorBo commented Nov 18, 2024

PTAL @jakobbotsch @dotnet/jit-contrib this improves perf on arm64 (even in the worst case when the first char is different). Seems like makes no sense on x64 judging by benchmarks.

@tannergooding
Copy link
Member

Seems like makes no sense on x64 judging by benchmarks.

Do you have a link to the perf numbers on x64?

I would expect this to be similar perf on most x64, but likely better on CPUs like e-cores or other scenarios where call overhead is greater

@EgorBo
Copy link
Member Author

EgorBo commented Nov 18, 2024

@EgorBot -amd -intel --envvars DOTNET_JitDisasm:Bestcase_IgnoreCase

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

public class Bench
{
    string s = "https://pkgs.dev.azure.com/dnceng/public/packaging/dotnet10/";

    [Benchmark]
    public bool Bestcase() => 
        s.StartsWith("https://pkgs.dev.azure.com/dnceng/public/pack", StringComparison.Ordinal);
    
    [Benchmark]
    public bool Bestcase_IgnoreCase() => 
        s.StartsWith("https://pkgs.dev.azure.com/dnceng/public/pack", StringComparison.OrdinalIgnoreCase);

    // Worst case: differs at 1st char:

    [Benchmark]
    public bool Worstcase() => 
        s.StartsWith("Xttps://pkgs.dev.azure.com/dnceng/public/pack", StringComparison.Ordinal);
    
    [Benchmark]
    public bool Worstcase_IgnoreCase() => 
        s.StartsWith("Xttps://pkgs.dev.azure.com/dnceng/public/pack", StringComparison.OrdinalIgnoreCase);
}

@EgorBo
Copy link
Member Author

EgorBo commented Nov 18, 2024

@tannergooding you can find x64 numbers here EgorBot/runtime-utils#162, Basically, almost no wins and clear regressions unlike arm64

@tannergooding
Copy link
Member

you can find x64 numbers here EgorBot/runtime-utils#162, Basically, almost no wins and clear regressions unlike arm64

Thanks! That roughly fits my expectations for that hardware. I don't think we have any e-cores to see if there is a contrast for those devices though, unfortunately.

I have no concerns with it being merged, just wanted to see some of the numbers since it was called out

@EgorBo EgorBo merged commit db6cef1 into dotnet:main Nov 20, 2024
107 of 108 checks passed
@EgorBo EgorBo deleted the unroll-more-str branch November 20, 2024 04:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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.

3 participants