perf(runtime): add safe pooled CallbackData reuse#10067
Draft
ReubenBond wants to merge 1 commit into
Draft
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds a safe, pooled reuse mechanism for CallbackData to reduce allocations in Orleans runtime/client request tracking, using ref-counted ownership + lease-based access to avoid reuse races across response, cancellation, timeout, and failover paths.
Changes:
- Introduces
CallbackDataPool(thread-local pooling) and adds ref-count/lease lifetime management toCallbackDataviaCallbackDataOwner/CallbackDataLease. - Updates
OutsideRuntimeClientandInsideRuntimeClientcallback registration/removal and response/status/timeout/failover paths to use leases and return callbacks to the pool safely. - Adjusts
InsideRuntimeClient.Invokearound invokable disposal (currently leaving disposal out of the method body).
Show a summary per file
| File | Description |
|---|---|
| src/Orleans.Runtime/Core/InsideRuntimeClient.cs | Switches callback dictionary values to pooled CallbackDataOwner with lease-based access in response/status/failover/expiry paths. |
| src/Orleans.Core/Runtime/OutsideRuntimeClient.cs | Same pooled callback owner/lease approach for the external client runtime callback lifecycle. |
| src/Orleans.Core/Runtime/CallbackDataPool.cs | Adds thread-local CallbackData pooling and centralized return/reset via ref counting. |
| src/Orleans.Core/Runtime/CallbackData.cs | Adds pooling support (Initialize/Reset), ref counting, correlation validation for status updates, and introduces owner/lease structs. |
Copilot's findings
- Files reviewed: 4/4 changed files
- Comments generated: 2
Comment on lines
315
to
321
| response = await invokable.Invoke(); | ||
| response = this.responseCopier.Copy(response); | ||
| } | ||
|
|
||
| invokable.Dispose(); | ||
| // Note: invokable disposal happens at the end of Invoke to return it to its pool | ||
| break; | ||
| } |
Comment on lines
+10
to
+57
| internal static class CallbackDataPool | ||
| { | ||
| private static readonly ThreadLocal<Stack<CallbackData>> _callbacks = new(() => new()); | ||
|
|
||
| /// <summary> | ||
| /// The maximum number of callbacks to keep per thread. | ||
| /// </summary> | ||
| public static int MaxPoolSizePerThread { get; set; } = 128; | ||
|
|
||
| /// <summary> | ||
| /// Gets a callback from the pool, or creates a new one if the pool is empty. | ||
| /// </summary> | ||
| public static CallbackData Get() | ||
| { | ||
| var stack = _callbacks.Value!; | ||
| if (stack.TryPop(out var callback)) | ||
| { | ||
| return callback; | ||
| } | ||
|
|
||
| return new CallbackData(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns a callback to the pool via the reference counting system. | ||
| /// This decrements the owner's reference count. The callback will be returned | ||
| /// to the pool when all leases have been released. | ||
| /// </summary> | ||
| /// <param name="owner">The owner of the callback.</param> | ||
| public static void Return(CallbackDataOwner owner) | ||
| { | ||
| owner.Release(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Internal method called by <see cref="CallbackData.ReleaseLease"/> when ref count reaches zero. | ||
| /// Actually returns the callback to the pool after resetting it. | ||
| /// </summary> | ||
| internal static void ReturnCore(CallbackData callback) | ||
| { | ||
| callback.Reset(); | ||
|
|
||
| var stack = _callbacks.Value!; | ||
| if (stack.Count < MaxPoolSizePerThread) | ||
| { | ||
| stack.Push(callback); | ||
| } | ||
| } |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
59f1538 to
aef33c8
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.
Summary
CallbackDatareuse viaCallbackDataPool,CallbackDataOwner, and lease-based lifetime management.Validation
git diff --checkdotnet build src\Orleans.Core\Orleans.Core.csproj -mdotnet build src\Orleans.Runtime\Orleans.Runtime.csproj -mgh pr checks 10067 --repo dotnet/orleans(62 checks passing)Notes
Microsoft Reviewers: Open in CodeFlow