Avoid using ConcurrentDictionary for channels with few methods#2597
Merged
JamesNK merged 7 commits intogrpc:masterfrom Feb 18, 2025
Merged
Avoid using ConcurrentDictionary for channels with few methods#2597JamesNK merged 7 commits intogrpc:masterfrom
JamesNK merged 7 commits intogrpc:masterfrom
Conversation
There was a problem hiding this comment.
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
Comments suppressed due to low confidence (1)
src/Grpc.Net.Client/Internal/ThreadSafeLookup.cs:24
- The comment should be corrected to 'Looking up a key in an array is as fast as a dictionary for small collections and uses much less memory.'
// Looking up a key in an array is as fast as a dictionary for small collections and uses much less memory. gfoidl reviewed Feb 17, 2025
Co-authored-by: Günther Foidl <gue@korporal.at>
Member Author
| @BrennanConroy Good feedback. I don't think the type would have broken and caused errors, but it could have gotten into a weird state (using the dictionary but still having some values in the array, duplicate values in the array). I've placed all updates in the lock. And added a comment about the value that's created. @gfoidl The main goal here is to optimize idle memory usage. Making the array only as big as it needs to be, plus some array allocations while a channel is first used is an ok tradeoff. |
gfoidl reviewed Feb 18, 2025
Co-authored-by: Günther Foidl <gue@korporal.at>
BrennanConroy approved these changes Feb 18, 2025
This was referenced Jul 29, 2025
Closed
Closed
This was referenced Mar 20, 2026
Bump the nuget-production-dependencies group with 23 updates PraveenKumarDova/opentelemetry-demo#176
Open
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.
GrpcChannelhas aConcurrentDictionaryto cache method details. However, if the channel never has more than a handful of methods thenConcurrentDictionaryuses a large amount of memory to store just a few instances.This PR adds a
ThreadSafeLookuptype that uses a replacing array for a small number of items and upgrades to a dictionary beyond a threashold.