Skip to content

Commit

Permalink
Add some caches to speed things up in the course of operation
Browse files Browse the repository at this point in the history
  • Loading branch information
LadyCailin committed Apr 5, 2019
1 parent 0865c21 commit 4fce2aa
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
@@ -1,8 +1,10 @@
package com.laytonsmith.PureUtilities.Common;

import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -43,6 +45,7 @@ public static Class forCanonicalName(String className, boolean initialize, Class
return forCanonicalName(className, true, initialize, classLoader);
}

private static final Map<String, Class> CANONICAL_CLASS_CACHE = new ConcurrentHashMap<>();
/**
* Private version, which accepts the useInitializer parameter.
*
Expand All @@ -53,7 +56,11 @@ public static Class forCanonicalName(String className, boolean initialize, Class
* @return
* @throws ClassNotFoundException
*/
private static Class forCanonicalName(String className, boolean useInitializer, boolean initialize, ClassLoader classLoader) throws ClassNotFoundException {
private static Class forCanonicalName(String className, boolean useInitializer, boolean initialize,
ClassLoader classLoader) throws ClassNotFoundException {
if(CANONICAL_CLASS_CACHE.containsKey(className)) {
return CANONICAL_CLASS_CACHE.get(className);
}
if("void".equals(className)) {
return void.class;
}
Expand Down Expand Up @@ -144,6 +151,7 @@ private static Class forCanonicalName(String className, boolean useInitializer,
throw ex;
}
}
CANONICAL_CLASS_CACHE.put(className, c);
return c;
}

Expand Down
Expand Up @@ -262,14 +262,14 @@ private void instantiateInvalidType(Environment env) {
} else if("ms.lang.ClassType".equals(fqcn)) {
invalidType = new Mixed[]{this};
} else {
ObjectDefinitionTable odt = env.getEnv(CompilerEnvironment.class).getObjectDefinitionTable();
invalidType = new Mixed[types.size()];
for(int i = 0; i < invalidType.length; i++) {
// TODO: For now, we must use this mechanism, since we don't populate the ODT with
// all the native classes. But once we do, we should remove this check entirely here.
if(NativeTypeList.getNativeTypeList().contains(this.fqcn)) {
invalidType[i] = NativeTypeList.getInvalidInstanceForUse(this.fqcn);
} else {
ObjectDefinitionTable odt = env.getEnv(CompilerEnvironment.class).getObjectDefinitionTable();
ObjectDefinition od = odt.get(this.fqcn);
invalidType[i] = new UserObject(Target.UNKNOWN, null, env, od, null);
}
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/com/laytonsmith/core/constructs/NativeTypeList.java
Expand Up @@ -41,6 +41,8 @@ public class NativeTypeList {
*/
public static final String INVALID_INSTANCE_METHOD_NAME = "ConstructInvalidInstance";

private static final Map<FullyQualifiedClassName, Mixed> INVALID_INSTANCE_CACHE = new ConcurrentHashMap<>();

/**
* Given a simple name of a class, attempts to resolve
* within the native types (not user defined types). If the class can't be found, null is returned,
Expand Down Expand Up @@ -288,15 +290,23 @@ public static Class<? extends MixedInterfaceRunner> getInterfaceRunnerFor(FullyQ
* @throws java.lang.ClassNotFoundException
*/
public static Mixed getInvalidInstanceForUse(FullyQualifiedClassName fqcn) throws ClassNotFoundException {
if(INVALID_INSTANCE_CACHE.containsKey(fqcn)) {
return INVALID_INSTANCE_CACHE.get(fqcn);
}
Class<? extends Mixed> c = getNativeClassOrInterfaceRunner(fqcn);
if(ReflectionUtils.hasMethod(c, INVALID_INSTANCE_METHOD_NAME, Mixed.class)) {
return (Mixed) ReflectionUtils.invokeMethod(c, null, INVALID_INSTANCE_METHOD_NAME);
Mixed m = (Mixed) ReflectionUtils.invokeMethod(c, null, INVALID_INSTANCE_METHOD_NAME);
INVALID_INSTANCE_CACHE.put(fqcn, m);
return m;
}
Mixed m;
if(MEnumType.class.isAssignableFrom(c)) {
return getNativeEnumType(fqcn);
m = getNativeEnumType(fqcn);
} else { // Not abstract
return ReflectionUtils.instantiateUnsafe(c);
m = ReflectionUtils.instantiateUnsafe(c);
}
INVALID_INSTANCE_CACHE.put(fqcn, m);
return m;
}

/**
Expand Down

0 comments on commit 4fce2aa

Please sign in to comment.