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

This commit is contained in:
godotg
2023-10-21 09:51:02 +08:00
parent b973ca5974
commit afa85e55fb
4 changed files with 5 additions and 26 deletions
@@ -15,7 +15,7 @@ import java.lang.reflect.Proxy;
* @version 1.0
*/
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表达式函数(方法)名称
@@ -60,7 +49,7 @@ public abstract class LambdaUtils {
* @return 函数名称
*/
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));
}
/**
* 解析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
*/
public class TestFieldUtils {
public class FieldUtilsTest {
@Test
public void testPropertyName() {
@@ -19,7 +19,7 @@ import org.junit.Test;
/**
* @author veione
*/
public class TestLambdaFunctionCache {
public class LambdaFunctionCacheTest {
@Test
public void testFunctionCache() {
@@ -23,7 +23,7 @@ import java.lang.reflect.Method;
/**
* @author veione
*/
public class TestLambdaFunction {
public class LambdaFunctionTest {
//https://blog.csdn.net/iteye_19045/article/details/119299015
@Test
public void testFuncSerialization() throws Exception {