Skip to content

CSHARP-2450: Performance: Reduced lock contention in BsonSerializer.LookupActualType #347

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

Conversation

dnickless
Copy link
Contributor

BsonSerializer.LookupActualType is on the hot path during deserialization and has proven to be a nasty bottleneck in my setup:

image

I managed to remove the need for the ReaderWriterLockSlim by switching to a (unfortunately not generic) Hashtable type instead of a HashSet. Hashtable is safe for concurrent reads (as per https://docs.microsoft.com/en-us/dotnet/api/system.collections.hashtable?redirectedfrom=MSDN&view=netframework-4.7.2#thread-safety) and the amount of casting introduced is very small.

The HashSet instances that are tracked inside the Hashtable get replaced instead of updated upon a write which allows consumers to safely iterate over them.

@IanWhalen
Copy link

Hi @dnickless, thanks for opening this pull request, and my apologies for the delay in response here. The MongoDB .NET team relies primarily on our JIRA project to track and prioritize our work.

In order to continue the conversation, can you please open a ticket at the above link so we can discuss your proposal there?

@dnickless dnickless changed the title Performance: Reduced lock contention in BsonSerializer.LookupActualType CSHARP-2450: Performance: Reduced lock contention in BsonSerializer.LookupActualType Dec 11, 2018
@dnickless dnickless force-pushed the BsonSerializer_ReduceLockContention branch from 760c7ec to 4efb3a9 Compare February 21, 2019 22:21
@dnickless
Copy link
Contributor Author

Hi @IanWhalen, kindly see the updated JIRA ticket. This change appears to indicate some massive (!) improvement.

@IanWhalen
Copy link

Cool, thanks! We're still working through a lot of effort to get the driver ready for release concurrent to 4.2 and all of the new features that will be available there, but this is still on our radar when we get to handling PRs and other subsequent work.

…is on the hot path during deserialization by switching to the (unfortunately not generic) Hashtable type instead of a HashSet<T>. Hashtable is safe for concurrent reads (as per https://docs.microsoft.com/en-us/dotnet/api/system.collections.hashtable?redirectedfrom=MSDN&view=netframework-4.7.2#thread-safety) and the amount of casting introduced is very small. The HashSet<T> instances that are tracked inside the Hashtable get replaced instead of updated upon a write which allows consumers to safely iterate over them.
@dnickless dnickless force-pushed the BsonSerializer_ReduceLockContention branch from 4efb3a9 to 42a52e4 Compare February 28, 2019 19:51
@BorisDog BorisDog closed this Dec 18, 2020
@dnickless
Copy link
Contributor Author

Kindly have a look at my second attempt which isn't really better than this one here in my opinion but certainly less drastic and still effective: #433

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants