perf(serialization): optimize ConcurrentObjectPool with ThreadStatic storage#10073
Open
ReubenBond wants to merge 1 commit into
Open
perf(serialization): optimize ConcurrentObjectPool with ThreadStatic storage#10073ReubenBond wants to merge 1 commit into
ReubenBond wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes Orleans.Serialization’s ConcurrentObjectPool by replacing ThreadLocal<T> storage with [ThreadStatic] storage to reduce overhead in hot-path pooling used by invocation/serialization components.
Changes:
- Replaced
ThreadLocal<Stack<T>>with a[ThreadStatic]per-thread array of stacks. - Added per-pool-instance indexing (
_poolId) to avoid state sharing across pools which may use different policies.
Show a summary per file
| File | Description |
|---|---|
src/Orleans.Serialization/Invocation/Pools/ConcurrentObjectPool.cs |
Reworks per-thread pool storage to use [ThreadStatic] stacks indexed by pool instance id. |
Copilot's findings
- Files reviewed: 1/1 changed files
- Comments generated: 1
Comment on lines
+19
to
+43
| private static int NextPoolId = -1; | ||
|
|
||
| private readonly int _poolId = Interlocked.Increment(ref NextPoolId); | ||
| private readonly TPoolPolicy _policy; | ||
|
|
||
| public ConcurrentObjectPool(TPoolPolicy policy) => _policy = policy; | ||
|
|
||
| public int MaxPoolSize { get; set; } = int.MaxValue; | ||
|
|
||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| private Stack<T> GetStack() | ||
| { | ||
| var poolId = _poolId; | ||
| var stacks = PerThreadStack.Stacks; | ||
| if (stacks is null) | ||
| { | ||
| stacks = PerThreadStack.Stacks = new Stack<T>[poolId + 1]; | ||
| } | ||
| else if ((uint)poolId >= (uint)stacks.Length) | ||
| { | ||
| Array.Resize(ref stacks, Math.Max(poolId + 1, stacks.Length * 2)); | ||
| PerThreadStack.Stacks = stacks; | ||
| } | ||
|
|
||
| return stacks[poolId] ??= new(); |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
5a5c7bc to
906aef7
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Replaces
ThreadLocal<T>with[ThreadStatic]storage inConcurrentObjectPool.Microsoft Reviewers: Open in CodeFlow