fix(performance): optimize ModuleEnvironmentSandbox.getSubTypeIdentifier
Always use the class's fully-qualified Java class name as its type identifier, as ReflectionsSandbox does.
The previous implementation led to calling loadClass on *all* of Component's subclasses every time the serializer asked for this type identifier. We encountered scalability problems with that in
-/*
- * Copyright 2019 MovingBlocks
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
+// Copyright 2020 The Terasology Foundation
+// SPDX-License-Identifier: Apache-2.0
package org.terasology.persistence.typeHandling.reflection;
-import com.google.common.base.Preconditions;
import org.terasology.engine.SimpleUri;
import org.terasology.engine.module.ModuleManager;
import org.terasology.module.ModuleEnvironment;
@@ -105,28 +91,9 @@ private boolean doesSubclassMatch(Class<?> subclass, String subTypeIdentifier) {
@Override
public <T> String getSubTypeIdentifier(Class<? extends T> subType, Class<T> baseType) {
- String subTypeUri = getTypeUri(subType);
-
- if (getModuleProviding(baseType) == null) {
return subType.getName();
}
- long subTypesWithSameUri = typeRegistry.getSubtypesOf(baseType).stream()
- .map(this::getTypeUri)
- .filter(subTypeUri::equals)
- .count();
-
- Preconditions.checkArgument(subTypesWithSameUri > 0,
- "Subtype " + subType + " was not found in the module environment");
-
- if (subTypesWithSameUri > 1) {
- // More than one subType with same SimpleUri, use fully qualified name
- return subType.getName();
- }
-
- return subTypeUri.toString();
- }
-
@Override
public <T> boolean isValidTypeHandlerDeclaration(TypeInfo<T> type, TypeHandler<T> typeHandler) {
Name moduleDeclaringHandler = getModuleProviding(typeHandler.getClass());
@@ -150,18 +117,6 @@ private boolean doesSubclassMatch(Class<?> subclass, String subTypeIdentifier) {
return Objects.equals(moduleDeclaringType, moduleDeclaringHandler);
}
- private String getTypeUri(Class<?> type) {
- Name moduleProvidingType = getModuleProviding(type);
-
- if (moduleProvidingType == null || moduleProvidingType.isEmpty()) {
- return type.getName();
- }
-
- String typeSimpleName = type.getSimpleName();
-
- return new SimpleUri(moduleProvidingType, typeSimpleName).toString();
- }
-
private Name getModuleProviding(Class<?> type) {
if (type.getClassLoader() == null) {
return null;
