Inline some methods into Releasables (#123273)

Bit of a random find. Looks like we're effectively spending something
like ~0.5% of the total CPU time on resolving virtual calls for these
methods. Inlining `IOUtils` reduces their size quite a bit, removes
dead-code for rethrowing `IOException` that is never actually thrown
and hopefully as a result improve compilation a little here.
This commit is contained in:
Armin Braun 2025-02-27 12:09:36 +01:00 committed by GitHub
parent 731a412326
commit 43f3032db8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -9,8 +9,6 @@
package org.elasticsearch.core;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicReference;
@ -21,11 +19,17 @@ public enum Releasables {
/** Release the provided {@link Releasable}s. */
public static void close(Iterable<? extends Releasable> releasables) {
RuntimeException firstException = null;
for (final Releasable releasable : releasables) {
try {
// this does the right thing with respect to add suppressed and not wrapping errors etc.
IOUtils.close(releasables);
} catch (IOException e) {
throw new UncheckedIOException(e);
close(releasable);
} catch (RuntimeException e) {
firstException = useOrSuppress(firstException, e);
}
}
if (firstException != null) {
throw firstException;
}
}
@ -38,7 +42,18 @@ public enum Releasables {
/** Release the provided {@link Releasable}s. */
public static void close(Releasable... releasables) {
close(true, releasables);
RuntimeException firstException = null;
for (final Releasable releasable : releasables) {
try {
close(releasable);
} catch (RuntimeException e) {
firstException = useOrSuppress(firstException, e);
}
}
if (firstException != null) {
throw firstException;
}
}
/** Release the provided {@link Releasable}s expecting no exception to by thrown by any of them. */
@ -63,19 +78,21 @@ public enum Releasables {
/** Release the provided {@link Releasable}s, ignoring exceptions. */
public static void closeWhileHandlingException(Releasable... releasables) {
close(false, releasables);
for (final Releasable releasable : releasables) {
try {
close(releasable);
} catch (RuntimeException e) {
// ignored
}
}
}
/** Release the provided {@link Releasable}s, ignoring exceptions if <code>success</code> is {@code false}. */
private static void close(boolean success, Releasable... releasables) {
try {
// this does the right thing with respect to add suppressed and not wrapping errors etc.
IOUtils.close(releasables);
} catch (IOException e) {
if (success) {
throw new UncheckedIOException(e);
}
private static RuntimeException useOrSuppress(RuntimeException firstException, RuntimeException e) {
if (firstException == null || firstException == e) {
return e;
}
firstException.addSuppressed(e);
return firstException;
}
/** Wrap several releasables into a single one. This is typically useful for use with try-with-resources: for example let's assume