perf[hint]: perfect storage autoconfig

This commit is contained in:
godotg
2023-09-04 23:19:59 +08:00
parent 403c06cce9
commit 19f2775a65
2 changed files with 81 additions and 19 deletions
@@ -25,6 +25,7 @@ import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import java.util.HashSet;
import java.util.function.Predicate;
/**
* Register runtime hints for the token library
@@ -45,26 +46,10 @@ public class GraalvmStorageHints implements RuntimeHintsRegistrar {
classes.add(StorageData.class);
classes.add(StorageConfig.class);
try {
for (var className : ClassUtils.getAllClasses("")) {
try {
var clazz = Class.forName(className);
if (!clazz.isAnnotationPresent(GraalvmNativeStorage.class)) {
continue;
}
classes.add(clazz);
classes.addAll(ClassUtils.relevantClass(clazz));
} catch (Throwable t) {
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
var filterClasses = HintUtils.filterAllClass(clazz -> clazz.isAnnotationPresent(GraalvmNativeStorage.class));
classes.addAll(filterClasses);
for (var clazz : classes) {
this.bindingRegistrar.registerReflectionHints(hints.reflection(), clazz);
logger.info("storage graalvm aot hints register serialization [{}]", clazz);
}
HintUtils.registerRelevantClass(hints, classes);
for (var resource : StorageEnum.values()) {
var include = StringUtils.format("*.{}", resource.getType());
@@ -0,0 +1,77 @@
/*
* Copyright (C) 2020 The zfoo Authors
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.boot.graalvm;
import com.zfoo.protocol.util.ClassUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aot.hint.RuntimeHints;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import static org.springframework.aot.hint.MemberCategory.*;
/**
* @author godotg
*/
public abstract class HintUtils {
private static final Logger logger = LoggerFactory.getLogger(HintUtils.class);
/**
* 与set有关的所有内部类包括泛型
*/
public static Set<Class<?>> filterAllClass(Predicate<Class<?>> predicate) {
try {
var classes = new HashSet<Class<?>>();
for (var className : ClassUtils.getAllClasses("")) {
Class<?> clazz = null;
try {
clazz = Class.forName(className);
} catch (Throwable t) {
// do nothing
}
if (clazz == null) {
continue;
}
if (predicate.test(clazz)) {
classes.add(clazz);
}
}
return classes;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static void registerRelevantClass(RuntimeHints hints, Set<Class<?>> classes) {
var relevantClasses = classes.stream()
.map(it -> ClassUtils.relevantClass(it))
.flatMap(it -> it.stream())
.distinct()
.toList();
for (var clazz : relevantClasses) {
hints.reflection().registerType(clazz, DECLARED_FIELDS, INVOKE_PUBLIC_CONSTRUCTORS, INVOKE_PUBLIC_METHODS);
logger.info("zfoo for graalvm aot hints register [{}]", clazz);
}
}
}