core: avoid allocating Iterators in EquivalentAddressGroup, which is called for each new RPC

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
This commit is contained in:
Carl Mastrangelo 2016-09-02 14:07:03 -07:00
parent c4f7f5c4fd
commit ea3f506249
1 changed files with 26 additions and 2 deletions

View File

@ -50,13 +50,26 @@ public final class EquivalentAddressGroup {
private final List<SocketAddress> addrs;
/**
* {@link SocketAddress} docs say that the addresses are immutable, so we cache the hashCode.
*/
private final int hashCode;
/**
* List constructor.
*/
public EquivalentAddressGroup(List<SocketAddress> addrs) {
Preconditions.checkArgument(!addrs.isEmpty(), "addrs is empty");
this.addrs = Collections.unmodifiableList(new ArrayList<SocketAddress>(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;
}
}