Skip to content

Commit 64deb06

Browse files
committed
Cache CClassType.get(Class)
Improves runtime performance of type checking functions.
1 parent 7893dbf commit 64deb06

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

src/main/java/com/laytonsmith/core/constructs/CClassType.java

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public final class CClassType extends Construct implements com.laytonsmith.core.
3838

3939
public static final String PATH_SEPARATOR = FullyQualifiedClassName.PATH_SEPARATOR;
4040

41-
private static final Map<FullyQualifiedClassName, CClassType> CACHE = new HashMap<>();
41+
private static final Map<FullyQualifiedClassName, CClassType> FQCN_CCLASSTYPE_CACHE = new HashMap<>();
42+
private static final Map<Class<? extends Mixed>, CClassType> CLASS_CCLASSTYPE_CACHE = new HashMap<>();
4243

4344
// The only types that can be created here are the ones that don't have a real class associated with them, or the
4445
// TYPE value itself
@@ -68,7 +69,7 @@ public final class CClassType extends Construct implements com.laytonsmith.core.
6869
public static final CClassType[] EMPTY_CLASS_ARRAY = new CClassType[0];
6970

7071
static {
71-
CACHE.put(FullyQualifiedClassName.forNativeClass(CClassType.class), TYPE);
72+
FQCN_CCLASSTYPE_CACHE.put(FullyQualifiedClassName.forNativeClass(CClassType.class), TYPE);
7273
}
7374

7475
private final boolean isTypeUnion;
@@ -112,13 +113,21 @@ public final class CClassType extends Construct implements com.laytonsmith.core.
112113
* @return
113114
*/
114115
public static CClassType get(Class<? extends Mixed> type) {
115-
try {
116-
CClassType t = get(FullyQualifiedClassName.forNativeClass(type));
117-
t.nativeClass = type;
118-
return t;
119-
} catch (ClassNotFoundException ex) {
120-
throw new Error(ex);
116+
117+
// Get cached result or compute and cache result.
118+
CClassType cclassType = CLASS_CCLASSTYPE_CACHE.get(type);
119+
if(cclassType == null) {
120+
try {
121+
cclassType = get(FullyQualifiedClassName.forNativeClass(type));
122+
cclassType.nativeClass = type;
123+
} catch (ClassNotFoundException ex) {
124+
throw new Error(ex);
125+
}
126+
CLASS_CCLASSTYPE_CACHE.put(type, cclassType);
121127
}
128+
129+
// Return the result.
130+
return cclassType;
122131
}
123132

124133
/**
@@ -129,13 +138,13 @@ public static CClassType get(Class<? extends Mixed> type) {
129138
*/
130139
public static CClassType get(FullyQualifiedClassName type) throws ClassNotFoundException {
131140
assert type != null;
132-
CClassType ctype = CACHE.get(type);
141+
CClassType ctype = FQCN_CCLASSTYPE_CACHE.get(type);
133142
if(ctype == null) {
134-
synchronized(CACHE) {
135-
ctype = CACHE.get(type);
143+
synchronized(FQCN_CCLASSTYPE_CACHE) {
144+
ctype = FQCN_CCLASSTYPE_CACHE.get(type);
136145
if(ctype == null) {
137146
ctype = new CClassType(type, Target.UNKNOWN, false);
138-
CACHE.put(type, ctype);
147+
FQCN_CCLASSTYPE_CACHE.put(type, ctype);
139148
}
140149
}
141150
}
@@ -157,13 +166,13 @@ public static CClassType get(FullyQualifiedClassName... types) throws ClassNotFo
157166
SortedSet<FullyQualifiedClassName> t = new TreeSet<>(Arrays.asList(types));
158167
FullyQualifiedClassName type
159168
= FullyQualifiedClassName.forFullyQualifiedClass(StringUtils.Join(t, "|", e -> e.getFQCN()));
160-
CClassType ctype = CACHE.get(type);
169+
CClassType ctype = FQCN_CCLASSTYPE_CACHE.get(type);
161170
if(ctype == null) {
162-
synchronized(CACHE) {
163-
ctype = CACHE.get(type);
171+
synchronized(FQCN_CCLASSTYPE_CACHE) {
172+
ctype = FQCN_CCLASSTYPE_CACHE.get(type);
164173
if(ctype == null) {
165174
ctype = new CClassType(type, Target.UNKNOWN, false);
166-
CACHE.put(type, ctype);
175+
FQCN_CCLASSTYPE_CACHE.put(type, ctype);
167176
}
168177
}
169178
}
@@ -195,7 +204,7 @@ public static CClassType get(CClassType... types) throws ClassNotFoundException
195204
public static CClassType defineClass(FullyQualifiedClassName fqcn) {
196205
try {
197206
CClassType type = new CClassType(fqcn, Target.UNKNOWN, true);
198-
CACHE.put(fqcn, type);
207+
FQCN_CCLASSTYPE_CACHE.put(fqcn, type);
199208
return type;
200209
} catch (ClassNotFoundException ex) {
201210
throw new Error(ex);

0 commit comments

Comments
 (0)