-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
SYSLIB1045 compile regex at compiletime #11371
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
Conversation
private readonly Regex _htmlRegex = new(@"</[c-g\d]+>|</[i-o\d]+>|</[a\d]+>|</[q-z\d]+>|<[cg]+[^>]*>|<[i-o]+[^>]*>|<[q-z]+[^>]*>|<[a]+[^>]*>|<(\[^\]*\|'[^']*'|[^'\>])*>", RegexOptions.IgnoreCase & RegexOptions.Compiled); | ||
[GeneratedRegex(@"^\d")] | ||
private static partial Regex DigitRegex(); | ||
[GeneratedRegex(@"</[c-g\d]+>|</[i-o\d]+>|</[a\d]+>|</[q-z\d]+>|<[cg]+[^>]*>|<[i-o]+[^>]*>|<[q-z]+[^>]*>|<[a]+[^>]*>|<(\[^\]*\|'[^']*'|[^'\>])*>")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[GeneratedRegex(@"</[c-g\d]+>|</[i-o\d]+>|</[a\d]+>|</[q-z\d]+>|<[cg]+[^>]*>|<[i-o]+[^>]*>|<[q-z]+[^>]*>|<[a]+[^>]*>|<(\[^\]*\|'[^']*'|[^'\>])*>")] | |
[GeneratedRegex(@"</[c-g\d]+>|</[i-o\d]+>|</[a\d]+>|</[q-z\d]+>|<[cg]+[^>]*>|<[i-o]+[^>]*>|<[q-z]+[^>]*>|<[a]+[^>]*>|<(\[^\]*\|'[^']*'|[^'\>])*>", RegexOptions.IgnoreCase)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This changes the behavior though: &
But probably what was intended...
Plugins/BuildServerIntegration/AzureDevOpsIntegration/Settings/ProjectUrlHelper.cs
Outdated
Show resolved
Hide resolved
Plugins/BuildServerIntegration/AzureDevOpsIntegration/Settings/ProjectUrlHelper.cs
Outdated
Show resolved
Hide resolved
Plugins/BuildServerIntegration/AzureDevOpsIntegration/Settings/ProjectUrlHelper.cs
Outdated
Show resolved
Hide resolved
e99ad8f
to
5af45e6
Compare
Rebased on master with the merged .net8. Squashed comments (and removed some not yet used GeneratedRegex for grep). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
5af45e6
to
9204154
Compare
Rebased, moved GeneratedRegex to start of class with other private (a few regex are public too) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like some of the Verify snapshots need to be updated due to callstack changes.
Regex with static strings can be precompiled to improve performance.
9204154
to
2d58d49
Compare
squashed and rebased Will merge tomorrow unless there are protests |
Depends on #11240
Proposed changes
Enable C# 12
https://devblogs.microsoft.com/dotnet/regular-expression-improvements-in-dotnet-7/#source-generation
Compile all static regex at compile time instead of at runtime.
See also #11370 that refers to https://learn.microsoft.com/en-us/dotnet/standard/base-types/best-practices-regex#regular-expressions-compiled-to-an-assembly
Before .NET7 either interpreted that executes slightly slower or compiled that slows down starting was used.
For .NET7 the recommended use is to compile regex at compile time, with both advantages.
This PR implements the analyzer suggestion with a few manual tweaks:
private static readonly Regex
the[GeneratedRegex
attribute was placed in the top, to simplify comparisonsTest methodology
Merge strategy
I agree that the maintainer squash merge this PR (if the commit message is clear).
✒️ I contribute this code under The Developer Certificate of Origin.