-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Add is pattern benchmarks
#85027
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
Merged
+167
−2
Merged
Add is pattern benchmarks
#85027
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System; | ||
| using System.Runtime.CompilerServices; | ||
| using BenchmarkDotNet.Attributes; | ||
|
|
||
| namespace Benchmarks; | ||
|
|
||
| public enum IsPatternInputPosition | ||
| { | ||
| FirstHit, | ||
| LastHit, | ||
| Miss, | ||
| } | ||
|
|
||
| public enum IsPatternPathInput | ||
| { | ||
| Empty, | ||
| Slash, | ||
| Backslash, | ||
| Other, | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// The pattern and comparison forms are expected to have equivalent performance when short patterns use linear | ||
| /// lowering. This benchmark detects regressions in that equivalence. | ||
| /// </summary> | ||
| /// <seealso href="https://github.com/dotnet/roslyn/issues/80052"/> | ||
| [EvaluateOverhead(false)] | ||
| public class IsPatternLeadingSlashBenchmarks | ||
| { | ||
| private string _path = null!; | ||
|
|
||
| [Params(IsPatternPathInput.Empty, IsPatternPathInput.Slash, IsPatternPathInput.Backslash, IsPatternPathInput.Other)] | ||
| public IsPatternPathInput Input { get; set; } | ||
|
|
||
| [GlobalSetup] | ||
| public void Setup() | ||
| { | ||
| _path = Input switch | ||
| { | ||
| IsPatternPathInput.Empty => "", | ||
| IsPatternPathInput.Slash => "/path", | ||
| IsPatternPathInput.Backslash => "\\path", | ||
| IsPatternPathInput.Other => "path", | ||
| _ => throw new InvalidOperationException(), | ||
| }; | ||
| } | ||
|
|
||
| [Benchmark(Baseline = true)] | ||
| public bool Comparisons() => HasLeadingSlashWithComparisons(_path); | ||
|
|
||
| [Benchmark] | ||
| public bool Pattern() => HasLeadingSlashWithPattern(_path); | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| private static bool HasLeadingSlashWithComparisons(ReadOnlySpan<char> path) | ||
| { | ||
| if (path.Length > 0 && (path[0] == '/' || path[0] == '\\')) | ||
| return true; | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| private static bool HasLeadingSlashWithPattern(ReadOnlySpan<char> path) | ||
| { | ||
| if (path.Length > 0 && path[0] is '/' or '\\') | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 I guess the pattern cases will become equivalent to the comparison cases when the compiler used to build these is updated to include the fix, but perhaps these benchmarks are still useful as a sort of regression gate? |
||
| return true; | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// The pattern and comparison forms are expected to have equivalent performance when short patterns use linear | ||
| /// lowering. This benchmark detects regressions in that equivalence. | ||
| /// </summary> | ||
| /// <seealso href="https://github.com/dotnet/runtime/pull/132452"/> | ||
| [EvaluateOverhead(false)] | ||
| public class IsPatternJsonTerminatorBenchmarks : IsPatternByteInputBenchmarks | ||
| { | ||
| protected override byte FirstCase => (byte)'.'; | ||
| protected override byte LastCase => (byte)'e'; | ||
|
|
||
| [Benchmark(Baseline = true)] | ||
| public int Comparisons() | ||
| { | ||
| var value = Value; | ||
| return value != (byte)'.' && value != (byte)'E' && value != (byte)'e' | ||
| ? value + 1 | ||
| : value - 1; | ||
| } | ||
|
|
||
| [Benchmark] | ||
| public int Pattern() | ||
| { | ||
| var value = Value; | ||
| return value is not ((byte)'.' or (byte)'E' or (byte)'e') | ||
| ? value + 1 | ||
| : value - 1; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Compares explicit linear tests with general decision-DAG dispatch for four sparse values. | ||
| /// </summary> | ||
| /// <seealso href="https://github.com/dotnet/roslyn/pull/84961"/> | ||
| [EvaluateOverhead(false)] | ||
| public class IsPatternSparseFourValueBenchmarks : IsPatternByteInputBenchmarks | ||
| { | ||
| protected override byte FirstCase => (byte)'+'; | ||
| protected override byte LastCase => (byte)'e'; | ||
|
|
||
| [Benchmark(Baseline = true)] | ||
| public bool Comparisons() => Guard && (Value == (byte)'+' || Value == (byte)'.' || Value == (byte)'E' || Value == (byte)'e'); | ||
|
|
||
| [Benchmark] | ||
| public bool Pattern() => Guard && Value is (byte)'+' or (byte)'.' or (byte)'E' or (byte)'e'; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Compares explicit linear tests with general decision-DAG dispatch for four dense values. | ||
| /// </summary> | ||
| /// <seealso href="https://github.com/dotnet/roslyn/pull/84961"/> | ||
| [EvaluateOverhead(false)] | ||
| public class IsPatternDenseFourValueBenchmarks : IsPatternByteInputBenchmarks | ||
| { | ||
| protected override byte FirstCase => 1; | ||
| protected override byte LastCase => 4; | ||
|
|
||
| [Benchmark(Baseline = true)] | ||
| public bool Comparisons() => Guard && (Value == 1 || Value == 2 || Value == 3 || Value == 4); | ||
|
|
||
| [Benchmark] | ||
| public bool Pattern() => Guard && Value is 1 or 2 or 3 or 4; | ||
| } | ||
|
|
||
| public abstract class IsPatternByteInputBenchmarks | ||
| { | ||
| [Params(IsPatternInputPosition.FirstHit, IsPatternInputPosition.LastHit, IsPatternInputPosition.Miss)] | ||
| public IsPatternInputPosition Input { get; set; } | ||
|
|
||
| protected bool Guard; | ||
| protected byte Value; | ||
|
|
||
| protected abstract byte FirstCase { get; } | ||
| protected abstract byte LastCase { get; } | ||
|
|
||
| [GlobalSetup] | ||
| public void Setup() | ||
| { | ||
| Guard = true; | ||
| Value = Input switch | ||
| { | ||
| IsPatternInputPosition.FirstHit => FirstCase, | ||
| IsPatternInputPosition.LastHit => LastCase, | ||
| IsPatternInputPosition.Miss => byte.MaxValue, | ||
| _ => throw new InvalidOperationException(), | ||
| }; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.