From 50552be2d52b5522f0e65045558a13925f0d4afb Mon Sep 17 00:00:00 2001 From: Jake Wharton Date: Fri, 31 Jan 2014 14:19:31 -0800 Subject: [PATCH] Reduce RequestBuilder allocations. Do not initialize the header array unless it's used. When headers are specified by the methodInfo, initialize the ArrayList using the copy constructor. Do not initialize the query StringBuilder unless it is used. --- checkstyle.xml | 2 +- .../main/java/retrofit/RequestBuilder.java | 33 ++++++++++--------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/checkstyle.xml b/checkstyle.xml index 7da88bbd1a..4727a6aeb0 100644 --- a/checkstyle.xml +++ b/checkstyle.xml @@ -96,7 +96,7 @@ - + diff --git a/retrofit/src/main/java/retrofit/RequestBuilder.java b/retrofit/src/main/java/retrofit/RequestBuilder.java index 10ba0b76ba..5f209d8c80 100644 --- a/retrofit/src/main/java/retrofit/RequestBuilder.java +++ b/retrofit/src/main/java/retrofit/RequestBuilder.java @@ -20,7 +20,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; - import retrofit.client.Header; import retrofit.client.Request; import retrofit.converter.Converter; @@ -31,8 +30,6 @@ final class RequestBuilder implements RequestInterceptor.RequestFacade { private final Converter converter; - private final List
headers; - private final StringBuilder queryParams; private final String[] paramNames; private final RestMethodInfo.ParamUsage[] paramUsages; private final String requestMethod; @@ -43,8 +40,10 @@ final class RequestBuilder implements RequestInterceptor.RequestFacade { private final MultipartTypedOutput multipartBody; private TypedOutput body; - private String relativeUrl; private String apiUrl; + private String relativeUrl; + private StringBuilder queryParams; + private List
headers; RequestBuilder(Converter converter, RestMethodInfo methodInfo) { this.converter = converter; @@ -55,18 +54,15 @@ final class RequestBuilder implements RequestInterceptor.RequestFacade { isSynchronous = methodInfo.isSynchronous; isObservable = methodInfo.isObservable; - headers = new ArrayList
(); if (methodInfo.headers != null) { - headers.addAll(methodInfo.headers); + headers = new ArrayList
(methodInfo.headers); } - queryParams = new StringBuilder(); - relativeUrl = methodInfo.requestUrl; String requestQuery = methodInfo.requestQuery; if (requestQuery != null) { - queryParams.append('?').append(requestQuery); + queryParams = new StringBuilder().append('?').append(requestQuery); } switch (methodInfo.requestType) { @@ -98,6 +94,10 @@ void setApiUrl(String apiUrl) { if (name == null) { throw new IllegalArgumentException("Header name must not be null."); } + List
headers = this.headers; + if (headers == null) { + this.headers = headers = new ArrayList
(2); + } headers.add(new Header(name, value)); } @@ -154,6 +154,10 @@ void addQueryParam(String name, String value, boolean urlEncodeValue) { value = URLEncoder.encode(String.valueOf(value), "UTF-8"); } StringBuilder queryParams = this.queryParams; + if (queryParams == null) { + this.queryParams = queryParams = new StringBuilder(); + } + queryParams.append(queryParams.length() > 0 ? '&' : '?'); queryParams.append(name).append('=').append(value); } catch (UnsupportedEncodingException e) { @@ -287,8 +291,11 @@ void setArguments(Object[] args) { } Request build() throws UnsupportedEncodingException { - String apiUrl = this.apiUrl; + if (multipartBody != null && multipartBody.getPartCount() == 0) { + throw new IllegalStateException("Multipart requests must contain at least one part."); + } + String apiUrl = this.apiUrl; StringBuilder url = new StringBuilder(apiUrl); if (apiUrl.endsWith("/")) { // We require relative paths to start with '/'. Prevent a double-slash. @@ -298,14 +305,10 @@ Request build() throws UnsupportedEncodingException { url.append(relativeUrl); StringBuilder queryParams = this.queryParams; - if (queryParams.length() > 0) { + if (queryParams != null) { url.append(queryParams); } - if (multipartBody != null && multipartBody.getPartCount() == 0) { - throw new IllegalStateException("Multipart requests must contain at least one part."); - } - return new Request(requestMethod, url.toString(), headers, body); } }