Skip to content

Commit

Permalink
Fix #265. Don't invoke getTypeParameters and getComponentType on each…
Browse files Browse the repository at this point in the history
… call of setGenerics(). Pre-compute and cache them instead whenever it is possible.
  • Loading branch information
romix committed Nov 18, 2014
1 parent 000d88d commit 143c097
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/com/esotericsoftware/kryo/serializers/FieldSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public class FieldSerializer<T> extends Serializer<T> implements Comparator<Fiel
final Kryo kryo;
final Class type;
/** type variables declared for this type */
final private TypeVariable[] typeParameters;
final TypeVariable[] typeParameters;
final Class componentType;
private CachedField[] fields = new CachedField[0];
private CachedField[] transientFields = new CachedField[0];
protected HashSet<CachedField> removedFields = new HashSet();
Expand Down Expand Up @@ -122,6 +123,10 @@ public FieldSerializer (Kryo kryo, Class type) {
this.kryo = kryo;
this.type = type;
this.typeParameters = type.getTypeParameters();
if (this.typeParameters == null || this.typeParameters.length == 0)
this.componentType = type.getComponentType();
else
this.componentType = null;
this.useAsmEnabled = kryo.getAsmEnabled();
if (!this.useAsmEnabled && !unsafeAvailable) {
this.useAsmEnabled = true;
Expand All @@ -138,6 +143,10 @@ public FieldSerializer (Kryo kryo, Class type, Class[] generics) {
this.type = type;
this.generics = generics;
this.typeParameters = type.getTypeParameters();
if (this.typeParameters == null || this.typeParameters.length == 0)
this.componentType = type.getComponentType();
else
this.componentType = null;
this.useAsmEnabled = kryo.getAsmEnabled();
if (!this.useAsmEnabled && !unsafeAvailable) {
this.useAsmEnabled = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,15 @@ Generics buildGenericsScope (Class clazz, Class[] generics) {
TypeVariable[] typeParams = null;

while (typ != null) {
typeParams = typ.getTypeParameters();
if (typ == this.serializer.type)
typeParams = this.serializer.typeParameters;
else
typeParams = typ.getTypeParameters();
if (typeParams == null || typeParams.length == 0) {
typ = typ.getComponentType();
if (typ == this.serializer.type)
typ = this.serializer.componentType;
else
typ = typ.getComponentType();
} else
break;
}
Expand Down

0 comments on commit 143c097

Please sign in to comment.