From 6fae3b477f80e422f62c75d8e6c39214c6c4915e Mon Sep 17 00:00:00 2001 From: Jan Supol Date: Fri, 25 Oct 2019 12:10:48 +0200 Subject: [PATCH 1/2] Performance improvement in Resource.Builder#mergeResources Signed-off-by: Jan Supol --- .../jersey/server/model/Resource.java | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/core-server/src/main/java/org/glassfish/jersey/server/model/Resource.java b/core-server/src/main/java/org/glassfish/jersey/server/model/Resource.java index c036413cc3..831e39cc1c 100644 --- a/core-server/src/main/java/org/glassfish/jersey/server/model/Resource.java +++ b/core-server/src/main/java/org/glassfish/jersey/server/model/Resource.java @@ -25,10 +25,13 @@ import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.stream.Collectors; import javax.ws.rs.Path; +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.MultivaluedMap; import org.glassfish.jersey.Severity; import org.glassfish.jersey.internal.Errors; @@ -543,27 +546,29 @@ private void onBuildChildResource(Builder childResourceBuilder, Resource.Data ch } private static List mergeResources(List resources) { - List mergedResources = new ArrayList<>(); - for (int i = 0; i < resources.size(); i++) { - Resource.Data outer = resources.get(i); - Resource.Builder builder = null; + final MultivaluedMap resourceData = new MultivaluedHashMap<>(); + final List mergedResources = new ArrayList<>(); - for (int j = i + 1; j < resources.size(); j++) { - Resource.Data inner = resources.get(j); + for (Iterator resourcesIterator = resources.iterator(); resourcesIterator.hasNext(); ) { + Resource.Data data = resourcesIterator.next(); + if (resourceData.containsKey(data.path)) { + resourcesIterator.remove(); + } + resourceData.add(data.path, data); + } - if (outer.path.equals(inner.path)) { - if (builder == null) { - builder = Resource.builder(outer); + for (Map.Entry> entry : resourceData.entrySet()) { + if (entry.getValue().size() == 1) { + mergedResources.add(entry.getValue().get(0)); + } else { + Resource.Builder builder = null; + for (Resource.Data data : entry.getValue()) { + if (builder != null) { + builder.mergeWith(data); + } else { + builder = Resource.builder(data); } - builder.mergeWith(inner); - resources.remove(j); - //noinspection AssignmentToForLoopParameter - j--; } - } - if (builder == null) { - mergedResources.add(outer); - } else { mergedResources.add(builder.buildResourceData()); } } From 2deef247881d91e16a15c24168a7bf8ba8188f72 Mon Sep 17 00:00:00 2001 From: Jan Supol Date: Wed, 30 Oct 2019 09:42:18 +0100 Subject: [PATCH 2/2] Revert LinkedList->ArrayList to keep fast Iterator.remove Signed-off-by: Jan Supol --- .../main/java/org/glassfish/jersey/server/model/Resource.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-server/src/main/java/org/glassfish/jersey/server/model/Resource.java b/core-server/src/main/java/org/glassfish/jersey/server/model/Resource.java index 831e39cc1c..bb7961feb1 100644 --- a/core-server/src/main/java/org/glassfish/jersey/server/model/Resource.java +++ b/core-server/src/main/java/org/glassfish/jersey/server/model/Resource.java @@ -203,7 +203,7 @@ public static final class Builder { private Builder(final Resource.Builder parentResource) { this.methodBuilders = new LinkedHashSet<>(); this.childResourceBuilders = new LinkedHashSet<>(); - this.childResources = new ArrayList<>(); + this.childResources = new LinkedList<>(); this.resourceMethods = new LinkedList<>(); this.handlerClasses = Collections.newSetFromMap(new IdentityHashMap<>()); this.handlerInstances = Collections.newSetFromMap(new IdentityHashMap<>());