Skip to content

Commit

Permalink
AVRO-3987 replace synchronized with immutable replacement approach (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ashley-taylor committed May 31, 2024
1 parent 7becb63 commit e932c94
Showing 1 changed file with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,8 @@ protected ClassAccessorData computeValue(Class<?> c) {
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;
Expand All @@ -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;
}
return result;
}
Expand Down

0 comments on commit e932c94

Please sign in to comment.