Skip to content
Browse files

Merge pull request #444 from patriot1burke/master

optimzations
  • Loading branch information...
2 parents 46b4821 + 5b3e9b8 commit 12246639647ae8d7c10f9ea5e9a609c7c63c6ba4 @patriot1burke patriot1burke committed
View
4 .../profiling-tests/src/test/java/org/jboss/resteasy/test/profiling/MockedProfilingTest.java
@@ -42,6 +42,8 @@ public void testCleartext() throws Exception
{
final int WARMUP = 10;
final int INTERATIONS = 100;
+ //final int WARMUP = 1000;
+ //final int INTERATIONS = 100000;
ResteasyDeployment deployment = new ResteasyDeployment();
deployment.start();
@@ -49,7 +51,7 @@ public void testCleartext() throws Exception
registry.addPerRequestResource(CleartextResource.class);
MockHttpResponse response = new MockHttpResponse();
- MockHttpRequest request = MockHttpRequest.post("/test/create").accept(MediaType.TEXT_PLAIN).contentType(MediaType.TEXT_PLAIN);
+ MockHttpRequest request = MockHttpRequest.post("/test/create").contentType(MediaType.TEXT_PLAIN);
ByteArrayInputStream stream = new ByteArrayInputStream("hello".getBytes());
request.setInputStream(stream);
View
2 jaxrs/resteasy-jaxrs/src/main/java/org/jboss/resteasy/core/AbstractAsynchronousResponse.java
@@ -167,7 +167,7 @@ else if (entity instanceof Response)
MediaType type = method.resolveContentType(request, entity);
BuiltResponse jaxrsResponse = (BuiltResponse)Response.ok(entity, type).build();
jaxrsResponse.setGenericType(method.getGenericReturnType());
- jaxrsResponse.addMethodAnnotations(method.getMethod());
+ jaxrsResponse.addMethodAnnotations(method.getMethodAnnotations());
response = jaxrsResponse;
}
try
View
16 jaxrs/resteasy-jaxrs/src/main/java/org/jboss/resteasy/core/ResourceMethodInvoker.java
@@ -53,6 +53,7 @@
protected ResteasyProviderFactory parentProviderFactory;
protected ResteasyProviderFactory resourceMethodProviderFactory;
protected ResourceMethod method;
+ protected Annotation[] methodAnnotations;
protected ContainerRequestFilter[] requestFilters;
protected ContainerResponseFilter[] responseFilters;
protected WriterInterceptor[] writerInterceptors;
@@ -72,6 +73,7 @@ public ResourceMethodInvoker(ResourceMethod method, InjectorFactory injector, Re
this.resource = resource;
this.parentProviderFactory = providerFactory;
this.method = method;
+ this.methodAnnotations = this.method.getAnnotatedMethod().getAnnotations();
resourceInfo = new ResourceInfo()
{
@@ -179,6 +181,8 @@ protected void incrementMethodCount(String httpMethod)
return stats;
}
+
+
public ContainerRequestFilter[] getRequestFilters()
{
return requestFilters;
@@ -206,15 +210,19 @@ public Type getGenericReturnType()
public Annotation[] getMethodAnnotations()
{
- return method.getAnnotatedMethod().getAnnotations();
+ return methodAnnotations;
}
+
+
@Override
public Method getMethod()
{
return method.getMethod();
}
+
+
public BuiltResponse invoke(HttpRequest request, HttpResponse response)
{
Object target = resource.createResource(request, response, resourceMethodProviderFactory);
@@ -308,7 +316,7 @@ else if (isValidatable)
if (rtn == null || method.getReturnType().equals(void.class))
{
BuiltResponse build = (BuiltResponse) Response.noContent().build();
- build.addMethodAnnotations(method.getAnnotatedMethod());
+ build.addMethodAnnotations(getMethodAnnotations());
return build;
}
if (Response.class.isAssignableFrom(method.getReturnType()) || rtn instanceof Response)
@@ -321,7 +329,7 @@ else if (isValidatable)
rtn = new BuiltResponse(r.getStatus(), metadata, r.getEntity(), null);
}
BuiltResponse rtn1 = (BuiltResponse) rtn;
- rtn1.addMethodAnnotations(method.getAnnotatedMethod());
+ rtn1.addMethodAnnotations(getMethodAnnotations());
if (rtn1.getGenericType() == null)
{
if (getMethod().getReturnType().equals(Response.class))
@@ -349,7 +357,7 @@ else if (isValidatable)
jaxrsResponse.setGenericType(method.getGenericReturnType());
}
}
- jaxrsResponse.addMethodAnnotations(method.getAnnotatedMethod());
+ jaxrsResponse.addMethodAnnotations(getMethodAnnotations());
return jaxrsResponse;
}
View
18 ...sy-jaxrs/src/main/java/org/jboss/resteasy/core/interception/JaxrsInterceptorRegistry.java
@@ -329,6 +329,7 @@ protected Object getInterceptor()
protected List<JaxrsInterceptorRegistryListener> listeners = new CopyOnWriteArrayList<JaxrsInterceptorRegistryListener>();
protected ResteasyProviderFactory providerFactory;
protected Class<T> intf;
+ protected volatile T[] cachedPreMatch;
public JaxrsInterceptorRegistry(ResteasyProviderFactory providerFactory, Class<T> intf)
{
@@ -372,16 +373,20 @@ public int compare(Match match, Match match2)
public T[] preMatch()
{
- List<Match> matches = new ArrayList<Match>();
- for (InterceptorFactory factory : interceptors)
+ if (cachedPreMatch == null)
{
- Match match = factory.preMatch();
- if (match != null)
+ List<Match> matches = new ArrayList<Match>();
+ for (InterceptorFactory factory : interceptors)
{
- matches.add(match);
+ Match match = factory.preMatch();
+ if (match != null)
+ {
+ matches.add(match);
+ }
}
+ cachedPreMatch = createArray(matches);
}
- return createArray(matches);
+ return cachedPreMatch;
}
@@ -418,6 +423,7 @@ protected void sort(List<Match> matches)
public void register(InterceptorFactory factory)
{
interceptors.add(factory);
+ cachedPreMatch = null;
for (JaxrsInterceptorRegistryListener listener : listeners)
{
listener.registryUpdated(this);
View
177 jaxrs/resteasy-jaxrs/src/main/java/org/jboss/resteasy/core/registry/SegmentNode.java
@@ -149,37 +149,54 @@ public void potentials(String path, int start, List<MethodExpression> matches)
}
}
- public static MediaType createSortFactor(MediaType client, MediaType server)
+ public static class SortFactor
{
- int d = 0;
- String type;
- String subtype;
+ public float q = 1.0f;
+ public float qs = 1.0f;
+ public int d;
+ public int dm;
+ public String type;
+ public String subtype;
+ public Map<String, String> params;
+
+ public boolean isWildcardType()
+ {
+ return type.equals(MediaType.MEDIA_TYPE_WILDCARD);
+ }
+ public boolean isWildcardSubtype()
+ {
+ return subtype.equals(MediaType.MEDIA_TYPE_WILDCARD);
+ }
+
+ }
+
+ public static SortFactor createSortFactor(MediaType client, MediaType server)
+ {
+ SortFactor sortFactor = new SortFactor();
if (client.isWildcardType() != server.isWildcardType())
{
- type = (client.isWildcardType()) ? server.getType() : client.getType();
- d++;
+ sortFactor.type = (client.isWildcardType()) ? server.getType() : client.getType();
+ sortFactor.d++;
}
else
{
- type = client.getType();
+ sortFactor.type = client.getType();
}
if (client.isWildcardSubtype() != server.isWildcardSubtype())
{
- subtype = (client.isWildcardSubtype()) ? server.getSubtype() : client.getSubtype();
- d++;
+ sortFactor.subtype = (client.isWildcardSubtype()) ? server.getSubtype() : client.getSubtype();
+ sortFactor.d++;
}
else
{
- subtype = client.getSubtype();
+ sortFactor.subtype = client.getSubtype();
}
- Map<String, String> params = new HashMap<String, String>();
String q = client.getParameters().get("q");
- if (q != null) params.put("q", q);
+ if (q != null) sortFactor.q = Float.parseFloat(q);
String qs = server.getParameters().get("qs");
- if (qs != null) params.put("qs", qs);
- params.put("d", Integer.toString(d));
+ if (qs != null) sortFactor.qs = Float.parseFloat(qs);
- int dm = 0;
+ sortFactor.dm = 0;
for (Map.Entry<String, String> entry : client.getParameters().entrySet())
{
String name = entry.getKey();
@@ -188,12 +205,12 @@ public static MediaType createSortFactor(MediaType client, MediaType server)
String val = server.getParameters().get(name);
if (val == null)
{
- dm++;
+ sortFactor.dm++;
continue;
}
if (!val.equals(entry.getValue()))
{
- dm++;
+ sortFactor.dm++;
continue;
}
}
@@ -206,59 +223,46 @@ public static MediaType createSortFactor(MediaType client, MediaType server)
String val = client.getParameters().get(name);
if (val == null)
{
- dm++;
+ sortFactor.dm++;
continue;
}
if (!val.equals(entry.getValue()))
{
- dm++;
+ sortFactor.dm++;
continue;
}
}
- params.put("dm", Integer.toString(dm));
- return new MediaType(type, subtype, params);
-
+ return sortFactor;
}
protected class SortEntry implements Comparable<SortEntry>
{
Match match;
- MediaType accept;
- MediaType consumes;
- float qConsumes = 1.0f;
- float qsConsumes = 1.0f;
- int dConsumes = 0;
- int dmConsumes = 0;
- MediaType produces;
- float qProduces = 1.0f;
- float qsProduces = 1.0f;
- int dProduces = 0;
- int dmProduces = 0;
-
- public SortEntry(Match match, MediaType consumes, MediaType produces, MediaType accept)
+ MediaType serverProduce;
+ SortFactor consumes;
+ SortFactor produces;
+
+ public SortEntry(Match match, SortFactor consumes, SortFactor produces, MediaType serverProduce)
{
- this.accept = accept;
+ this.serverProduce = serverProduce;
this.match = match;
this.consumes = consumes;
- String q = consumes.getParameters().get("q");
- if (q != null) qConsumes = Float.parseFloat(q);
- String qs = consumes.getParameters().get("qs");
- if (qs != null) qsConsumes = Float.parseFloat(qs);
- String d = consumes.getParameters().get("d");
- if (d != null) dConsumes = Integer.parseInt(d);
- String dm = consumes.getParameters().get("dm");
- if (dm != null) dmConsumes = Integer.parseInt(dm);
-
-
this.produces = produces;
- q = produces.getParameters().get("q");
- if (q != null) qProduces = Float.parseFloat(q);
- qs = produces.getParameters().get("qs");
- if (qs != null) qsProduces = Float.parseFloat(qs);
- d = produces.getParameters().get("d");
- if (d != null) dProduces = Integer.parseInt(d);
- dm = produces.getParameters().get("dm");
- if (dm != null) dmProduces = Integer.parseInt(dm);
+ }
+
+ public MediaType getAcceptType()
+ {
+ // take params from produce and type and subtype from sort factor
+ // to define the returned media type
+ Map<String, String> params = new HashMap<String, String>();
+ for (Map.Entry<String, String> entry : serverProduce.getParameters().entrySet())
+ {
+ String name = entry.getKey();
+ if ("q".equals(name)
+ || "qs".equals(name)) continue;
+ params.put(name, entry.getValue());
+ }
+ return new MediaType(produces.type, produces.subtype, params);
}
@@ -270,34 +274,34 @@ public int compareTo(SortEntry o)
if (consumes.isWildcardSubtype() && !o.consumes.isWildcardSubtype()) return 1;
if (!consumes.isWildcardSubtype() && o.consumes.isWildcardSubtype()) return -1;
- if (qConsumes > o.qConsumes) return -1;
- if (qConsumes < o.qConsumes) return 1;
+ if (consumes.q > o.consumes.q) return -1;
+ if (consumes.q < o.consumes.q) return 1;
- if (qsConsumes > o.qsConsumes) return -1;
- if (qsConsumes < o.qsConsumes) return 1;
+ if (consumes.qs > o.consumes.qs) return -1;
+ if (consumes.qs < o.consumes.qs) return 1;
- if (dConsumes < o.dConsumes) return -1;
- if (dConsumes > o.dConsumes) return 1;
+ if (consumes.d < o.consumes.d) return -1;
+ if (consumes.d > o.consumes.d) return 1;
- if (dmConsumes < o.dmConsumes) return -1;
- if (dmConsumes > o.dmConsumes) return 1;
+ if (consumes.dm < o.consumes.dm) return -1;
+ if (consumes.dm > o.consumes.dm) return 1;
if (produces.isWildcardType() && !o.produces.isWildcardType()) return 1;
if (!produces.isWildcardType() && o.produces.isWildcardType()) return -1;
if (produces.isWildcardSubtype() && !o.produces.isWildcardSubtype()) return 1;
if (!produces.isWildcardSubtype() && o.produces.isWildcardSubtype()) return -1;
- if (qProduces > o.qProduces) return -1;
- if (qProduces < o.qProduces) return 1;
+ if (produces.q > o.produces.q) return -1;
+ if (produces.q < o.produces.q) return 1;
- if (qsProduces > o.qsProduces) return -1;
- if (qsProduces < o.qsProduces) return 1;
+ if (produces.qs > o.produces.qs) return -1;
+ if (produces.qs < o.produces.qs) return 1;
- if (dProduces < o.dProduces) return -1;
- if (dProduces > o.dProduces) return 1;
+ if (produces.d < o.produces.d) return -1;
+ if (produces.d > o.produces.d) return 1;
- if (dmProduces < o.dmProduces) return -1;
- if (dmProduces > o.dmProduces) return 1;
+ if (produces.dm < o.produces.dm) return -1;
+ if (produces.dm > o.produces.dm) return 1;
return match.expression.compareTo(o.match.expression);
}
@@ -307,9 +311,9 @@ public Match match(List<Match> matches, String httpMethod, HttpRequest request)
{
MediaType contentType = request.getHttpHeaders().getMediaType();
- List<MediaType> oldaccepts = request.getHttpHeaders().getAcceptableMediaTypes();
- List<WeightedMediaType> accepts = new ArrayList<WeightedMediaType>();
- for (MediaType accept : oldaccepts) accepts.add(WeightedMediaType.parse(accept));
+ List<MediaType> requestAccepts = request.getHttpHeaders().getAcceptableMediaTypes();
+ List<WeightedMediaType> weightedAccepts = new ArrayList<WeightedMediaType>();
+ for (MediaType accept : requestAccepts) weightedAccepts.add(WeightedMediaType.parse(accept));
List<Match> list = new ArrayList<Match>();
@@ -327,7 +331,7 @@ public Match match(List<Match> matches, String httpMethod, HttpRequest request)
if (invoker.doesConsume(contentType))
{
consumeMatch = true;
- if (invoker.doesProduce(accepts))
+ if (invoker.doesProduce(weightedAccepts))
{
list.add(match);
}
@@ -377,7 +381,7 @@ else if (!consumeMatch)
}
throw new NotAcceptableException("No match for accept header");
}
- //if (list.size() == 1) return list.get(0); don't do this optimization so that was can set chosen accept
+ //if (list.size() == 1) return list.get(0); //don't do this optimization as we need to set chosen accept
List<SortEntry> sortList = new ArrayList<SortEntry>();
for (Match match : list)
{
@@ -394,14 +398,14 @@ else if (!consumeMatch)
{
produces = WILDCARD_ARRAY;
}
- List<MediaType> consumeCombo = new ArrayList<MediaType>();
+ List<SortFactor> consumeCombo = new ArrayList<SortFactor>();
for (MediaType consume : consumes)
{
consumeCombo.add(createSortFactor(contentType, consume));
}
for (MediaType produce : produces)
{
- List<MediaType> acceptableMediaTypes = request.getHttpHeaders().getAcceptableMediaTypes();
+ List<MediaType> acceptableMediaTypes = requestAccepts;
if (acceptableMediaTypes.size() == 0)
{
acceptableMediaTypes = DEFAULT_ACCEPTS;
@@ -410,22 +414,11 @@ else if (!consumeMatch)
{
if (accept.isCompatible(produce))
{
- MediaType sortFactor = createSortFactor(accept, produce);
- // take params from produce and type and subtype from sort factor
- // to define the returned media type
- Map<String, String> params = new HashMap<String, String>();
- for (Map.Entry<String, String> entry : produce.getParameters().entrySet())
- {
- String name = entry.getKey();
- if ("q".equals(name)
- || "qs".equals(name)) continue;
- params.put(name, entry.getValue());
- }
- MediaType chosen = new MediaType(sortFactor.getType(), sortFactor.getSubtype(), params);
+ SortFactor sortFactor = createSortFactor(accept, produce);
- for (MediaType consume : consumeCombo)
+ for (SortFactor consume : consumeCombo)
{
- sortList.add(new SortEntry(match, consume, sortFactor, chosen));
+ sortList.add(new SortEntry(match, consume, sortFactor, produce));
}
}
@@ -434,7 +427,7 @@ else if (!consumeMatch)
}
Collections.sort(sortList);
SortEntry sortEntry = sortList.get(0);
- request.setAttribute(RESTEASY_CHOSEN_ACCEPT, sortEntry.accept);
+ request.setAttribute(RESTEASY_CHOSEN_ACCEPT, sortEntry.getAcceptType());
return sortEntry.match;
}
View
4 jaxrs/resteasy-jaxrs/src/main/java/org/jboss/resteasy/specimpl/BuiltResponse.java
@@ -166,7 +166,7 @@ public void setMetadata(MultivaluedMap<String, Object> metadata)
return annotations;
}
- public void addMethodAnnotations(Method method)
+ public void addMethodAnnotations(Annotation[] methodAnnotations)
{
List<Annotation> ann = new ArrayList<Annotation>();
if (annotations != null)
@@ -176,7 +176,7 @@ public void addMethodAnnotations(Method method)
ann.add(annotation);
}
}
- for (Annotation annotation : method.getAnnotations())
+ for (Annotation annotation : methodAnnotations)
{
ann.add(annotation);
}

0 comments on commit 1224663

Please sign in to comment.
Something went wrong with that request. Please try again.