Optimization: Use precalculated IOContexts for withReadAdvice() to not create new instances all the time (#13245)

This commit is contained in:
Uwe Schindler 2024-03-29 14:21:11 +01:00 committed by GitHub
parent 69172b14ce
commit 11eb0cc317
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 5 additions and 1 deletions

View File

@ -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;
}