SerializerCache: remove synchronization when only _sharedMap is accessed with a read call#4459
SerializerCache: remove synchronization when only _sharedMap is accessed with a read call#4459pjfanning wants to merge 3 commits intoFasterXML:2.18from
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. |