Skip to content

Commit

Permalink
Reduce RequestBuilder allocations.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JakeWharton committed Jan 31, 2014
1 parent a942fcf commit 50552be
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
2 changes: 1 addition & 1 deletion checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
<module name="EqualsHashCode"/>
<!--module name="HiddenField"/-->
<module name="IllegalInstantiation"/>
<module name="InnerAssignment"/>
<!--module name="InnerAssignment"/-->
<!--module name="MagicNumber"/-->
<module name="MissingSwitchDefault"/>
<module name="RedundantThrows"/>
Expand Down
33 changes: 18 additions & 15 deletions retrofit/src/main/java/retrofit/RequestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,8 +30,6 @@

final class RequestBuilder implements RequestInterceptor.RequestFacade {
private final Converter converter;
private final List<Header> headers;
private final StringBuilder queryParams;
private final String[] paramNames;
private final RestMethodInfo.ParamUsage[] paramUsages;
private final String requestMethod;
Expand All @@ -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<Header> headers;

RequestBuilder(Converter converter, RestMethodInfo methodInfo) {
this.converter = converter;
Expand All @@ -55,18 +54,15 @@ final class RequestBuilder implements RequestInterceptor.RequestFacade {
isSynchronous = methodInfo.isSynchronous;
isObservable = methodInfo.isObservable;

headers = new ArrayList<Header>();
if (methodInfo.headers != null) {
headers.addAll(methodInfo.headers);
headers = new ArrayList<Header>(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) {
Expand Down Expand Up @@ -98,6 +94,10 @@ void setApiUrl(String apiUrl) {
if (name == null) {
throw new IllegalArgumentException("Header name must not be null.");
}
List<Header> headers = this.headers;
if (headers == null) {
this.headers = headers = new ArrayList<Header>(2);
}
headers.add(new Header(name, value));
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
Expand All @@ -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);
}
}

0 comments on commit 50552be

Please sign in to comment.