Skip to content

Introduce RegexRunnerPool to reuse multiple RegexRunner instances at once under contention #116184

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

neon-sunset
Copy link
Contributor

Just a proof of concept for now, will update description if it pans out.

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label May 31, 2025
@neon-sunset neon-sunset changed the title Introduce RegexRunnerPool to reuse multiple RegexRunner instances at once (usually under contention) Introduce RegexRunnerPool to reuse multiple RegexRunner instances at once under contention May 31, 2025
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-regularexpressions
See info in area-owners.md if you want to be subscribed.

@neon-sunset

This comment was marked as resolved.


namespace System.Text.RegularExpressions
{
internal sealed class RegexRunnerPool
Copy link
Member

Choose a reason for hiding this comment

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

is there any value in attmepting to use an existing pool such as something out of Microsoft.Extensions.ObjectPool

Copy link
Contributor Author

@neon-sunset neon-sunset Jun 2, 2025

Choose a reason for hiding this comment

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

Adding a queue/stack pool to existing cached runner in a field essentially mimics DefaultObjectPool<T>: https://github.com/dotnet/aspnetcore/blob/main/src/ObjectPool/src/DefaultObjectPool.cs

However, I didn't see an easy way to bring it over and ObjectPool opts for using ConcurrentQueue<T> instead, which incurs >800B in allocations compared to ConcurrentStack<T> which is 56B to allocate + add one element. I wanted to keep memory usage to a minimum and avoid increasing assembly size more than necessary - regex patterns may still be fairly shortlived even if shared by multiple threads, or there can be thousands of them kept for a long time (which matches the workload of the application I found the initial issue in). This is still just an attempt - it needs a less noisy benchmark and I need to look into test failures first. Thank you for reviewing this!

Copy link
Member

Choose a reason for hiding this comment

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

thank you for working on it.


public void Return(RegexRunner runner)
{
if (Interlocked.Increment(ref _storedCount) <= s_maxCapacity)
Copy link
Member

Choose a reason for hiding this comment

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

if you keep this code you could add some Debug.Assert that it wasn't already returned

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Text.RegularExpressions community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants