mirror of https://github.com/apache/lucene.git
Optimization: Use precalculated IOContexts for withReadAdvice() to not create new instances all the time (#13245)
This commit is contained in:
parent
69172b14ce
commit
11eb0cc317
|
@ -16,6 +16,7 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.lucene.store;
|
package org.apache.lucene.store;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -49,6 +50,9 @@ public record IOContext(
|
||||||
|
|
||||||
public static final IOContext READONCE = new IOContext(ReadAdvice.SEQUENTIAL);
|
public static final IOContext READONCE = new IOContext(ReadAdvice.SEQUENTIAL);
|
||||||
|
|
||||||
|
private static final IOContext[] DEFAULT_READADVICE_CACHE =
|
||||||
|
Arrays.stream(ReadAdvice.values()).map(IOContext::new).toArray(IOContext[]::new);
|
||||||
|
|
||||||
@SuppressWarnings("incomplete-switch")
|
@SuppressWarnings("incomplete-switch")
|
||||||
public IOContext {
|
public IOContext {
|
||||||
Objects.requireNonNull(context, "context must not be null");
|
Objects.requireNonNull(context, "context must not be null");
|
||||||
|
@ -93,7 +97,7 @@ public record IOContext(
|
||||||
*/
|
*/
|
||||||
public IOContext withReadAdvice(ReadAdvice advice) {
|
public IOContext withReadAdvice(ReadAdvice advice) {
|
||||||
if (context == Context.DEFAULT) {
|
if (context == Context.DEFAULT) {
|
||||||
return new IOContext(advice);
|
return DEFAULT_READADVICE_CACHE[advice.ordinal()];
|
||||||
} else {
|
} else {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue