Skip to content

Conversation

gerhardol
Copy link
Member

@gerhardol gerhardol commented Nov 19, 2023

Depends on #11240

Proposed changes

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:

  • For all regex with previous private static readonly Regex the [GeneratedRegex attribute was placed in the top, to simplify comparisons
  • Option RegexOptions.Compiled was manually removed
  • The default name MyRegex was changed to something meaningful
  • Some variables without any use was removed
  • A few minor adjustments and suggestions done.

Test methodology

  • Tests still run

Merge strategy

  • squash

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.

@ghost ghost assigned gerhardol Nov 19, 2023
@gerhardol gerhardol changed the title Feature/net8 regex SYSLIB1045 compile regex at compiletime Nov 19, 2023
@gerhardol gerhardol marked this pull request as draft November 19, 2023 18:54
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]+[^>]*>|<(\[^\]*\|'[^']*'|[^'\>])*>")]
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
[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)]

Copy link
Member Author

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...

@gerhardol gerhardol marked this pull request as ready for review November 26, 2023 08:45
@gerhardol
Copy link
Member Author

Rebased on master with the merged .net8. Squashed comments (and removed some not yet used GeneratedRegex for grep).
One open question is if [GeneratedRegex should be aligned and put first (as previous static readonly where that existed) or last (as when converted).

Copy link
Member

@RussKie RussKie left a comment

Choose a reason for hiding this comment

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

👍

@RussKie RussKie added the 📭 needs: author feedback More info/confirmation awaited from OP; issues typically get closed after 30 days of inactivity label Nov 26, 2023
@ghost ghost removed the 📭 needs: author feedback More info/confirmation awaited from OP; issues typically get closed after 30 days of inactivity label Nov 26, 2023
@gerhardol
Copy link
Member Author

Rebased, moved GeneratedRegex to start of class with other private (a few regex are public too)

Copy link
Member

@RussKie RussKie left a 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.

@RussKie RussKie added the 📭 needs: author feedback More info/confirmation awaited from OP; issues typically get closed after 30 days of inactivity label Nov 26, 2023
Regex with static strings can be precompiled to improve performance.
@ghost ghost removed the 📭 needs: author feedback More info/confirmation awaited from OP; issues typically get closed after 30 days of inactivity label Nov 27, 2023
@gerhardol
Copy link
Member Author

squashed and rebased
Fixed the tests. Just adding [GeneratedRegex] attribute changes the test results, not related to the actual test.

Will merge tomorrow unless there are protests

@RussKie RussKie merged commit ec9fa97 into gitextensions:master Nov 28, 2023
@ghost ghost added this to the vNext milestone Nov 28, 2023
@gerhardol gerhardol deleted the feature/net8-regex branch November 28, 2023 06:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants