From 74581924ccd16eb670f2dad9a219c318e9b92989 Mon Sep 17 00:00:00 2001 From: Sebastian Bazley Date: Fri, 10 Dec 2010 22:41:07 +0000 Subject: [PATCH] Improve equals performance: no need to check for null; perform cheapest checks first. Use short-cut evaluation git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1044525 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/http/conn/routing/HttpRoute.java | 9 +++--- .../http/conn/routing/RouteTracker.java | 28 +++++-------------- .../org/apache/http/conn/scheme/Scheme.java | 1 - 3 files changed, 12 insertions(+), 26 deletions(-) diff --git a/httpclient/src/main/java/org/apache/http/conn/routing/HttpRoute.java b/httpclient/src/main/java/org/apache/http/conn/routing/HttpRoute.java index 57d45041a..7db7c160d 100644 --- a/httpclient/src/main/java/org/apache/http/conn/routing/HttpRoute.java +++ b/httpclient/src/main/java/org/apache/http/conn/routing/HttpRoute.java @@ -324,22 +324,23 @@ public final boolean isSecure() { /** * Compares this route to another. * - * @param o the object to compare with + * @param obj the object to compare with * * @return true if the argument is the same route, * false */ @Override public final boolean equals(Object obj) { - if (obj == null) return false; if (this == obj) return true; if (obj instanceof HttpRoute) { HttpRoute that = (HttpRoute) obj; - return LangUtils.equals(this.targetHost, that.targetHost) && - LangUtils.equals(this.localAddress, that.localAddress) && + return + // Do the cheapest tests first (this.secure == that.secure) && (this.tunnelled == that.tunnelled) && (this.layered == that.layered) && + LangUtils.equals(this.targetHost, that.targetHost) && + LangUtils.equals(this.localAddress, that.localAddress) && LangUtils.equals(this.proxyChain, that.proxyChain); } else { return false; diff --git a/httpclient/src/main/java/org/apache/http/conn/routing/RouteTracker.java b/httpclient/src/main/java/org/apache/http/conn/routing/RouteTracker.java index 32f3f687c..86ada48ba 100644 --- a/httpclient/src/main/java/org/apache/http/conn/routing/RouteTracker.java +++ b/httpclient/src/main/java/org/apache/http/conn/routing/RouteTracker.java @@ -30,6 +30,7 @@ import java.net.InetAddress; import org.apache.http.annotation.NotThreadSafe; +import org.apache.http.util.LangUtils; import org.apache.http.HttpHost; @@ -293,30 +294,15 @@ public final boolean equals(Object o) { return false; RouteTracker that = (RouteTracker) o; - boolean equal = this.targetHost.equals(that.targetHost); - equal &= - ( this.localAddress == that.localAddress) || - ((this.localAddress != null) && - this.localAddress.equals(that.localAddress)); - equal &= - ( this.proxyChain == that.proxyChain) || - ((this.proxyChain != null) && - (that.proxyChain != null) && - (this.proxyChain.length == that.proxyChain.length)); - // comparison of actual proxies follows below - equal &= + return + // Do the cheapest checks first (this.connected == that.connected) && (this.secure == that.secure) && (this.tunnelled == that.tunnelled) && - (this.layered == that.layered); - - // chain length has been compared above, now check the proxies - if (equal && (this.proxyChain != null)) { - for (int i=0; equal && (i