Avoided allocation of rarely used containers.

This commit is contained in:
Simone Bordet 2014-05-15 09:59:31 +02:00
parent eb1900d10b
commit fa51281546
1 changed files with 22 additions and 20 deletions

View File

@ -59,8 +59,6 @@ public class HttpRequest implements Request
{ {
private final HttpFields headers = new HttpFields(); private final HttpFields headers = new HttpFields();
private final Fields params = new Fields(true); private final Fields params = new Fields(true);
private final Map<String, Object> attributes = new HashMap<>();
private final List<RequestListener> requestListeners = new ArrayList<>();
private final List<Response.ResponseListener> responseListeners = new ArrayList<>(); private final List<Response.ResponseListener> responseListeners = new ArrayList<>();
private final AtomicReference<Throwable> aborted = new AtomicReference<>(); private final AtomicReference<Throwable> aborted = new AtomicReference<>();
private final HttpClient client; private final HttpClient client;
@ -78,6 +76,8 @@ public class HttpRequest implements Request
private ContentProvider content; private ContentProvider content;
private boolean followRedirects; private boolean followRedirects;
private List<HttpCookie> cookies; private List<HttpCookie> cookies;
private Map<String, Object> attributes;
private List<RequestListener> requestListeners;
protected HttpRequest(HttpClient client, HttpConversation conversation, URI uri) protected HttpRequest(HttpClient client, HttpConversation conversation, URI uri)
{ {
@ -280,6 +280,8 @@ public class HttpRequest implements Request
@Override @Override
public Request attribute(String name, Object value) public Request attribute(String name, Object value)
{ {
if (attributes == null)
attributes = new HashMap<>(4);
attributes.put(name, value); attributes.put(name, value);
return this; return this;
} }
@ -287,7 +289,7 @@ public class HttpRequest implements Request
@Override @Override
public Map<String, Object> getAttributes() public Map<String, Object> getAttributes()
{ {
return attributes; return attributes != null ? attributes : Collections.<String, Object>emptyMap();
} }
@Override @Override
@ -302,8 +304,8 @@ public class HttpRequest implements Request
{ {
// This method is invoked often in a request/response conversation, // This method is invoked often in a request/response conversation,
// so we avoid allocation if there is no need to filter. // so we avoid allocation if there is no need to filter.
if (type == null) if (type == null || requestListeners == null)
return (List<T>)requestListeners; return requestListeners != null ? (List<T>)requestListeners : Collections.<T>emptyList();
ArrayList<T> result = new ArrayList<>(); ArrayList<T> result = new ArrayList<>();
for (RequestListener listener : requestListeners) for (RequestListener listener : requestListeners)
@ -315,14 +317,13 @@ public class HttpRequest implements Request
@Override @Override
public Request listener(Request.Listener listener) public Request listener(Request.Listener listener)
{ {
this.requestListeners.add(listener); return requestListener(listener);
return this;
} }
@Override @Override
public Request onRequestQueued(final QueuedListener listener) public Request onRequestQueued(final QueuedListener listener)
{ {
this.requestListeners.add(new QueuedListener() return requestListener(new QueuedListener()
{ {
@Override @Override
public void onQueued(Request request) public void onQueued(Request request)
@ -330,13 +331,12 @@ public class HttpRequest implements Request
listener.onQueued(request); listener.onQueued(request);
} }
}); });
return this;
} }
@Override @Override
public Request onRequestBegin(final BeginListener listener) public Request onRequestBegin(final BeginListener listener)
{ {
this.requestListeners.add(new BeginListener() return requestListener(new BeginListener()
{ {
@Override @Override
public void onBegin(Request request) public void onBegin(Request request)
@ -344,13 +344,12 @@ public class HttpRequest implements Request
listener.onBegin(request); listener.onBegin(request);
} }
}); });
return this;
} }
@Override @Override
public Request onRequestHeaders(final HeadersListener listener) public Request onRequestHeaders(final HeadersListener listener)
{ {
this.requestListeners.add(new HeadersListener() return requestListener(new HeadersListener()
{ {
@Override @Override
public void onHeaders(Request request) public void onHeaders(Request request)
@ -358,13 +357,12 @@ public class HttpRequest implements Request
listener.onHeaders(request); listener.onHeaders(request);
} }
}); });
return this;
} }
@Override @Override
public Request onRequestCommit(final CommitListener listener) public Request onRequestCommit(final CommitListener listener)
{ {
this.requestListeners.add(new CommitListener() return requestListener(new CommitListener()
{ {
@Override @Override
public void onCommit(Request request) public void onCommit(Request request)
@ -372,13 +370,12 @@ public class HttpRequest implements Request
listener.onCommit(request); listener.onCommit(request);
} }
}); });
return this;
} }
@Override @Override
public Request onRequestContent(final ContentListener listener) public Request onRequestContent(final ContentListener listener)
{ {
this.requestListeners.add(new ContentListener() return requestListener(new ContentListener()
{ {
@Override @Override
public void onContent(Request request, ByteBuffer content) public void onContent(Request request, ByteBuffer content)
@ -386,13 +383,12 @@ public class HttpRequest implements Request
listener.onContent(request, content); listener.onContent(request, content);
} }
}); });
return this;
} }
@Override @Override
public Request onRequestSuccess(final SuccessListener listener) public Request onRequestSuccess(final SuccessListener listener)
{ {
this.requestListeners.add(new SuccessListener() return requestListener(new SuccessListener()
{ {
@Override @Override
public void onSuccess(Request request) public void onSuccess(Request request)
@ -400,13 +396,12 @@ public class HttpRequest implements Request
listener.onSuccess(request); listener.onSuccess(request);
} }
}); });
return this;
} }
@Override @Override
public Request onRequestFailure(final FailureListener listener) public Request onRequestFailure(final FailureListener listener)
{ {
this.requestListeners.add(new FailureListener() return requestListener(new FailureListener()
{ {
@Override @Override
public void onFailure(Request request, Throwable failure) public void onFailure(Request request, Throwable failure)
@ -414,6 +409,13 @@ public class HttpRequest implements Request
listener.onFailure(request, failure); listener.onFailure(request, failure);
} }
}); });
}
private Request requestListener(RequestListener listener)
{
if (requestListeners == null)
requestListeners = new ArrayList<>();
requestListeners.add(listener);
return this; return this;
} }