-
Notifications
You must be signed in to change notification settings - Fork 5k
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
base: main
Are you sure you want to change the base?
Conversation
Tagging subscribers to this area: @dotnet/area-system-text-regularexpressions |
This comment was marked as resolved.
This comment was marked as resolved.
…once (usually under contention)
118b9ac
to
e641312
Compare
|
||
namespace System.Text.RegularExpressions | ||
{ | ||
internal sealed class RegexRunnerPool |
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.
is there any value in attmepting to use an existing pool such as something out of Microsoft.Extensions.ObjectPool
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.
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!
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.
thank you for working on it.
|
||
public void Return(RegexRunner runner) | ||
{ | ||
if (Interlocked.Increment(ref _storedCount) <= s_maxCapacity) |
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.
if you keep this code you could add some Debug.Assert that it wasn't already returned
Just a proof of concept for now, will update description if it pans out.