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

Improve Regex performance (mainly interpreted) #449

Merged
merged 6 commits into from
Dec 3, 2019

Conversation

stephentoub
Copy link
Member

This is a follow-on to #271, which improved the performance of Regex, mainly for compiled regular expressions. This PR improves the throughput of interpreted Regexes, those without the RegexOptions.Compiled option.

The primary change here is similar in nature to the primary change for Compiled: generating a fast lookup for ASCII values rather than having to parse/analyze the character class description for each character matched against it. In the Compiled case, we took the time to analyze the first 128 characters and emit a static lookup to make it fast to find the answer for any character < 128. But generating such a lookup table takes time. For Compiled, we're willing to pay that time, because signing up for Compiled is done when you want to maximize throughput at the expense of such upfront costs. We don't have that luxury for non-compiled. Instead, we throw a small additional amount of memory at the problem. Instead of storing 128 bits per character class (one for each character), we store 256 bits per character class: for each character a bit indicating whether we've evaluated it and a bit indicating the result. The first time we encounter a character, we do the more costly evaluation, and then we store back both that we evaluated it and the result; subsequent accesses will see that it was already evaluated and just use the cached result.

A few other, smaller opportunistic changes were also made here based on profiling, including improving some of the codegen around repeated array accesses and inlining a function on a hot path where doing so made an impactful difference. Also a tiny bit of code cleanup as I was touching the relevant functions.

Method Toolchain Mean Error StdDev Ratio Allocated
Email New 465.6 ns 1.26 ns 1.12 ns 0.78 -
Email Old 600.5 ns 2.97 ns 2.78 ns 1.00 -
Date New 242.7 ns 0.43 ns 0.40 ns 0.59 -
Date Old 414.6 ns 0.52 ns 0.49 ns 1.00 -
IP New 1,975.4 ns 3.46 ns 3.07 ns 0.77 -
IP Old 2,573.9 ns 8.53 ns 7.13 ns 1.00 -
Uri New 311.8 ns 24.92 ns 39.53 ns 0.63 -
Uri Old 508.8 ns 1.31 ns 1.16 ns 1.00 -
EmailStatic New 517.3 ns 2.28 ns 2.13 ns 0.80 104 B
EmailStatic Old 650.2 ns 2.03 ns 1.69 ns 1.00 104 B
DateStatic New 303.4 ns 0.44 ns 0.39 ns 0.66 104 B
DateStatic Old 458.3 ns 1.95 ns 1.83 ns 1.00 104 B
IPStatic New 2,038.7 ns 2.53 ns 2.37 ns 0.75 104 B
IPStatic Old 2,702.2 ns 22.82 ns 19.05 ns 1.00 104 B
UriStatic New 341.6 ns 0.55 ns 0.43 ns 0.62 104 B
UriStatic Old 551.8 ns 1.36 ns 1.13 ns 1.00 104 B
Ctor New 5,568.8 ns 15.11 ns 12.62 ns 0.99 9792 B
Ctor Old 5,641.3 ns 41.50 ns 38.81 ns 1.00 9712 B
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Running;
using System.Text.RegularExpressions;

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

[MemoryDiagnoser]
public class Regexes
{
    [Params(RegexOptions.None)] //, RegexOptions.Compiled)]
    public RegexOptions Options { get; set; }

    private Regex _email, _date, _ip, _uri;

    private const string EmailPattern = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,12}|[0-9]{1,3})(\]?)$";
    private const string DatePattern = @"\b(?<month>\d{1,2})/(?<day>\d{1,2})/(?<year>\d{2,4})\b";
    private const string IPPattern = @"(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9])";
    private const string UriPattern = @"[\w]+://[^/\s?#]+[^\s?#]+(?:\?[^\s#]*)?(?:#[^\s]*)?";

    [GlobalSetup]
    public void Setup()
    {
        _email = new Regex(EmailPattern, Options);
        _date = new Regex(DatePattern, Options);
        _ip = new Regex(IPPattern, Options);
        _uri = new Regex(UriPattern, Options);
    }

    [Benchmark] public void Email() => _email.IsMatch("[email protected]");
    [Benchmark] public void Date() => _date.IsMatch("Today is 11/18/2019");
    [Benchmark] public void IP() => _ip.IsMatch("012.345.678.910");
    [Benchmark] public void Uri() => _uri.IsMatch("http://example.org");

    [Benchmark] public void EmailStatic() => Regex.IsMatch("[email protected]", EmailPattern, Options);
    [Benchmark] public void DateStatic() => Regex.IsMatch("Today is 11/18/2019", DatePattern, Options);
    [Benchmark] public void IPStatic() => Regex.IsMatch("012.345.678.910", IPPattern, Options);
    [Benchmark] public void UriStatic() => Regex.IsMatch("http://example.org", UriPattern, Options);

    [Benchmark] public void Ctor() => new Regex(@"(^(.*)(\(([0-9]+),([0-9]+)\)): )(error|warning) ([A-Z]+[0-9]+) ?: (.*)", Options);
}

cc: @danmosemsft, @ViktorHofer, @davidwrighton, @eerhardt

Reduce the checks needed and elimiate unnecessary layers of function calls.
This doesn't show up in real regexes and is just adding unnecessary complication to the code.  No one writes `[a-b]`... they just write `a`.  SingletonInverse is more useful, as you can search for any character except for a specific one, e.g. find the first character that's not a dash.
It's small but isn't getting inlined; it's only called in 4 places, but on hot paths, and inlininig it nets around an ~8% throughput win.
Copy link
Member

@eerhardt eerhardt left a comment

Choose a reason for hiding this comment

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

Looks good to me. Just a couple minor nits/questions.

@stephentoub
Copy link
Member Author

Thanks for reviewing, @eerhardt.

@stephentoub stephentoub merged commit 2cc926e into dotnet:master Dec 3, 2019
@stephentoub stephentoub deleted the regexperf branch December 3, 2019 00:09
@danmoseley
Copy link
Member

This is awesome, thanks @stephentoub. Do you think there may much more that can be achieved incrementally?

@stephentoub
Copy link
Member Author

Do you think there may much more that can be achieved incrementally?

I do, e.g. #496

@stephentoub stephentoub mentioned this pull request Jan 7, 2020
41 tasks
@stephentoub stephentoub added the tenet-performance Performance related issue label Jan 12, 2020
@stephentoub stephentoub added this to the 5.0 milestone Jan 12, 2020
@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.

5 participants