From 7d820ecb578ff8325470f63457338f798dcaac2b Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Tue, 7 Jan 2025 10:14:19 +0200 Subject: [PATCH] Slightly reduce allocations in RESTEasy Reactive startup --- .../startup/RuntimeDeploymentManager.java | 41 +++++++++++-------- .../server/model/DynamicFeatures.java | 7 ++-- .../reactive/server/model/Features.java | 7 ++-- 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/startup/RuntimeDeploymentManager.java b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/startup/RuntimeDeploymentManager.java index b5734722de63a..6f3e78a856097 100644 --- a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/startup/RuntimeDeploymentManager.java +++ b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/startup/RuntimeDeploymentManager.java @@ -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; @@ -141,25 +142,29 @@ public BeanFactory.BeanInstance apply(Class aClass) { } Map> mappersByMethod = new RuntimeMappingDeployment(templates) .buildClassMapper(); - mappersByMethod.forEach((method, mapper) -> { - for (RequestMapper.RequestPath 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 mapper) { + for (int i = 0; i < mapper.getTemplates().size(); i++) { + RequestMapper.RequestPath 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 @@ -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) { @@ -271,7 +275,8 @@ private ConfigurationImpl configureFeatures(Features features, ResourceIntercept FeatureContextImpl featureContext = new FeatureContextImpl(interceptors, exceptionMapping, configuration, info.getFactoryCreator()); List 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) { diff --git a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/model/DynamicFeatures.java b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/model/DynamicFeatures.java index fc75b35d05289..15fe5be02cd45 100644 --- a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/model/DynamicFeatures.java +++ b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/model/DynamicFeatures.java @@ -25,9 +25,10 @@ public List getResourceDynamicFeatures() { } public void initializeDefaultFactories(Function> factoryCreator) { - for (ResourceDynamicFeature i : resourceDynamicFeatures) { - if (i.getFactory() == null) { - i.setFactory((BeanFactory) factoryCreator.apply(i.getClassName())); + for (int i = 0; i < resourceDynamicFeatures.size(); i++) { + ResourceDynamicFeature resourceFeature = resourceDynamicFeatures.get(i); + if (resourceFeature.getFactory() == null) { + resourceFeature.setFactory((BeanFactory) factoryCreator.apply(resourceFeature.getClassName())); } } } diff --git a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/model/Features.java b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/model/Features.java index a8fbfc3cd476d..9f136384d4bc1 100644 --- a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/model/Features.java +++ b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/model/Features.java @@ -22,9 +22,10 @@ public List getResourceFeatures() { } public void initializeDefaultFactories(Function> factoryCreator) { - for (ResourceFeature i : resourceFeatures) { - if (i.getFactory() == null) { - i.setFactory((BeanFactory) factoryCreator.apply(i.getClassName())); + for (int i = 0; i < resourceFeatures.size(); i++) { + ResourceFeature resourceFeature = resourceFeatures.get(i); + if (resourceFeature.getFactory() == null) { + resourceFeature.setFactory((BeanFactory) factoryCreator.apply(resourceFeature.getClassName())); } } }