mirror of https://github.com/grpc/grpc-java.git
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:
parent
c4f7f5c4fd
commit
ea3f506249
|
|
@ -50,13 +50,26 @@ public final class EquivalentAddressGroup {
|
||||||
|
|
||||||
private final List<SocketAddress> addrs;
|
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) {
|
public EquivalentAddressGroup(List<SocketAddress> addrs) {
|
||||||
Preconditions.checkArgument(!addrs.isEmpty(), "addrs is empty");
|
Preconditions.checkArgument(!addrs.isEmpty(), "addrs is empty");
|
||||||
this.addrs = Collections.unmodifiableList(new ArrayList<SocketAddress>(addrs));
|
this.addrs = Collections.unmodifiableList(new ArrayList<SocketAddress>(addrs));
|
||||||
|
hashCode = this.addrs.hashCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Singleton constructor.
|
||||||
|
*/
|
||||||
public EquivalentAddressGroup(SocketAddress addr) {
|
public EquivalentAddressGroup(SocketAddress addr) {
|
||||||
this.addrs = Collections.singletonList(addr);
|
this.addrs = Collections.singletonList(addr);
|
||||||
|
hashCode = addrs.hashCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -73,7 +86,8 @@ public final class EquivalentAddressGroup {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return addrs.hashCode();
|
// Avoids creating an iterator on the underlying array list.
|
||||||
|
return hashCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -81,6 +95,16 @@ public final class EquivalentAddressGroup {
|
||||||
if (!(other instanceof EquivalentAddressGroup)) {
|
if (!(other instanceof EquivalentAddressGroup)) {
|
||||||
return false;
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue