From ea3f5062491eef8ce6f1dc81b831f97eba43ee6b Mon Sep 17 00:00:00 2001 From: Carl Mastrangelo Date: Fri, 2 Sep 2016 14:07:03 -0700 Subject: [PATCH] core: avoid allocating Iterators in EquivalentAddressGroup, which is called for each new RPC MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Benchmarked with 3 runs of 4 forks Before: Benchmark (direct) (transport) Mode Cnt Score Error Units TransportBenchmark.unaryCall1024 true NETTY sample 255593 156248.670 ± 563.514 ns/op Benchmark (direct) (transport) Mode Cnt Score Error Units TransportBenchmark.unaryCall1024 true NETTY sample 261443 152753.415 ± 500.957 ns/op Benchmark (direct) (transport) Mode Cnt Score Error Units TransportBenchmark.unaryCall1024 true NETTY sample 258978 154205.374 ± 453.808 ns/op After: Benchmark (direct) (transport) Mode Cnt Score Error Units TransportBenchmark.unaryCall1024 true NETTY sample 262194 152313.101 ± 502.563 ns/op Benchmark (direct) (transport) Mode Cnt Score Error Units TransportBenchmark.unaryCall1024 true NETTY sample 264811 150809.474 ± 477.459 ns/op Benchmark (direct) (transport) Mode Cnt Score Error Units TransportBenchmark.unaryCall1024 true NETTY sample 263606 151501.729 ± 593.149 ns/op --- .../java/io/grpc/EquivalentAddressGroup.java | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/io/grpc/EquivalentAddressGroup.java b/core/src/main/java/io/grpc/EquivalentAddressGroup.java index 3d399d59f5..4fe963eafe 100644 --- a/core/src/main/java/io/grpc/EquivalentAddressGroup.java +++ b/core/src/main/java/io/grpc/EquivalentAddressGroup.java @@ -50,13 +50,26 @@ public final class EquivalentAddressGroup { private final List addrs; + /** + * {@link SocketAddress} docs say that the addresses are immutable, so we cache the hashCode. + */ + private final int hashCode; + + /** + * List constructor. + */ public EquivalentAddressGroup(List addrs) { Preconditions.checkArgument(!addrs.isEmpty(), "addrs is empty"); this.addrs = Collections.unmodifiableList(new ArrayList(addrs)); + hashCode = this.addrs.hashCode(); } + /** + * Singleton constructor. + */ public EquivalentAddressGroup(SocketAddress addr) { this.addrs = Collections.singletonList(addr); + hashCode = addrs.hashCode(); } /** @@ -73,7 +86,8 @@ public final class EquivalentAddressGroup { @Override public int hashCode() { - return addrs.hashCode(); + // Avoids creating an iterator on the underlying array list. + return hashCode; } @Override @@ -81,6 +95,16 @@ public final class EquivalentAddressGroup { if (!(other instanceof EquivalentAddressGroup)) { return false; } - return addrs.equals(((EquivalentAddressGroup) other).addrs); + EquivalentAddressGroup that = (EquivalentAddressGroup) other; + if (addrs.size() != that.addrs.size()) { + return false; + } + // Avoids creating an iterator on the underlying array list. + for (int i = 0; i < addrs.size(); i++) { + if (!addrs.get(i).equals(that.addrs.get(i))) { + return false; + } + } + return true; } }