Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slightly optimize Quarkus REST startup by reducing unnecessary allocations #45350

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.Executor;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
Expand Down Expand Up @@ -141,25 +142,29 @@ public BeanFactory.BeanInstance<?> apply(Class<?> aClass) {
}
Map<String, RequestMapper<RuntimeResource>> mappersByMethod = new RuntimeMappingDeployment(templates)
.buildClassMapper();
mappersByMethod.forEach((method, mapper) -> {
for (RequestMapper.RequestPath<RuntimeResource> path : mapper.getTemplates()) {
if ((clazz.getIsDisabled() != null) && clazz.getIsDisabled().get()) {
String templateWithoutSlash = path.template.template.startsWith("/")
? path.template.template.substring(1)
: path.template.template;
String fullPath = clazz.getPath().endsWith("/") ? finalPrefix + clazz.getPath() + templateWithoutSlash
: finalPrefix + clazz.getPath() + "/" + templateWithoutSlash;
if (!disabledEndpoints.containsKey(fullPath)) {
disabledEndpoints.put(fullPath, new ArrayList<>());
boolean isResourceClassDisabled = (clazz.getIsDisabled() != null) && clazz.getIsDisabled().get();
if (isResourceClassDisabled) {
mappersByMethod.forEach(new BiConsumer<>() {
@Override
public void accept(String method, RequestMapper<RuntimeResource> mapper) {
for (int i = 0; i < mapper.getTemplates().size(); i++) {
RequestMapper.RequestPath<RuntimeResource> path = mapper.getTemplates().get(i);
String templateWithoutSlash = path.template.template.startsWith("/")
? path.template.template.substring(1)
: path.template.template;
String fullPath = clazz.getPath().endsWith("/")
? finalPrefix + clazz.getPath() + templateWithoutSlash
: finalPrefix + clazz.getPath() + "/" + templateWithoutSlash;
if (!disabledEndpoints.containsKey(fullPath)) {
disabledEndpoints.put(fullPath, new ArrayList<>());
}
disabledEndpoints.get(fullPath).add(method);
}
disabledEndpoints.get(fullPath).add(method);
}
}
});
if ((clazz.getIsDisabled() != null) && clazz.getIsDisabled().get()) {
continue;
});
} else {
resourceLocatorHandler.addResource(loadClass(clazz.getClassName()), mappersByMethod);
}
resourceLocatorHandler.addResource(loadClass(clazz.getClassName()), mappersByMethod);
}

//it is possible that multiple resource classes use the same path
Expand Down Expand Up @@ -259,7 +264,6 @@ private void addRuntimeConfigurableHandlers(RuntimeResource runtimeResource,
}
}

//TODO: this needs plenty more work to support all possible types and provide all information the FeatureContext allows
private ConfigurationImpl configureFeatures(Features features, ResourceInterceptors interceptors,
RuntimeExceptionMapper exceptionMapping) {

Expand All @@ -271,7 +275,8 @@ private ConfigurationImpl configureFeatures(Features features, ResourceIntercept
FeatureContextImpl featureContext = new FeatureContextImpl(interceptors, exceptionMapping,
configuration, info.getFactoryCreator());
List<ResourceFeature> resourceFeatures = features.getResourceFeatures();
for (ResourceFeature resourceFeature : resourceFeatures) {
for (int i = 0; i < resourceFeatures.size(); i++) {
ResourceFeature resourceFeature = resourceFeatures.get(i);
Feature feature = resourceFeature.getFactory().createInstance().getInstance();
boolean enabled = feature.configure(featureContext);
if (enabled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ public List<ResourceDynamicFeature> getResourceDynamicFeatures() {
}

public void initializeDefaultFactories(Function<String, BeanFactory<?>> factoryCreator) {
for (ResourceDynamicFeature i : resourceDynamicFeatures) {
if (i.getFactory() == null) {
i.setFactory((BeanFactory<DynamicFeature>) factoryCreator.apply(i.getClassName()));
for (int i = 0; i < resourceDynamicFeatures.size(); i++) {
ResourceDynamicFeature resourceFeature = resourceDynamicFeatures.get(i);
if (resourceFeature.getFactory() == null) {
resourceFeature.setFactory((BeanFactory<DynamicFeature>) factoryCreator.apply(resourceFeature.getClassName()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ public List<ResourceFeature> getResourceFeatures() {
}

public void initializeDefaultFactories(Function<String, BeanFactory<?>> factoryCreator) {
for (ResourceFeature i : resourceFeatures) {
if (i.getFactory() == null) {
i.setFactory((BeanFactory<Feature>) factoryCreator.apply(i.getClassName()));
for (int i = 0; i < resourceFeatures.size(); i++) {
geoand marked this conversation as resolved.
Show resolved Hide resolved
ResourceFeature resourceFeature = resourceFeatures.get(i);
if (resourceFeature.getFactory() == null) {
resourceFeature.setFactory((BeanFactory<Feature>) factoryCreator.apply(resourceFeature.getClassName()));
}
}
}
Expand Down
Loading