From 11eb0cc317771bdd78c61d01d712b62014f662b2 Mon Sep 17 00:00:00 2001 From: Uwe Schindler Date: Fri, 29 Mar 2024 14:21:11 +0100 Subject: [PATCH] Optimization: Use precalculated IOContexts for withReadAdvice() to not create new instances all the time (#13245) --- lucene/core/src/java/org/apache/lucene/store/IOContext.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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; }