Skip to content

Commit 5bbf7ce

Browse files
authored
Cache marked type to optimize SerializeSecurityConfigurator registerInterface performance (#15500)
1 parent ea0976b commit 5bbf7ce

1 file changed

Lines changed: 41 additions & 36 deletions

File tree

dubbo-common/src/main/java/org/apache/dubbo/common/utils/SerializeSecurityConfigurator.java

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@
4747
import static org.apache.dubbo.common.constants.LoggerCodeConstants.COMMON_IO_EXCEPTION;
4848

4949
public class SerializeSecurityConfigurator implements ScopeClassLoaderListener<ModuleModel> {
50-
private final SerializeSecurityManager serializeSecurityManager;
51-
52-
private static final ErrorTypeAwareLogger logger =
50+
private static final ErrorTypeAwareLogger LOGGER =
5351
LoggerFactory.getErrorTypeAwareLogger(SerializeSecurityConfigurator.class);
5452

53+
private final Set<Type> markedTypeCache = new HashSet<>();
54+
55+
private final SerializeSecurityManager serializeSecurityManager;
56+
5557
private final ModuleModel moduleModel;
5658

5759
private final ClassHolder classHolder;
@@ -137,7 +139,7 @@ private void loadAllow(ClassLoader classLoader) {
137139
Set<URL> urls = ClassLoaderResourceLoader.loadResources(SERIALIZE_ALLOW_LIST_FILE_PATH, classLoader);
138140
for (URL u : urls) {
139141
try {
140-
logger.info("Read serialize allow list from " + u);
142+
LOGGER.info("Read serialize allow list from " + u);
141143
String[] lines = IOUtils.readLines(u.openStream());
142144
for (String line : lines) {
143145
line = line.trim();
@@ -147,7 +149,7 @@ private void loadAllow(ClassLoader classLoader) {
147149
serializeSecurityManager.addToAlwaysAllowed(line);
148150
}
149151
} catch (IOException e) {
150-
logger.error(
152+
LOGGER.error(
151153
COMMON_IO_EXCEPTION,
152154
"",
153155
"",
@@ -161,7 +163,7 @@ private void loadBlocked(ClassLoader classLoader) {
161163
Set<URL> urls = ClassLoaderResourceLoader.loadResources(SERIALIZE_BLOCKED_LIST_FILE_PATH, classLoader);
162164
for (URL u : urls) {
163165
try {
164-
logger.info("Read serialize blocked list from " + u);
166+
LOGGER.info("Read serialize blocked list from " + u);
165167
String[] lines = IOUtils.readLines(u.openStream());
166168
for (String line : lines) {
167169
line = line.trim();
@@ -171,7 +173,7 @@ private void loadBlocked(ClassLoader classLoader) {
171173
serializeSecurityManager.addToDisAllowed(line);
172174
}
173175
} catch (IOException e) {
174-
logger.error(
176+
LOGGER.error(
175177
COMMON_IO_EXCEPTION,
176178
"",
177179
"",
@@ -213,8 +215,9 @@ public synchronized void registerInterface(Class<?> clazz) {
213215
return;
214216
}
215217

216-
Set<Type> markedClass = new HashSet<>();
217-
checkClass(markedClass, clazz);
218+
if (!checkClass(clazz)) {
219+
return;
220+
}
218221

219222
addToAllow(clazz);
220223

@@ -223,111 +226,111 @@ public synchronized void registerInterface(Class<?> clazz) {
223226
for (Method method : methodsToExport) {
224227
Class<?>[] parameterTypes = method.getParameterTypes();
225228
for (Class<?> parameterType : parameterTypes) {
226-
checkClass(markedClass, parameterType);
229+
checkClass(parameterType);
227230
}
228231

229232
Type[] genericParameterTypes = method.getGenericParameterTypes();
230233
for (Type genericParameterType : genericParameterTypes) {
231-
checkType(markedClass, genericParameterType);
234+
checkType(genericParameterType);
232235
}
233236

234237
Class<?> returnType = method.getReturnType();
235-
checkClass(markedClass, returnType);
238+
checkClass(returnType);
236239

237240
Type genericReturnType = method.getGenericReturnType();
238-
checkType(markedClass, genericReturnType);
241+
checkType(genericReturnType);
239242

240243
Class<?>[] exceptionTypes = method.getExceptionTypes();
241244
for (Class<?> exceptionType : exceptionTypes) {
242-
checkClass(markedClass, exceptionType);
245+
checkClass(exceptionType);
243246
}
244247

245248
Type[] genericExceptionTypes = method.getGenericExceptionTypes();
246249
for (Type genericExceptionType : genericExceptionTypes) {
247-
checkType(markedClass, genericExceptionType);
250+
checkType(genericExceptionType);
248251
}
249252
}
250253
}
251254

252-
private void checkType(Set<Type> markedClass, Type type) {
255+
private void checkType(Type type) {
253256
if (type == null) {
254257
return;
255258
}
256259

257260
if (type instanceof Class) {
258-
checkClass(markedClass, (Class<?>) type);
261+
checkClass((Class<?>) type);
259262
return;
260263
}
261264

262-
if (!markedClass.add(type)) {
265+
if (!markedTypeCache.add(type)) {
263266
return;
264267
}
265268

266269
if (type instanceof ParameterizedType) {
267270
ParameterizedType parameterizedType = (ParameterizedType) type;
268-
checkClass(markedClass, (Class<?>) parameterizedType.getRawType());
271+
checkClass((Class<?>) parameterizedType.getRawType());
269272
for (Type actualTypeArgument : parameterizedType.getActualTypeArguments()) {
270-
checkType(markedClass, actualTypeArgument);
273+
checkType(actualTypeArgument);
271274
}
272275
} else if (type instanceof GenericArrayType) {
273276
GenericArrayType genericArrayType = (GenericArrayType) type;
274-
checkType(markedClass, genericArrayType.getGenericComponentType());
277+
checkType(genericArrayType.getGenericComponentType());
275278
} else if (type instanceof TypeVariable) {
276279
TypeVariable typeVariable = (TypeVariable) type;
277280
for (Type bound : typeVariable.getBounds()) {
278-
checkType(markedClass, bound);
281+
checkType(bound);
279282
}
280283
} else if (type instanceof WildcardType) {
281284
WildcardType wildcardType = (WildcardType) type;
282285
for (Type bound : wildcardType.getUpperBounds()) {
283-
checkType(markedClass, bound);
286+
checkType(bound);
284287
}
285288
for (Type bound : wildcardType.getLowerBounds()) {
286-
checkType(markedClass, bound);
289+
checkType(bound);
287290
}
288291
}
289292
}
290293

291-
private void checkClass(Set<Type> markedClass, Class<?> clazz) {
294+
private boolean checkClass(Class<?> clazz) {
292295
if (clazz == null) {
293-
return;
296+
return false;
294297
}
295298

296-
if (!markedClass.add(clazz)) {
297-
return;
299+
if (!markedTypeCache.add(clazz)) {
300+
return false;
298301
}
299302

300303
addToAllow(clazz);
301304

302305
if (ClassUtils.isSimpleType(clazz) || clazz.isPrimitive() || clazz.isArray()) {
303-
return;
306+
return true;
304307
}
305308
String className = clazz.getName();
306309
if (className.startsWith("java.")
307310
|| className.startsWith("javax.")
308311
|| className.startsWith("com.sun.")
309312
|| className.startsWith("sun.")
310313
|| className.startsWith("jdk.")) {
311-
return;
314+
return true;
312315
}
313316

314317
Class<?>[] interfaces = clazz.getInterfaces();
315318
for (Class<?> interfaceClass : interfaces) {
316-
checkClass(markedClass, interfaceClass);
319+
checkClass(interfaceClass);
317320
}
318321

319322
for (Type genericInterface : clazz.getGenericInterfaces()) {
320-
checkType(markedClass, genericInterface);
323+
checkType(genericInterface);
321324
}
322325

323326
Class<?> superclass = clazz.getSuperclass();
324327
if (superclass != null) {
325-
checkClass(markedClass, superclass);
328+
checkClass(superclass);
326329
}
327330

328331
Type genericSuperclass = clazz.getGenericSuperclass();
329332
if (genericSuperclass != null) {
330-
checkType(markedClass, genericSuperclass);
333+
checkType(genericSuperclass);
331334
}
332335

333336
Field[] fields = clazz.getDeclaredFields();
@@ -338,9 +341,11 @@ private void checkClass(Set<Type> markedClass, Class<?> clazz) {
338341
}
339342

340343
Class<?> fieldClass = field.getType();
341-
checkClass(markedClass, fieldClass);
342-
checkType(markedClass, field.getGenericType());
344+
checkClass(fieldClass);
345+
checkType(field.getGenericType());
343346
}
347+
348+
return true;
344349
}
345350

346351
private void addToAllow(Class<?> clazz) {

0 commit comments

Comments
 (0)