-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
SerializerCache: remove synchronization when only _sharedMap is accessed with a read call #4459
base: 2.18
Are you sure you want to change the base?
Conversation
pjfanning
commented
Mar 28, 2024
- there is a lot of synchronization in SerializerCache
- most of it may be necessary on the update side but on the read side, we can remove some
- we can follow up and replace the remaining synchronization later
Ok, please see my notes on #4442: I am not sure these are safe to remove. |
The code that I changed just reads values. I really don't think the existing synchronization helps these methods except in the unlikely event that they are called slightly after an update method in a different thread that locks and then blocks the read. This would only be by chance - there is no determinism. If the read methods here were doing something like this
That pattern benefits from locking. These plain reads do not benefit from locking imho. |
@pjfanning But synchronization does have wider scope so it is NOT just guarding reads. That's what I realized reading through the code: |
_sharedMap is created in the constructor and is final. This coding approach makes it fully safe to read without locking. The JVM basically guarantees that the constructor must complete before anything else can use the instance. Lazy initialization requires locking. Eager initialization in the constructor does not. There is more complicated locking in other methods in this class and that is why I've left them alone for now but imho, these simpler cases are safe to change. |
I am not worried about existence of
so that So why doesn't performance suck really bad under normal circumstances? Because there is the "read-only map" that is used instead of shared one for all access, once initial construction of serializers settles.
is what
and over time most (or perhaps all) access to serializers will go through these immutable, non-synchronized maps. |