Skip to content

Add is pattern benchmarks - #85027

Merged
jjonescz merged 2 commits into
dotnet:mainfrom
jjonescz:80052-is-codegen-benchmarks
Aug 27, 2026
Merged

Add is pattern benchmarks#85027
jjonescz merged 2 commits into
dotnet:mainfrom
jjonescz:80052-is-codegen-benchmarks

Conversation

@jjonescz

@jjonescz jjonescz commented Aug 25, 2026

Copy link
Copy Markdown
Member

Follow up on #84961 (comment).

Results of IsPatternLeadingSlashBenchmarks

BenchmarkDotNet v0.16.0-preview.1, Windows 11 (10.0.26100.9106/24H2/2024Update/HudsonValley) (Hyper-V)
Intel Xeon Platinum 8370C CPU 2.80GHz (Max: 2.79GHz), 1 CPU, 16 logical and 8 physical cores
Memory: 63.95 GB Total, 16.03 GB Available
.NET SDK 11.0.100-preview.7.26381.103
  [Host]   : .NET 10.0.11 (10.0.11, 10.0.1126.37416), X64 RyuJIT x86-64-v4
  ShortRun : .NET 10.0.11 (10.0.11, 10.0.1126.37416), X64 RyuJIT x86-64-v4

Job=ShortRun  EvaluateOverhead=False  IterationCount=3  
LaunchCount=1  WarmupCount=3  

Method Input Mean Error StdDev Ratio RatioSD
Comparisons Empty 4.081 ns 6.2051 ns 0.3401 ns 1.00 0.00
Pattern Empty 3.537 ns 0.9690 ns 0.0531 ns 0.87 0.07
Comparisons Slash 4.010 ns 1.4465 ns 0.0793 ns 1.00 0.00
Pattern Slash 4.241 ns 0.7739 ns 0.0424 ns 1.06 0.02
Comparisons Backslash 3.593 ns 2.5362 ns 0.1390 ns 1.00 0.00
Pattern Backslash 4.629 ns 0.6133 ns 0.0336 ns 1.29 0.04
Comparisons Other 4.184 ns 3.6089 ns 0.1978 ns 1.00 0.00
Pattern Other 4.678 ns 3.8583 ns 0.2115 ns 1.12 0.06
Microsoft Reviewers: Open in CodeFlow

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).
There may be pipelines that require an authorized user to comment /azp run to run.

[MethodImpl(MethodImplOptions.NoInlining)]
private static bool HasLeadingSlashWithPattern(ReadOnlySpan<char> path)
{
if (path.Length > 0 && path[0] is '/' or '\\')

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR adds new micro-benchmarks under src/Tools/Benchmarks for small is-pattern scenarios and updates the compiler’s LocalRewriter_IsPatternOperator comment to reference an in-repo benchmark source instead of an external PR link.

Changes:

  • Added IsPatternBenchmarks.cs containing benchmarks comparing comparison-based implementations vs is-pattern implementations for a few small-pattern scenarios.
  • Updated the documentation comment for MaxTestsForInvertedLinearSequence to point at the new benchmark file.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/Tools/Benchmarks/IsPatternBenchmarks.cs Introduces new BenchmarkDotNet benchmarks for small is-pattern cases (leading slash and small byte-pattern sets).
src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_IsPatternOperator.cs Updates the explanatory comment for the inverted-linear-sequence threshold to reference in-repo benchmark code.
Suppressed comments (1)

src/Tools/Benchmarks/IsPatternBenchmarks.cs:107

  • The baseline implementation compares a byte (Value) to char literals. Aligning the baseline with the pattern version by using byte constants makes the comparison more directly about the lowering strategy and avoids implicit integral promotions.
    public bool Comparisons() => Guard && (Value == '+' || Value == '.' || Value == 'E' || Value == 'e');

Comment thread src/Tools/Benchmarks/IsPatternBenchmarks.cs Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Suppressed comments (1)

Previously missed (1) — in code that hasn't changed since the last review.

src/Tools/Benchmarks/IsPatternBenchmarks.cs:34

  • Avoid the null-forgiving initializer here; the benchmark can start with a non-null default and still be overwritten in GlobalSetup. This removes a nullability suppression and makes the type safe if the benchmark is ever instantiated outside BenchmarkDotNet’s setup flow.
    private string _path = null!;

@jjonescz
jjonescz marked this pull request as ready for review August 26, 2026 14:15
@jjonescz
jjonescz requested a review from a team as a code owner August 26, 2026 14:15
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).
There may be pipelines that require an authorized user to comment /azp run to run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants