From 822d9b181332606a614639a3cd52d40c756b3525 Mon Sep 17 00:00:00 2001 From: Zapan Date: Mon, 20 Jan 2025 10:11:18 +0800 Subject: [PATCH 1/2] [type:optimize] Optimize BodyParamUtils with Caffeine cache - Added Caffeine cache for base type checking - Improved isBaseType() performance with 5000-entry cache - Reduced class loading overhead --- .gitignore | 2 ++ .../plugin/api/utils/BodyParamUtils.java | 36 ++++++++++++------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index b9784dc9b280..648f8ec05970 100644 --- a/.gitignore +++ b/.gitignore @@ -45,3 +45,5 @@ Thumbs.db # rust ignore *.lock + +.factorypath diff --git a/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/BodyParamUtils.java b/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/BodyParamUtils.java index 4368e976ea04..282d06678134 100644 --- a/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/BodyParamUtils.java +++ b/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/BodyParamUtils.java @@ -17,8 +17,13 @@ package org.apache.shenyu.plugin.api.utils; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + import org.apache.commons.lang3.ClassUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; @@ -27,12 +32,10 @@ import org.apache.shenyu.common.utils.ReflectUtils; import org.springframework.util.LinkedMultiValueMap; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; /** * Common rpc parameter builder utils. @@ -127,6 +130,11 @@ private static boolean isNameMapping(final String parameterTypes) { return parameterTypes.startsWith("{") && parameterTypes.endsWith("}"); } + // Caffeine cache with maximum size of 5000 + private static final Cache BASE_TYPE_CACHE = Caffeine.newBuilder() + .maximumSize(5000) + .build(); + /** * isBaseType. * @@ -134,10 +142,12 @@ private static boolean isNameMapping(final String parameterTypes) { * @return whether the base type is. */ private static boolean isBaseType(final String paramType) { - try { - return ReflectUtils.isPrimitives(ClassUtils.getClass(paramType)); - } catch (ClassNotFoundException e) { - return false; - } + return BASE_TYPE_CACHE.get(paramType, key -> { + try { + return ReflectUtils.isPrimitives(ClassUtils.getClass(key)); + } catch (ClassNotFoundException e) { + return false; + } + }); } } From 88d05c8b8de23846d7961e060c3b308ed462c130 Mon Sep 17 00:00:00 2001 From: Zapan Date: Mon, 20 Jan 2025 12:44:27 +0800 Subject: [PATCH 2/2] [type:fix] Fix for style check. --- .../apache/shenyu/plugin/api/utils/BodyParamUtils.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/BodyParamUtils.java b/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/BodyParamUtils.java index 282d06678134..cda68dc5c549 100644 --- a/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/BodyParamUtils.java +++ b/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/BodyParamUtils.java @@ -44,6 +44,11 @@ public final class BodyParamUtils { private static final Pattern QUERY_PARAM_PATTERN = Pattern.compile("([^&=]+)(=?)([^&]+)?"); + // Caffeine cache with maximum size of 5000 + private static final Cache BASE_TYPE_CACHE = Caffeine.newBuilder() + .maximumSize(5000) + .build(); + private BodyParamUtils() { } @@ -130,10 +135,6 @@ private static boolean isNameMapping(final String parameterTypes) { return parameterTypes.startsWith("{") && parameterTypes.endsWith("}"); } - // Caffeine cache with maximum size of 5000 - private static final Cache BASE_TYPE_CACHE = Caffeine.newBuilder() - .maximumSize(5000) - .build(); /** * isBaseType.