[Validation Approach] Improve ConcurrentStack.TryPopRange performance under contention. - #133381
Open
aw0lid wants to merge 1 commit into
Open
[Validation Approach] Improve ConcurrentStack.TryPopRange performance under contention.#133381aw0lid wants to merge 1 commit into
aw0lid wants to merge 1 commit into
Conversation
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-collections |
aw0lid
marked this pull request as ready for review
September 8, 2026 10:03
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
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.
Overview
This PR is opened as a validation PR to investigate potential approaches for improving
ConcurrentStack<T>.TryPopRangeperformance under contention, as described in #100083.The current implementation attempts to pop the requested number of items as a single batch. Under contention, repeated CAS failures can cause the same large portion of the stack to be traversed repeatedly, leading to a significant performance degradation.
Approach 1: Adaptive Batch Sizing
The first approach explored in this PR is adaptive batch sizing.
The batch size starts at the requested count.
If the CAS fails 30 times consecutively, the batch size is reduced by half and the operation is retried. After a successful CAS, the popped batch is copied to the destination array and the remaining items are processed using the current batch size.
If contention increases again and the CAS fails another 30 times, the batch size is reduced again.
For example:
The goal of this approach is to avoid repeatedly traversing a large number of nodes when the stack is highly contended, while still allowing large batches when contention is low.
Initial results
Show benchmark code
The most significant improvement can be seen under high contention, where the benchmark goes from ~58 seconds to ~476 ms with 8 threads, while allocations drop from ~15.8 GB to ~268 MB.
These results are encouraging and show that adaptive batch sizing can significantly reduce the performance degradation under contention.
Further approaches may be explored in this PR to evaluate different strategies and trade-offs.
Open Question
One important aspect that still needs to be evaluated is whether performing multiple successful CAS operations within a single
TryPopRangeinvocation is compatible with the atomicity semantics expected from the API.Further validation and discussion are needed before considering any approach as a final implementation.