perf[storage]: use string as cache to reduce memory consumption

This commit is contained in:
godotg committed 2023-10-21 09:51:02 +08:00
1 parent b973ca5974
commit afa85e55fb
4 files changed
+5 -26

No files matched your search

@@ -15,7 +15,7 @@ import java.lang.reflect.Proxy;
* @version 1.0 * @version 1.0
*/ */
public abstract class LambdaUtils { public abstract class LambdaUtils {
private static final ConcurrentReferenceHashMap<Class<?>, LambdaMeta> CACHE = new ConcurrentReferenceHashMap<>(64, 0.75F, 16, ConcurrentReferenceHashMap.ReferenceType.SOFT); private static final ConcurrentReferenceHashMap<Class<?>, String> CACHE = new ConcurrentReferenceHashMap<>(64, 0.75F, 16, ConcurrentReferenceHashMap.ReferenceType.SOFT);
/** /**
* 该缓存可能会在任意不定的时间被清除 * 该缓存可能会在任意不定的时间被清除
@@ -40,17 +40,6 @@ public abstract class LambdaUtils {
} }
} }
/**
* 解析lambda表达式,加了缓存。
* 该缓存可能会在任意不定的时间被清除
*
* @param <T> Lambda类型
* @param func 需要解析的 lambda 对象(无参方法)
* @return 返回解析后的结果
*/
public static <T> LambdaMeta resolve(Func1<T, ?> func) {
return _resolve(func);
}
/** /**
* 获取lambda表达式函数(方法)名称 * 获取lambda表达式函数(方法)名称
@@ -60,7 +49,7 @@ public abstract class LambdaUtils {
* @return 函数名称 * @return 函数名称
*/ */
public static <T> String getMethodName(Func1<T, ?> func) { public static <T> String getMethodName(Func1<T, ?> func) {
return resolve(func).getImplMethodName(); return CACHE.computeIfAbsent(func.getClass(), it -> extract(func).getImplMethodName());
} }
/** /**
@@ -74,14 +63,4 @@ public abstract class LambdaUtils {
return FieldUtils.methodToProperty(getMethodName(func)); return FieldUtils.methodToProperty(getMethodName(func));
} }
/**
* 解析lambda表达式,加了缓存。
* 该缓存可能会在任意不定的时间被清除
*
* @param func 需要解析的 lambda 对象
* @return 返回解析后的结果
*/
private static LambdaMeta _resolve(Serializable func) {
return CACHE.computeIfAbsent(func.getClass(), c -> extract(func));
}
} }
@@ -20,7 +20,7 @@ import static org.junit.Assert.assertEquals;
/** /**
* @author veione * @author veione
*/ */
public class TestFieldUtils { public class FieldUtilsTest {
@Test @Test
public void testPropertyName() { public void testPropertyName() {
@@ -19,7 +19,7 @@ import org.junit.Test;
/** /**
* @author veione * @author veione
*/ */
public class TestLambdaFunctionCache { public class LambdaFunctionCacheTest {
@Test @Test
public void testFunctionCache() { public void testFunctionCache() {
@@ -23,7 +23,7 @@ import java.lang.reflect.Method;
/** /**
* @author veione * @author veione
*/ */
public class TestLambdaFunction { public class LambdaFunctionTest {
//https://blog.csdn.net/iteye_19045/article/details/119299015 //https://blog.csdn.net/iteye_19045/article/details/119299015
@Test @Test
public void testFuncSerialization() throws Exception { public void testFuncSerialization() throws Exception {