Skip to content

Commit

Permalink
Merge pull request quarkusio#45350 from geoand/rr-alloc-startup
Browse files Browse the repository at this point in the history
Slightly optimize Quarkus REST startup by reducing unnecessary allocations
  • Loading branch information
gsmet authored Jan 7, 2025
2 parents 0e013fa + 7d820ec commit 9289bab
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
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++) {
ResourceFeature resourceFeature = resourceFeatures.get(i);
if (resourceFeature.getFactory() == null) {
resourceFeature.setFactory((BeanFactory<Feature>) factoryCreator.apply(resourceFeature.getClassName()));
}
}
}
Expand Down

0 comments on commit 9289bab

Please sign in to comment.