[
https://issues.apache.org/jira/browse/CXF-7716?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16449123#comment-16449123
]
ASF GitHub Bot commented on CXF-7716:
-------------------------------------
reta commented on a change in pull request #407: [CXF-7716] Reduce
StringBuilders and other performance changes.
URL: https://github.com/apache/cxf/pull/407#discussion_r183578479
##########
File path:
rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
##########
@@ -1508,11 +1509,40 @@ public static boolean matchMimeTypes(MediaType
requestContentType,
}
}
}
-
return new ArrayList<>(supportedMimeTypeList);
+ }
+
+ public static boolean doMimeTypesIntersect(List<MediaType>
requiredMediaTypes, List<MediaType> userMediaTypes) {
+ for (MediaType requiredType : requiredMediaTypes) {
Review comment:
Turned out to be quite tricky ... but I have one suggestion for you. Here is
the implementation for `doMimeTypesIntersect` and `intersectMimeTypes`:
```
public static List<MediaType> intersectMimeTypes(List<MediaType>
requiredMediaTypes,
List<MediaType>
userMediaTypes,
boolean
addRequiredParamsIfPossible,
boolean
addDistanceParameter) {
final AccumulatingConsumer consumer = new
AccumulatingConsumer(addRequiredParamsIfPossible, addDistanceParameter);
intersectMimeTypes(requiredMediaTypes, userMediaTypes, consumer);
return new ArrayList<>(consumer.supportedMimeTypeList);
}
public static boolean doMimeTypesIntersect(List<MediaType>
requiredMediaTypes, List<MediaType> userMediaTypes) {
final NonAccumulatingConsumer consumer = new
NonAccumulatingConsumer();
intersectMimeTypes(requiredMediaTypes, userMediaTypes, consumer);
return consumer.doIntersect;
}
```
Here is the initial `intersectMimeTypes` function, refactored to use
consumers, the part which is was accumulating (or returning `true`) is now part
of the consumers:
```
private static void intersectMimeTypes(List<MediaType> requiredMediaTypes,
List<MediaType> userMediaTypes,
BiConsumer<MediaType, MediaType> consumer) {
for (MediaType requiredType : requiredMediaTypes) {
for (MediaType userType : userMediaTypes) {
boolean isCompatible = isMediaTypeCompatible(requiredType,
userType);
if (isCompatible) {
boolean parametersMatched = true;
for (Map.Entry<String, String> entry :
userType.getParameters().entrySet()) {
String value =
requiredType.getParameters().get(entry.getKey());
if (value != null && entry.getValue() != null
&& !(stripDoubleQuotesIfNeeded(value).equals(
stripDoubleQuotesIfNeeded(entry.getValue())))) {
if (HTTP_CHARSET_PARAM.equals(entry.getKey())
&& value.equalsIgnoreCase(entry.getValue()))
{
continue;
}
parametersMatched = false;
break;
}
}
if (!parametersMatched) {
continue;
}
consumer.accept(requiredType, userType);
}
}
}
}
```
And finally, two consumers:
```
private static class NonAccumulatingConsumer implements
BiConsumer<MediaType, MediaType> {
private boolean doIntersect;
@Override
public void accept(MediaType t, MediaType u) {
doIntersect = true;
}
}
```
And
```
private static class AccumulatingConsumer implements
BiConsumer<MediaType, MediaType> {
private final Set<MediaType> supportedMimeTypeList = new
LinkedHashSet<MediaType>();
private final boolean addRequiredParamsIfPossible;
private final boolean addDistanceParameter;
private AccumulatingConsumer(boolean addRequiredParamsIfPossible,
boolean addDistanceParameter) {
this.addRequiredParamsIfPossible = addRequiredParamsIfPossible;
this.addDistanceParameter = addDistanceParameter;
}
@Override
public void accept(MediaType requiredType, MediaType userType) {
boolean requiredTypeWildcard =
requiredType.getType().equals(MediaType.MEDIA_TYPE_WILDCARD);
boolean requiredSubTypeWildcard =
requiredType.getSubtype().contains(MediaType.MEDIA_TYPE_WILDCARD);
String type = requiredTypeWildcard ? userType.getType() :
requiredType.getType();
String subtype = requiredSubTypeWildcard ? userType.getSubtype()
: requiredType.getSubtype();
Map<String, String> parameters = userType.getParameters();
if (addRequiredParamsIfPossible) {
parameters = new LinkedHashMap<String, String>(parameters);
for (Map.Entry<String, String> entry :
requiredType.getParameters().entrySet()) {
if (!parameters.containsKey(entry.getKey())) {
parameters.put(entry.getKey(), entry.getValue());
}
}
}
if (addDistanceParameter) {
int distance = 0;
if (requiredTypeWildcard) {
distance++;
}
if (requiredSubTypeWildcard) {
distance++;
}
parameters.put(MEDIA_TYPE_DISTANCE_PARAM,
Integer.toString(distance));
}
supportedMimeTypeList.add(new MediaType(type, subtype,
parameters));
}
}
```
Ideally, would be good to move to a dedicated utility class, but roughly
this is an idea. What do you think. @WhiteCat22, does it make sense?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> IBM Performance Team has found several performance increases
> ------------------------------------------------------------
>
> Key: CXF-7716
> URL: https://issues.apache.org/jira/browse/CXF-7716
> Project: CXF
> Issue Type: Bug
> Components: JAX-RS
> Affects Versions: 3.0.15, 3.2.4
> Reporter: Adam Anderson
> Priority: Major
> Fix For: 3.2.5
>
> Original Estimate: 1h
> Remaining Estimate: 1h
>
> Our performance team has identified several areas of improvement to reduce
> garbage collection and CPU usage.
> First, we reduced the amount of StringBuilders created in HTTPUtils.java and
> ResourceUtils.java.
> Second, we created JAXRSUtils.doMimeTypesIntersect() - a method similar to
> JAXRSUtils.intersectMimeTypes - that doesn't create a HashSet but instead
> returns a boolean when we just need to know if they intersect.
> Third, we found that getting the annotations for parameters to create
> constructor arguments in PerRequestResourceProvider.java is expensive, so we
> cache them in the constructor instead of getting them via reflection every
> request.
> These changes combined result in a ~1.5-2% performance increase.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)