Skip to content

Commit

Permalink
#774 Do not re-use serializer instances for Compatible and Tagged
Browse files Browse the repository at this point in the history
… serializers in `ReflectField`
  • Loading branch information
theigl committed Nov 4, 2020
1 parent 5e29ca3 commit b5af092
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
public class CompatibleFieldSerializer<T> extends FieldSerializer<T> {
private static final int binarySearchThreshold = 32;

private CompatibleFieldSerializerConfig config;
private final CompatibleFieldSerializerConfig config;

public CompatibleFieldSerializer (Kryo kryo, Class type) {
this(kryo, type, new CompatibleFieldSerializerConfig());
Expand All @@ -56,6 +56,7 @@ public CompatibleFieldSerializer (Kryo kryo, Class type) {
public CompatibleFieldSerializer (Kryo kryo, Class type, CompatibleFieldSerializerConfig config) {
super(kryo, type, config);
this.config = config;
setReuseSerializers(false);
}

@Override
Expand Down Expand Up @@ -242,11 +243,6 @@ else if (compare > 0)
return fields;
}

@Override
boolean supportsReuse() {
return !config.readUnknownFieldData;
}

public CompatibleFieldSerializerConfig getCompatibleFieldSerializerConfig () {
return config;
}
Expand Down
11 changes: 8 additions & 3 deletions src/com/esotericsoftware/kryo/serializers/FieldSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class FieldSerializer<T> extends Serializer<T> {
final FieldSerializerConfig config;
final CachedFields cachedFields;
private final GenericsHierarchy genericsHierarchy;
private boolean reuseSerializers = true;

public FieldSerializer (Kryo kryo, Class type) {
this(kryo, type, new FieldSerializerConfig());
Expand Down Expand Up @@ -211,9 +212,13 @@ protected T createCopy (Kryo kryo, T original) {
return (T)kryo.newInstance(original.getClass());
}

/** If true, the serializer supports re-use of serializers for all instances of a field */
boolean supportsReuse() {
return true;
boolean getReuseSerializers() {
return reuseSerializers;
}

/** If true, {@link ReflectField}s re-use serializers for all instances of a field if the concrete value class is known */
void setReuseSerializers(boolean reuseSerializers) {
this.reuseSerializers = reuseSerializers;
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/com/esotericsoftware/kryo/serializers/ReflectField.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void write (Output output, Object object) {
if (serializer == null) {
serializer = kryo.getSerializer(concreteType);
// The concrete type of the field is known, always use the same serializer.
if (valueClass != null && this.fieldSerializer.supportsReuse()) this.serializer = serializer;
if (valueClass != null && this.fieldSerializer.getReuseSerializers()) this.serializer = serializer;
}
kryo.getGenerics().pushGenericType(genericType);
if (canBeNull) {
Expand Down Expand Up @@ -121,7 +121,7 @@ public void read (Input input, Object object) {
if (serializer == null) {
serializer = kryo.getSerializer(concreteType);
// The concrete type of the field is known, always use the same serializer.
if (valueClass != null && this.fieldSerializer.supportsReuse()) this.serializer = serializer;
if (valueClass != null && this.fieldSerializer.getReuseSerializers()) this.serializer = serializer;
}
kryo.getGenerics().pushGenericType(genericType);
if (canBeNull)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public TaggedFieldSerializer (Kryo kryo, Class type, TaggedFieldSerializerConfig
super(kryo, type, config);
this.config = config;
setAcceptsNull(true);
setReuseSerializers(false);
}

@Override
Expand Down Expand Up @@ -242,11 +243,6 @@ public T read (Kryo kryo, Input input, Class<? extends T> type) {
return object;
}

@Override
boolean supportsReuse() {
return !config.readUnknownTagData;
}

public TaggedFieldSerializerConfig getTaggedFieldSerializerConfig () {
return config;
}
Expand Down

0 comments on commit b5af092

Please sign in to comment.