-
Notifications
You must be signed in to change notification settings - Fork 5.1k
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...ries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexRunnerPool.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Concurrent; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Threading; | ||
|
||
namespace System.Text.RegularExpressions | ||
{ | ||
internal sealed class RegexRunnerPool | ||
{ | ||
private static readonly int s_maxCapacity = Math.Max(4, Environment.ProcessorCount); | ||
|
||
private ConcurrentStack<RegexRunner> _storage = new(); | ||
private int _storedCount; | ||
|
||
public bool TryGet([NotNullWhen(true)] out RegexRunner? runner) | ||
{ | ||
if (_storage.TryPop(out runner)) | ||
{ | ||
Interlocked.Decrement(ref _storedCount); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
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 commentThe 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 |
||
{ | ||
_storage.Push(runner); | ||
} | ||
else | ||
{ | ||
Interlocked.Decrement(ref _storedCount); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Uh oh!
There was an error while loading. Please reload this page.
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.csHowever, 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 toConcurrentStack<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.