Skip to content

Commit 306491a

Browse files
Fix a few toString implementations+usages that affect test performance (#112380)
No need to precompute the toString for `ActionListener` and `Releasable`, that's quite expensive at times. Also string concat is way faster than formating these days, so use that in the transport channels. Lastly, short-circuit some obvious spots in network address serialization and remove code that duplicates the JDK (remove the IPV4 specific forbidden API because it makes no sense, but still needed to disable the check to make the build green because of the exclude on the parent class).
1 parent e2efc0c commit 306491a

File tree

7 files changed

+14
-14
lines changed

7 files changed

+14
-14
lines changed

libs/core/src/main/java/org/elasticsearch/core/Releasables.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,9 @@ public static Releasable assertOnce(final Releasable delegate) {
149149
private final AtomicReference<Exception> firstCompletion = new AtomicReference<>();
150150

151151
private void assertFirstRun() {
152-
var previousRun = firstCompletion.compareAndExchange(null, new Exception(delegate.toString()));
153-
assert previousRun == null : previousRun; // reports the stack traces of both completions
152+
var previousRun = firstCompletion.compareAndExchange(null, new Exception("already executed"));
153+
// reports the stack traces of both completions
154+
assert previousRun == null : new AssertionError(delegate.toString(), previousRun);
154155
}
155156

156157
@Override

libs/core/src/test/java/org/elasticsearch/core/ReleasablesTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public String toString() {
6969
.anyMatch(ste -> ste.toString().contains("CloserWithIdentifiableMethodNames.closeMethod2"))
7070
);
7171
assertTrue(
72-
Arrays.stream(assertionError.getCause().getStackTrace())
72+
Arrays.stream(assertionError.getCause().getCause().getStackTrace())
7373
.anyMatch(ste -> ste.toString().contains("CloserWithIdentifiableMethodNames.closeMethod1"))
7474
);
7575
}

server/src/main/java/org/elasticsearch/action/ActionListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,8 @@ static <Response> ActionListener<Response> assertOnce(ActionListener<Response> d
388388
private final AtomicReference<ElasticsearchException> firstCompletion = new AtomicReference<>();
389389

390390
private void assertFirstRun() {
391-
var previousRun = firstCompletion.compareAndExchange(null, new ElasticsearchException(delegate.toString()));
392-
assert previousRun == null : previousRun; // reports the stack traces of both completions
391+
var previousRun = firstCompletion.compareAndExchange(null, new ElasticsearchException("executed already"));
392+
assert previousRun == null : "[" + delegate + "] " + previousRun; // reports the stack traces of both completions
393393
}
394394

395395
@Override

server/src/main/java/org/elasticsearch/common/network/InetAddresses.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.elasticsearch.common.network;
1919

20+
import org.elasticsearch.core.SuppressForbidden;
2021
import org.elasticsearch.core.Tuple;
2122

2223
import java.net.Inet4Address;
@@ -246,14 +247,14 @@ public static String toUriString(InetAddress ip) {
246247
* @return {@code String} containing the text-formatted IP address
247248
* @since 10.0
248249
*/
250+
@SuppressForbidden(reason = "java.net.Inet4Address#getHostAddress() is fine no need to duplicate its code")
249251
public static String toAddrString(InetAddress ip) {
250252
if (ip == null) {
251253
throw new NullPointerException("ip");
252254
}
253-
if (ip instanceof Inet4Address) {
255+
if (ip instanceof Inet4Address inet4Address) {
254256
// For IPv4, Java's formatting is good enough.
255-
byte[] bytes = ip.getAddress();
256-
return (bytes[0] & 0xff) + "." + (bytes[1] & 0xff) + "." + (bytes[2] & 0xff) + "." + (bytes[3] & 0xff);
257+
return inet4Address.getHostAddress();
257258
}
258259
if ((ip instanceof Inet6Address) == false) {
259260
throw new IllegalArgumentException("ip");

server/src/main/java/org/elasticsearch/common/network/NetworkAddress.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private NetworkAddress() {}
5757
* @return formatted string
5858
*/
5959
public static String format(InetAddress address) {
60-
return format(address, new PortsRange(""));
60+
return InetAddresses.toAddrString(address);
6161
}
6262

6363
/**
@@ -96,7 +96,7 @@ public static String format(InetSocketAddress address) {
9696
* @return formatted string
9797
*/
9898
public static String format(InetAddress address, int port) {
99-
return format(address, new PortsRange(String.valueOf(port)));
99+
return (address instanceof Inet6Address ? InetAddresses.toUriString(address) : InetAddresses.toAddrString(address)) + ":" + port;
100100
}
101101

102102
/**

server/src/main/java/org/elasticsearch/transport/TaskTransportChannel.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
package org.elasticsearch.transport;
1010

1111
import org.elasticsearch.TransportVersion;
12-
import org.elasticsearch.common.Strings;
1312
import org.elasticsearch.core.Releasable;
1413

1514
public class TaskTransportChannel implements TransportChannel {
@@ -58,6 +57,6 @@ public TransportChannel getChannel() {
5857

5958
@Override
6059
public String toString() {
61-
return Strings.format("TaskTransportChannel{task=%d}{%s}", taskId, channel);
60+
return "TaskTransportChannel{task=" + taskId + "}{" + channel + "}";
6261
}
6362
}

server/src/main/java/org/elasticsearch/transport/TcpTransportChannel.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
package org.elasticsearch.transport;
1010

1111
import org.elasticsearch.TransportVersion;
12-
import org.elasticsearch.common.Strings;
1312
import org.elasticsearch.core.Releasable;
1413

1514
public final class TcpTransportChannel implements TransportChannel {
@@ -89,6 +88,6 @@ public TcpChannel getChannel() {
8988

9089
@Override
9190
public String toString() {
92-
return Strings.format("TcpTransportChannel{req=%d}{%s}{%s}", requestId, action, channel);
91+
return "TcpTransportChannel{req=" + requestId + "}{" + action + "}{" + channel + "}";
9392
}
9493
}

0 commit comments

Comments
 (0)