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

Optimize Ordinal.EqualsIgnoreCase_Vector with AVX2 and AVX512 #93116

Merged
merged 5 commits into from
Oct 15, 2023

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented Oct 6, 2023

Closes #92497

Currently, str1.Equals(str2, StringComparison.OrdinalIgnoreCase) is only accelerated with Vector128, this PR adds Vector256 and Vector512 via ISimdVector abstraction - the best part that there is no code duplication.

public class Benchmarks
{
    static readonly string s1 = @"C:\prj\runtime\src\coreclr\System.Private.CoreLib\System.Private.CoreLib.csproj";
    static readonly string s2 = @"C:\prj\runtime\src\coreclr\System.Private.CoreLib\system.Private.CoreLib.csproj";

    [Benchmark]
    public bool EqualsIgnoreCase()
    {
        return s1.Equals(s2, StringComparison.OrdinalIgnoreCase);
    }
}
Method Toolchain Mean Error StdDev Ratio
EqualsIgnoreCase Core_Root\corerun.exe 10.610 ns 0.1701 ns 0.1591 ns 1.00
EqualsIgnoreCase Core_Root_PR\corerun.exe 5.878 ns 0.0354 ns 0.0313 ns 0.55

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Oct 6, 2023
@ghost ghost assigned EgorBo Oct 6, 2023
@EgorBo EgorBo added area-System.Runtime and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Oct 6, 2023
@ghost
Copy link

ghost commented Oct 6, 2023

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

Issue Details

Closes #92497

Currently, str1.Equals(str2, StringComparison.OrdinalIgnoreCase) is only accelerated with Vector128, this PR adds Vector256 and Vector512 via ISimdVector abstraction - the best part that there is no code duplication.

public class Benchmarks
{
    static readonly string s1 = @"C:\prj\runtime\src\coreclr\System.Private.CoreLib\System.Private.CoreLib.csproj";
    static readonly string s2 = @"C:\prj\runtime\src\coreclr\System.Private.CoreLib\system.Private.CoreLib.csproj";

    [Benchmark]
    public bool EqualsIgnoreCase()
    {
        return s1.Equals(s2, StringComparison.OrdinalIgnoreCase);
    }
}
Method Toolchain Mean Error StdDev Ratio
EqualsIgnoreCase Core_Root\corerun.exe 10.470 ns 4.530 ns 0.2483 ns 1.00
EqualsIgnoreCase Core_Root_PR\corerun.exe 5.502 ns 4.186 ns 0.2295 ns 0.53
Author: EgorBo
Assignees: EgorBo
Labels:

area-System.Runtime

Milestone: -

@EgorBo EgorBo marked this pull request as ready for review October 7, 2023 19:20
@EgorBo
Copy link
Member Author

EgorBo commented Oct 7, 2023

@stephentoub @tannergooding PTAL, some notes:

  1. I copied parts of Ascii.EqualsIgnoreCase here, I can't just invoke Ascii.EqualsIgnoreCase because we need to fallback to ICU in case of non-ascii, while Ascii.EqualsIgnoreCase just bails out. I'll think of how I can unify them separately - didn't want to regress Ascii.EqualsIgnoreCase by adding more conditional logic in it.
  2. Trailing elements are now handled with SIMD (used to be scalar) so all benchmarks for Length=8 (chars) and higher show improvements.

Had to replace == with Equals due to #93174

@EgorBo
Copy link
Member Author

EgorBo commented Oct 12, 2023

Ping, also cc @adamsitnik @MihaZupan who authored/contributed Ascii.cs where I copied this code from

Copy link
Member

@MihaZupan MihaZupan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM

@EgorBo EgorBo merged commit 4322fc9 into dotnet:main Oct 15, 2023
174 checks passed
@EgorBo EgorBo deleted the opt-simd-equals-ignore-case branch October 15, 2023 11:56
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add AVX/AVX512 to Ordinal.EqualsIgnoreCase
4 participants