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

Automatically anchor regexes beginning with .* #1706

Merged
merged 1 commit into from
Jan 17, 2020

Conversation

stephentoub
Copy link
Member

@stephentoub stephentoub commented Jan 14, 2020

Avoid unnecessary backtracking by adding an implicit ^ when

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Running;
using System.Text.RegularExpressions;

[MemoryDiagnoser]
public class Program
{
    static void Main(string[] args) => BenchmarkSwitcher.FromAssemblies(new[] { typeof(Program).Assembly }).Run(args);

    private readonly Regex _regex = new Regex(@".*text", RegexOptions.Compiled);

    [Benchmark] public bool IsNotMatch() => _regex.IsMatch("this is a test to see how well this does when trying to match this tex");
}
Method Toolchain Mean Error StdDev Ratio Gen 0 Gen 1 Gen 2 Allocated
IsNotMatch \master\corerun.exe 10.796 us 0.0597 us 0.0498 us 1.00 - - - -
IsNotMatch \pr\corerun.exe 1.131 us 0.0008 us 0.0007 us 0.10 - - - -

Without whitespace: https://github.com/dotnet/runtime/pull/1706/files?w=1

Contributes to #1349
cc: @danmosemsft, @eerhardt, @ViktorHofer, @pgovind

@stephentoub stephentoub added this to the 5.0 milestone Jan 14, 2020
@stephentoub
Copy link
Member Author

Thanks for reviewing, @danmosemsft.

@stephentoub stephentoub merged commit 4c5e56b into dotnet:master Jan 17, 2020
@stephentoub stephentoub deleted the starloop branch January 17, 2020 02:19
@ghost ghost locked as resolved and limited conversation to collaborators Dec 11, 2020
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.

2 participants