-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-2450: Reduce locking costs in BsonSerializer #482
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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.
Nit: Consider using GetOrAdd instead of the if-ContainsKey-assignment sequence as this will cause two lookups.
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 the feeback!
GetOrAdd(TKey key, Func<TKey,TValue> valueFactory)
internally performs multiple lookups. First to determine if the key is present and then after executingvalueFactory
but prior to inserting into the collection. If the value is not present in the collection,GetOrAdd
will perform two lookups and the currentContainsKey/[]=value
does the same. If the value is already present, both only perform a single lookup. Testing out both implementations I see the same performance numbers.I will add a comment to
__typesWithRegisteredKnownTypes[nominalType] = nominalType;
noting that it must be performed as the last step to ensure that other threads don't see partially initialized types.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.
You're right about the actual lookups, of course. I was thinking about the
GetHashCode()
calls of which you'd be saving one but, in this case, they're not costly so let's ignore that. It's just a standard thing that I constantly tell my devs: "Always useGetOrAdd
instead of the other sequence!"... ;)In fact, I had originally been coming from suggesting the use of
TryAdd()
which should have saved the lookups but that didn't work as there's additional work to be done inside theif
branch and it needs to happen before the dictionary insert... Anyway, that code is very ok the way it is now, I suppose.