Three examples of Regular Expression performance improvements in .NET 7

Stephens Toubs performance articles on devblogs.microsoft.com are always something I look forward to. In May 2022 he wrote one covering the awesome Regular Expression related performance improvements coming in .NET 7.

The article is a little tough to consume (due to the subject matter not Stephens writing) so I’ve pulled out three quick examples of Regex improvements from .NET 7 below. I do recommend you read the article when you get a chance though 👍…

Fixed-Distance set RegEx performance improvements in .NET 7

Rather than matching only at the start of a pattern, .NET 7 is now able to compute sets of characters that can exist at fixed-distance offsets from the beginning of the pattern.

In the example below which searches for United States social security numbers instead of only looking for \d, which can have close to 370 matches, .NET 7 can also look for \d when it’s a fixed amount of characters away from – and it can then pick the one it expects will yield the fastest search. We can see this yielded a close to 11x faster result over .NET 6 on my machine.

Click on the image for a larger view in a new tab…

Greedy and Lazy loop Regular Expression performance improvements in .NET 7

This test is silly but representative and has amazing performance improvements. It involved removing unnecessary match attempts by also applying a ‘bumpalong’ optimization which was previously applied to atomic loops to greedy and lazy loops too so the RegEx engine won’t have to redo the same work over and over…

Click on the image for a larger view in a new window…

Case-insensitive matching Regular Expression improvements in .NET 7

.NET 7 no longer implements RegexOptions.IgnoreCase by calling ToLower on each character in the pattern and each character in the input. Instead, all casing-related work is done when the Regex is constructed when .NET 7 will use a casing table to determine what other characters input characters should be considered equivalent too.

The results of this Regular Expression optimization are below. Click on the image for a larger view in a new window…

Leave a Reply

Your email address will not be published.