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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace a few Regex static method calls #728

Merged
merged 3 commits into from Apr 30, 2023

Conversation

stephentoub
Copy link
Member

Motivation and Context

Avoid use of Regex's static methods.

Description

The static methods on Regex are convenient, but they're also a bit of a performance trap. Parsing regexes is fairly expensive, so that static methods employ a MRU cache to avoid reparsing on every use, but a) that cache has cost to access and b) the cache is very small by default. The net result is usage of other static Regex methods elsewhere in the app can make the use of the static methods in SK much more expensive. Regex is thread-safe, and it's better to just store the necessary instances into readonly static fields and then use the instance methods on those.

Note that there is an analyzer that flags these, but it's only available when targeting .NET 7+, where it recommends (and provides a fixer) for turning them into source-generated regexes using [GeneratedRegex(...)].

Contribution Checklist

@github-actions github-actions bot added .NET Issue or Pull requests regarding .NET code kernel Issues or pull requests impacting the core kernel kernel.core labels Apr 28, 2023
The static methods on Regex are convenient, but they're also a bit of a performance trap.  Parsing regexes is fairly expensive, so that static methods employ a MRU cache to avoid reparsing on every use, but a) that cache has cost to access and b) the cache is very small by default.  The net result is usage of other static Regex methods elsewhere in the app can make the use of the static methods in SK much more expensive. Regex is thread-safe, and it's better to just store the necessary instances into readonly static fields and then use the instance methods on those.
@lemillermicrosoft lemillermicrosoft added the PR: ready to merge PR has been approved by all reviewers, and is ready to merge. label Apr 28, 2023
@dluc dluc enabled auto-merge (squash) April 30, 2023 19:22
@dluc dluc merged commit a997257 into microsoft:main Apr 30, 2023
11 checks passed
dluc pushed a commit to dluc/semantic-kernel that referenced this pull request Apr 30, 2023
### Motivation and Context

Avoid use of Regex's static methods.

### Description

The static methods on Regex are convenient, but they're also a bit of a
performance trap. Parsing regexes is fairly expensive, so that static
methods employ a MRU cache to avoid reparsing on every use, but a) that
cache has cost to access and b) the cache is very small by default. The
net result is usage of other static Regex methods elsewhere in the app
can make the use of the static methods in SK much more expensive. Regex
is thread-safe, and it's better to just store the necessary instances
into readonly static fields and then use the instance methods on those.

Note that there is an analyzer that flags these, but it's only available
when targeting .NET 7+, where it recommends (and provides a fixer) for
turning them into source-generated regexes using [GeneratedRegex(...)].
codebrain pushed a commit to searchpioneer/semantic-kernel that referenced this pull request May 16, 2023
### Motivation and Context

Avoid use of Regex's static methods.

### Description

The static methods on Regex are convenient, but they're also a bit of a
performance trap. Parsing regexes is fairly expensive, so that static
methods employ a MRU cache to avoid reparsing on every use, but a) that
cache has cost to access and b) the cache is very small by default. The
net result is usage of other static Regex methods elsewhere in the app
can make the use of the static methods in SK much more expensive. Regex
is thread-safe, and it's better to just store the necessary instances
into readonly static fields and then use the instance methods on those.

Note that there is an analyzer that flags these, but it's only available
when targeting .NET 7+, where it recommends (and provides a fixer) for
turning them into source-generated regexes using [GeneratedRegex(...)].
dehoward pushed a commit to lemillermicrosoft/semantic-kernel that referenced this pull request Jun 1, 2023
### Motivation and Context

Avoid use of Regex's static methods.

### Description

The static methods on Regex are convenient, but they're also a bit of a
performance trap. Parsing regexes is fairly expensive, so that static
methods employ a MRU cache to avoid reparsing on every use, but a) that
cache has cost to access and b) the cache is very small by default. The
net result is usage of other static Regex methods elsewhere in the app
can make the use of the static methods in SK much more expensive. Regex
is thread-safe, and it's better to just store the necessary instances
into readonly static fields and then use the instance methods on those.

Note that there is an analyzer that flags these, but it's only available
when targeting .NET 7+, where it recommends (and provides a fixer) for
turning them into source-generated regexes using [GeneratedRegex(...)].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kernel Issues or pull requests impacting the core kernel .NET Issue or Pull requests regarding .NET code PR: ready to merge PR has been approved by all reviewers, and is ready to merge.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants