diff --git a/lucene/core/src/java/org/apache/lucene/store/IOContext.java b/lucene/core/src/java/org/apache/lucene/store/IOContext.java index 860c5f9f6e5..f5b170ddd1f 100644 --- a/lucene/core/src/java/org/apache/lucene/store/IOContext.java +++ b/lucene/core/src/java/org/apache/lucene/store/IOContext.java @@ -16,6 +16,7 @@ */ package org.apache.lucene.store; +import java.util.Arrays; import java.util.Objects; /** @@ -49,6 +50,9 @@ public record IOContext( 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") public IOContext { Objects.requireNonNull(context, "context must not be null"); @@ -93,7 +97,7 @@ public record IOContext( */ public IOContext withReadAdvice(ReadAdvice advice) { if (context == Context.DEFAULT) { - return new IOContext(advice); + return DEFAULT_READADVICE_CACHE[advice.ordinal()]; } else { return this; }