This is an automated email from the ASF dual-hosted git repository.
opwvhk pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/avro.git
The following commit(s) were added to refs/heads/main by this push:
new e932c9453 AVRO-3987 replace synchronized with immutable replacement
approach (#2900)
e932c9453 is described below
commit e932c9453be7b36e8874fe92edb3710beef4e47c
Author: Ashley Taylor <[email protected]>
AuthorDate: Sat Jun 1 03:52:55 2024 +1200
AVRO-3987 replace synchronized with immutable replacement approach (#2900)
---
.../src/main/java/org/apache/avro/reflect/ReflectData.java | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git
a/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java
b/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java
index 347490679..24173b9b2 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java
@@ -360,8 +360,8 @@ public class ReflectData extends SpecificData {
static class ClassAccessorData {
private final Class<?> clazz;
private final Map<String, FieldAccessor> byName = new HashMap<>();
- // getAccessorsFor is already synchronized, no need to wrap
- final Map<Schema, FieldAccessor[]> bySchema = new WeakHashMap<>();
+ // getAccessorsFor replaces this map with each modification
+ volatile Map<Schema, FieldAccessor[]> bySchema = new WeakHashMap<>();
private ClassAccessorData(Class<?> c) {
clazz = c;
@@ -379,12 +379,14 @@ public class ReflectData extends SpecificData {
* 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;
}
return result;
}