Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Microsoft.CodeAnalysis.CSharp
internal sealed partial class LocalRewriter
{
/// <summary>
/// Benchmark results (see https://github.com/dotnet/roslyn/pull/84961) show that short patterns can be more
/// Benchmark results (<c>src/Tools/Benchmarks/IsPatternBenchmarks.cs</c>) show that short patterns can be more
/// efficient when emitted as comparisons. Larger patterns can benefit from switch dispatch.
/// </summary>
Comment thread
jjonescz marked this conversation as resolved.
Resolved
private const int MaxTestsForInvertedLinearSequence = 3;
Expand Down
150 changes: 150 additions & 0 deletions src/Tools/Benchmarks/IsPatternBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// 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,
}

/// <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 '\\')

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?

return true;

return false;
}
}

/// <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 != '.' && value != 'E' && value != 'e'
Comment thread
jjonescz marked this conversation as resolved.
Resolved
? 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;
}
}

/// <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 == '+' || Value == '.' || Value == 'E' || Value == 'e');

[Benchmark]
public bool Pattern() => Guard && Value is (byte)'+' or (byte)'.' or (byte)'E' or (byte)'e';
}

/// <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(),
};
}
}
Loading