KalleOlaviNiemitalo commented on code in PR #2900:
URL: https://github.com/apache/avro/pull/2900#discussion_r1618425234


##########
lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java:
##########
@@ -379,12 +379,14 @@ private ClassAccessorData(Class<?> c) {
      * Return the field accessors as an array, indexed by the field index of 
the
      * given schema.
      */
-    private synchronized FieldAccessor[] getAccessorsFor(Schema schema) {
-      // if synchronized is removed from this method, adjust bySchema 
appropriately
+    private FieldAccessor[] getAccessorsFor(Schema schema) {
+      // to avoid synchronization, we replace the map for each modification
       FieldAccessor[] result = bySchema.get(schema);
       if (result == null) {
         result = createAccessorsFor(schema);
+        Map<Schema, FieldAccessor[]> bySchema = new 
WeakHashMap<>(this.bySchema);
         bySchema.put(schema, result);
+        this.bySchema = bySchema;

Review Comment:
   I'm not really a Java programmer but the `this.bySchema = bySchema` 
assignment seems OK, assuming that the `volatile` causes a memory barrier that 
ensures the effects of `bySchema.put(schema, result)` will be seen by other 
threads that read `this.bySchema` after the assignment.
   
   However, I wonder about `bySchema.get(schema)`.  Is WeakHashMap.get formally 
documented to be safe for concurrent calls?  Each time WeakHashMap.get is 
called, WeakHashMap.expungeStaleEntries can delete some entries, and although 
WeakHashMap.expungeStaleEntries synchronizes those deletions against each 
other, it doesn't seem to synchronize them against any reads that 
WeakHashMap.get may be doing on other threads.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to