mirror of
https://github.com/elastic/elasticsearch.git
synced 2025-04-24 23:27:25 -04:00
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:
parent
731a412326
commit
43f3032db8
1 changed files with 36 additions and 19 deletions
|
@ -9,8 +9,6 @@
|
||||||
|
|
||||||
package org.elasticsearch.core;
|
package org.elasticsearch.core;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.UncheckedIOException;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
@ -21,11 +19,17 @@ public enum Releasables {
|
||||||
|
|
||||||
/** Release the provided {@link Releasable}s. */
|
/** Release the provided {@link Releasable}s. */
|
||||||
public static void close(Iterable<? extends Releasable> releasables) {
|
public static void close(Iterable<? extends Releasable> releasables) {
|
||||||
|
RuntimeException firstException = null;
|
||||||
|
for (final Releasable releasable : releasables) {
|
||||||
try {
|
try {
|
||||||
// this does the right thing with respect to add suppressed and not wrapping errors etc.
|
close(releasable);
|
||||||
IOUtils.close(releasables);
|
} catch (RuntimeException e) {
|
||||||
} catch (IOException e) {
|
firstException = useOrSuppress(firstException, e);
|
||||||
throw new UncheckedIOException(e);
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (firstException != null) {
|
||||||
|
throw firstException;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,7 +42,18 @@ public enum Releasables {
|
||||||
|
|
||||||
/** Release the provided {@link Releasable}s. */
|
/** Release the provided {@link Releasable}s. */
|
||||||
public static void close(Releasable... releasables) {
|
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. */
|
/** 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. */
|
/** Release the provided {@link Releasable}s, ignoring exceptions. */
|
||||||
public static void closeWhileHandlingException(Releasable... releasables) {
|
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 RuntimeException useOrSuppress(RuntimeException firstException, RuntimeException e) {
|
||||||
private static void close(boolean success, Releasable... releasables) {
|
if (firstException == null || firstException == e) {
|
||||||
try {
|
return e;
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
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
|
/** Wrap several releasables into a single one. This is typically useful for use with try-with-resources: for example let's assume
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue