Remove unnecessary iterator allocation in type handlers

HandlerMethodReturnValueHandlerComposite and
AbstractMethodMessageHandler iterate using index over collections
implementing RandomAccess to avoid unnecessary iterators.
This commit is contained in:
Carter Kozak 2018-04-10 15:55:35 -07:00 committed by Rossen Stoyanchev
parent 5b9e7e44e0
commit e2febbdd8c
3 changed files with 9 additions and 9 deletions

View File

@ -84,7 +84,7 @@ public abstract class AbstractMethodMessageHandler<T>
protected final Log logger = LogFactory.getLog(getClass()); protected final Log logger = LogFactory.getLog(getClass());
private Collection<String> destinationPrefixes = new ArrayList<>(); private final List<String> destinationPrefixes = new ArrayList<>();
private final List<HandlerMethodArgumentResolver> customArgumentResolvers = new ArrayList<>(4); private final List<HandlerMethodArgumentResolver> customArgumentResolvers = new ArrayList<>(4);
@ -428,7 +428,9 @@ public abstract class AbstractMethodMessageHandler<T>
if (CollectionUtils.isEmpty(this.destinationPrefixes)) { if (CollectionUtils.isEmpty(this.destinationPrefixes)) {
return destination; return destination;
} }
for (String prefix : this.destinationPrefixes) { // Avoid unnecessary iterator allocation
for (int i = 0, size = this.destinationPrefixes.size(); i < size; i++) {
String prefix = this.destinationPrefixes.get(i);
if (destination.startsWith(prefix)) { if (destination.startsWith(prefix)) {
return destination.substring(prefix.length()); return destination.substring(prefix.length());
} }

View File

@ -71,9 +71,7 @@ public class HandlerMethodArgumentResolverComposite implements HandlerMethodArgu
@Nullable List<? extends HandlerMethodArgumentResolver> argumentResolvers) { @Nullable List<? extends HandlerMethodArgumentResolver> argumentResolvers) {
if (argumentResolvers != null) { if (argumentResolvers != null) {
for (HandlerMethodArgumentResolver resolver : argumentResolvers) { this.argumentResolvers.addAll(argumentResolvers);
this.argumentResolvers.add(resolver);
}
} }
return this; return this;
} }

View File

@ -71,9 +71,7 @@ public class HandlerMethodReturnValueHandlerComposite implements AsyncHandlerMet
@Nullable List<? extends HandlerMethodReturnValueHandler> handlers) { @Nullable List<? extends HandlerMethodReturnValueHandler> handlers) {
if (handlers != null) { if (handlers != null) {
for (HandlerMethodReturnValueHandler handler : handlers) { this.returnValueHandlers.addAll(handlers);
this.returnValueHandlers.add(handler);
}
} }
return this; return this;
} }
@ -85,7 +83,9 @@ public class HandlerMethodReturnValueHandlerComposite implements AsyncHandlerMet
@Nullable @Nullable
private HandlerMethodReturnValueHandler getReturnValueHandler(MethodParameter returnType) { private HandlerMethodReturnValueHandler getReturnValueHandler(MethodParameter returnType) {
for (HandlerMethodReturnValueHandler handler : this.returnValueHandlers) { // Avoid allocating an iterator
for (int i = 0, size = this.returnValueHandlers.size(); i < size; i++) {
HandlerMethodReturnValueHandler handler = this.returnValueHandlers.get(i);
if (handler.supportsReturnType(returnType)) { if (handler.supportsReturnType(returnType)) {
return handler; return handler;
} }