Skip to content

Commit

Permalink
core: Optimize TypeSerializerCollection.get
Browse files Browse the repository at this point in the history
This was showing up quite heavily in some end user profiles
  • Loading branch information
zml2008 committed Aug 17, 2021
1 parent c4b641a commit 9f72f1c
Showing 1 changed file with 36 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
*/
package org.spongepowered.configurate.serialize;

import static io.leangen.geantyref.GenericTypeReflector.annotate;
import static io.leangen.geantyref.GenericTypeReflector.isSuperType;
import static java.util.Objects.requireNonNull;
import static org.spongepowered.configurate.util.Types.requireCompleteParameters;

import io.leangen.geantyref.GenericTypeReflector;
import io.leangen.geantyref.TypeToken;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.ObjectMapper;
import org.spongepowered.configurate.util.UnmodifiableCollections;
Expand Down Expand Up @@ -82,7 +82,7 @@ public final class TypeSerializerCollection {
}

private final @Nullable TypeSerializerCollection parent;
private final List<RegisteredSerializer> serializers;
final List<RegisteredSerializer> serializers;
private final Map<Type, TypeSerializer<?>> typeMatches = new ConcurrentHashMap<>();

private TypeSerializerCollection(final @Nullable TypeSerializerCollection parent, final List<RegisteredSerializer> serializers) {
Expand All @@ -106,7 +106,7 @@ private TypeSerializerCollection(final @Nullable TypeSerializerCollection parent
@SuppressWarnings("unchecked")
public <T> @Nullable TypeSerializer<T> get(final TypeToken<T> token) {
requireNonNull(token, "type");
return (TypeSerializer<T>) get(token.getType());
return (TypeSerializer<T>) get0(token.getType());
}

/**
Expand Down Expand Up @@ -143,19 +143,26 @@ private TypeSerializerCollection(final @Nullable TypeSerializerCollection parent
* serializer is found
* @since 4.0.0
*/
public @Nullable TypeSerializer<?> get(Type type) {
type = GenericTypeReflector.toCanonicalBoxed(annotate(requireNonNull(type, "type"))).getType();
@Nullable TypeSerializer<?> serial = this.typeMatches.computeIfAbsent(type, param -> {
public @Nullable TypeSerializer<?> get(final Type type) {
return this.get0(GenericTypeReflector.box(type));
}

private @Nullable TypeSerializer<?> get0(final Type canonical) {
@Nullable TypeSerializer<?> serial = this.typeMatches.computeIfAbsent(canonical, param -> {
for (RegisteredSerializer ent : this.serializers) {
if (ent.predicate.test(param)) {
return ent.serializer;
}
}
return null;
return NoOp.INSTANCE;
});

if (serial == NoOp.INSTANCE) {
serial = null;
}

if (serial == null && this.parent != null) {
serial = this.parent.get(type);
serial = this.parent.get0(canonical);
}
return serial;
}
Expand Down Expand Up @@ -432,10 +439,10 @@ public TypeSerializerCollection build() {
}
}

private static final class RegisteredSerializer {
static final class RegisteredSerializer {

private final Predicate<Type> predicate;
private final TypeSerializer<?> serializer;
final Predicate<Type> predicate;
final TypeSerializer<?> serializer;

private RegisteredSerializer(final Predicate<Type> predicate, final TypeSerializer<?> serializer) {
this.predicate = predicate;
Expand All @@ -444,4 +451,22 @@ private RegisteredSerializer(final Predicate<Type> predicate, final TypeSerializ

}

static final class NoOp implements TypeSerializer<Void> {

static final NoOp INSTANCE = new NoOp();

private NoOp() {
}

@Override
public Void deserialize(final Type type, final ConfigurationNode node) throws SerializationException {
throw new UnsupportedOperationException("this is a placeholder for null, should not be called directly");
}

@Override
public void serialize(final Type type, @Nullable final Void obj, final ConfigurationNode node) throws SerializationException {
throw new UnsupportedOperationException("this is a placeholder for null, should not be called directly");
}
}

}

0 comments on commit 9f72f1c

Please sign in to comment.