You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by so...@apache.org on 2026/04/01 10:41:30 UTC
(lucene) branch main updated: Deferring lambda in TermStates.java according to prefetch (#15627)
This is an automated email from the ASF dual-hosted git repository.
sokolov pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/lucene.git
The following commit(s) were added to refs/heads/main by this push:
new 8b1bad4fb1b Deferring lambda in TermStates.java according to prefetch (#15627)
8b1bad4fb1b is described below
commit 8b1bad4fb1b4fae9921a45bb9e3eb69a3b7fa112
Author: Shubham Sharma <sh...@gmail.com>
AuthorDate: Wed Apr 1 11:41:25 2026 +0100
Deferring lambda in TermStates.java according to prefetch (#15627)
Co-authored-by: Egor Potemkin <eg...@gmail.com>
---
lucene/CHANGES.txt | 2 ++
.../lucene90/blocktree/SegmentTermsEnumFrame.java | 6 ++--
.../lucene103/blocktree/SegmentTermsEnum.java | 41 +++++++++++++--------
.../lucene103/blocktree/SegmentTermsEnumFrame.java | 6 ++--
.../codecs/lucene90/Lucene90NormsProducer.java | 3 +-
.../java/org/apache/lucene/index/TermStates.java | 42 +++++++++++-----------
.../java/org/apache/lucene/store/IndexInput.java | 10 ++++--
.../lucene/store/MemorySegmentIndexInput.java | 38 ++++++++++++--------
.../org/apache/lucene/store/RandomAccessInput.java | 4 ++-
.../org/apache/lucene/util/IOBooleanSupplier.java | 9 +++++
.../lucene90/TestLucene90StoredFieldsFormat.java | 3 +-
.../lucene90/TestLucene90TermVectorsFormat.java | 3 +-
.../lucene/tests/store/MockIndexInputWrapper.java | 4 +--
.../tests/store/SerialIOCountingDirectory.java | 4 ++-
14 files changed, 109 insertions(+), 66 deletions(-)
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 85ef77209cc..4aaf30e7287 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -74,6 +74,8 @@ API Changes
* GITHUB#15295 : Switched to a fixed CFS threshold (Shubham Sharma)
+* GITHUB#15627 : Deferred lambda in TermStates.java according to prefetch (Shubham Sharma)
+
New Features
---------------------
* GITHUB#15505: Upgrade snowball to 2d2e312df56f2ede014a4ffb3e91e6dea43c24be. New stemmer: PolishStemmer (and
diff --git a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene90/blocktree/SegmentTermsEnumFrame.java b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene90/blocktree/SegmentTermsEnumFrame.java
index b7affa5c6f5..126b510d272 100644
--- a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene90/blocktree/SegmentTermsEnumFrame.java
+++ b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene90/blocktree/SegmentTermsEnumFrame.java
@@ -133,10 +133,10 @@ final class SegmentTermsEnumFrame {
loadBlock();
}
- void prefetchBlock() throws IOException {
+ boolean prefetchBlock() throws IOException {
if (nextEnt != -1) {
// Already loaded
- return;
+ return false;
}
// Clone the IndexInput lazily, so that consumers
@@ -145,7 +145,7 @@ final class SegmentTermsEnumFrame {
ste.initIndexInput();
// TODO: Could we know the number of bytes to prefetch?
- ste.in.prefetch(fp, 1);
+ return ste.in.prefetch(fp, 1);
}
/* Does initial decode of next block of terms; this
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene103/blocktree/SegmentTermsEnum.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene103/blocktree/SegmentTermsEnum.java
index c0c0991dfce..7b99252b7eb 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene103/blocktree/SegmentTermsEnum.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene103/blocktree/SegmentTermsEnum.java
@@ -476,26 +476,37 @@ final class SegmentTermsEnum extends BaseTermsEnum {
private IOBooleanSupplier getIOBooleanSupplier(BytesRef target, boolean prefetch)
throws IOException {
+ boolean doDefer;
if (prefetch) {
- currentFrame.prefetchBlock();
+ doDefer = currentFrame.prefetchBlock();
+ } else {
+ doDefer = false;
}
- return () -> {
- currentFrame.loadBlock();
+ return new IOBooleanSupplier() {
+ @Override
+ public boolean get() throws IOException {
+ currentFrame.loadBlock();
- final SeekStatus result = currentFrame.scanToTerm(target, true);
- if (result == SeekStatus.FOUND) {
- // if (DEBUG) {
- // System.out.println(" return FOUND term=" + term.utf8ToString() + " " + term);
- // }
- return true;
- } else {
- // if (DEBUG) {
- // System.out.println(" got result " + result + "; return NOT_FOUND term=" +
- // term.utf8ToString());
- // }
+ final SeekStatus result = currentFrame.scanToTerm(target, true);
+ if (result == SeekStatus.FOUND) {
+ // if (DEBUG) {
+ // System.out.println(" return FOUND term=" + term.utf8ToString() + " " + term);
+ // }
+ return true;
+ } else {
+ // if (DEBUG) {
+ // System.out.println(" got result " + result + "; return NOT_FOUND term=" +
+ // term.utf8ToString());
+ // }
+
+ return false;
+ }
+ }
- return false;
+ @Override
+ public boolean doDefer() {
+ return doDefer;
}
};
}
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene103/blocktree/SegmentTermsEnumFrame.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene103/blocktree/SegmentTermsEnumFrame.java
index dc3a53c334c..870c1ce1906 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene103/blocktree/SegmentTermsEnumFrame.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene103/blocktree/SegmentTermsEnumFrame.java
@@ -135,10 +135,10 @@ final class SegmentTermsEnumFrame {
loadBlock();
}
- void prefetchBlock() throws IOException {
+ boolean prefetchBlock() throws IOException {
if (nextEnt != -1) {
// Already loaded
- return;
+ return false;
}
// Clone the IndexInput lazily, so that consumers
@@ -147,7 +147,7 @@ final class SegmentTermsEnumFrame {
ste.initIndexInput();
// TODO: Could we know the number of bytes to prefetch?
- ste.in.prefetch(fp, 1);
+ return ste.in.prefetch(fp, 1);
}
/* Does initial decode of next block of terms; this
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90NormsProducer.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90NormsProducer.java
index 1965f49fab8..efbea3e22c7 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90NormsProducer.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90NormsProducer.java
@@ -345,8 +345,9 @@ final class Lucene90NormsProducer extends NormsProducer implements Cloneable {
}
@Override
- public void prefetch(long offset, long length) throws IOException {
+ public boolean prefetch(long offset, long length) throws IOException {
// Not delegating to the wrapped instance on purpose. This is only used for merging.
+ return false;
}
};
}
diff --git a/lucene/core/src/java/org/apache/lucene/index/TermStates.java b/lucene/core/src/java/org/apache/lucene/index/TermStates.java
index d45c4f9299e..286fd8b5db5 100644
--- a/lucene/core/src/java/org/apache/lucene/index/TermStates.java
+++ b/lucene/core/src/java/org/apache/lucene/index/TermStates.java
@@ -193,28 +193,28 @@ public final class TermStates {
this.states[ctx.ord] = EMPTY_TERMSTATE;
return null;
}
- return () -> {
- if (this.states[ctx.ord] == null) {
- TermState state = null;
- if (termExistsSupplier.get()) {
- state = termsEnum.termState();
- this.states[ctx.ord] = state;
- } else {
- this.states[ctx.ord] = EMPTY_TERMSTATE;
- }
- }
- TermState state = this.states[ctx.ord];
- if (state == EMPTY_TERMSTATE) {
- return null;
- }
- return state;
- };
- }
- TermState state = this.states[ctx.ord];
- if (state == EMPTY_TERMSTATE) {
- return null;
+ IOSupplier<TermState> stateSupplier =
+ () -> {
+ if (this.states[ctx.ord] == null) {
+ if (termExistsSupplier.get()) {
+ this.states[ctx.ord] = termsEnum.termState();
+ } else {
+ this.states[ctx.ord] = EMPTY_TERMSTATE;
+ }
+ }
+ TermState termState = this.states[ctx.ord];
+ return termState == EMPTY_TERMSTATE ? null : termState;
+ };
+ if (termExistsSupplier.doDefer()) {
+ return stateSupplier;
+ } else {
+ stateSupplier.get();
+ TermState termState = this.states[ctx.ord];
+ return termState == EMPTY_TERMSTATE ? null : () -> termState;
+ }
}
- return () -> state;
+ TermState termState = this.states[ctx.ord];
+ return termState == EMPTY_TERMSTATE ? null : () -> termState;
}
/**
diff --git a/lucene/core/src/java/org/apache/lucene/store/IndexInput.java b/lucene/core/src/java/org/apache/lucene/store/IndexInput.java
index bf445c2641d..5c99098fdc2 100644
--- a/lucene/core/src/java/org/apache/lucene/store/IndexInput.java
+++ b/lucene/core/src/java/org/apache/lucene/store/IndexInput.java
@@ -202,8 +202,8 @@ public abstract class IndexInput extends DataInput implements Closeable {
}
@Override
- public void prefetch(long offset, long length) throws IOException {
- slice.prefetch(offset, length);
+ public boolean prefetch(long offset, long length) throws IOException {
+ return slice.prefetch(offset, length);
}
@Override
@@ -223,8 +223,12 @@ public abstract class IndexInput extends DataInput implements Closeable {
*
* @param offset start offset
* @param length the number of bytes to prefetch
+ * @return true if prefetch actually prefetched something, hence user can benefit from deferring
+ * reading the prefetched memory block.
*/
- public void prefetch(long offset, long length) throws IOException {}
+ public boolean prefetch(long offset, long length) throws IOException {
+ return false;
+ }
/**
* Optional method: Updates the {@code IOContext} to specify a new read access pattern. IndexInput
diff --git a/lucene/core/src/java/org/apache/lucene/store/MemorySegmentIndexInput.java b/lucene/core/src/java/org/apache/lucene/store/MemorySegmentIndexInput.java
index 2debe19fdde..6ce3a99a12d 100644
--- a/lucene/core/src/java/org/apache/lucene/store/MemorySegmentIndexInput.java
+++ b/lucene/core/src/java/org/apache/lucene/store/MemorySegmentIndexInput.java
@@ -32,7 +32,7 @@ import java.util.function.Function;
import org.apache.lucene.util.ArrayUtil;
import org.apache.lucene.util.BitUtil;
import org.apache.lucene.util.Constants;
-import org.apache.lucene.util.IOConsumer;
+import org.apache.lucene.util.IOFunction;
/**
* Base IndexInput implementation that uses an array of MemorySegments to represent a file.
@@ -328,9 +328,9 @@ abstract class MemorySegmentIndexInput extends IndexInput implements MemorySegme
}
@Override
- public void prefetch(long offset, long length) throws IOException {
+ public boolean prefetch(long offset, long length) throws IOException {
if (NATIVE_ACCESS.isEmpty()) {
- return;
+ return false;
}
ensureOpen();
@@ -340,11 +340,11 @@ abstract class MemorySegmentIndexInput extends IndexInput implements MemorySegme
// power of two. There is a good chance that a good chunk of this index input is cached in
// physical memory. Let's skip the overhead of the madvise system call, we'll be trying again
// on the next power of two of the counter.
- return;
+ return false;
}
final NativeAccess nativeAccess = NATIVE_ACCESS.get();
- advise(
+ return advise(
offset,
length,
segment -> {
@@ -352,7 +352,9 @@ abstract class MemorySegmentIndexInput extends IndexInput implements MemorySegme
// We have a cache miss on at least one page, let's reset the counter.
sharedPrefetchCounter.set(0);
nativeAccess.madviseWillNeed(segment);
+ return true;
}
+ return false;
});
}
@@ -369,14 +371,21 @@ abstract class MemorySegmentIndexInput extends IndexInput implements MemorySegme
long offset = 0;
for (MemorySegment seg : segments) {
- advise(offset, seg.byteSize(), segment -> nativeAccess.madvise(segment, readAdvice));
+ advise(
+ offset,
+ seg.byteSize(),
+ segment -> {
+ nativeAccess.madvise(segment, readAdvice);
+ return true;
+ });
offset += seg.byteSize();
}
}
- void advise(long offset, long length, IOConsumer<MemorySegment> advice) throws IOException {
+ boolean advise(long offset, long length, IOFunction<MemorySegment, Boolean> advice)
+ throws IOException {
if (NATIVE_ACCESS.isEmpty()) {
- return;
+ return false;
}
ensureOpen();
@@ -404,12 +413,12 @@ abstract class MemorySegmentIndexInput extends IndexInput implements MemorySegme
length -= nativeAccess.getPageSize();
if (length <= 0) {
// This segment has no data beyond the first page.
- return;
+ return false;
}
}
final MemorySegment advisedSlice = segment.asSlice(offset, length);
- advice.accept(advisedSlice);
+ return advice.apply(advisedSlice);
} catch (IndexOutOfBoundsException _) {
throw new EOFException("Read past EOF: " + this);
} catch (NullPointerException | IllegalStateException e) {
@@ -615,6 +624,7 @@ abstract class MemorySegmentIndexInput extends IndexInput implements MemorySegme
slice.length,
segment -> {
nativeAccess.madvise(segment, advice);
+ return true;
});
}
}
@@ -804,9 +814,9 @@ abstract class MemorySegmentIndexInput extends IndexInput implements MemorySegme
}
@Override
- public void prefetch(long offset, long length) throws IOException {
+ public boolean prefetch(long offset, long length) throws IOException {
Objects.checkFromIndexSize(offset, length, this.length);
- super.prefetch(offset, length);
+ return super.prefetch(offset, length);
}
}
@@ -904,9 +914,9 @@ abstract class MemorySegmentIndexInput extends IndexInput implements MemorySegme
}
@Override
- public void prefetch(long offset, long length) throws IOException {
+ public boolean prefetch(long offset, long length) throws IOException {
Objects.checkFromIndexSize(offset, length, this.length);
- super.prefetch(this.offset + offset, length);
+ return super.prefetch(this.offset + offset, length);
}
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/store/RandomAccessInput.java b/lucene/core/src/java/org/apache/lucene/store/RandomAccessInput.java
index 44127d90a98..28d2005b413 100644
--- a/lucene/core/src/java/org/apache/lucene/store/RandomAccessInput.java
+++ b/lucene/core/src/java/org/apache/lucene/store/RandomAccessInput.java
@@ -77,7 +77,9 @@ public interface RandomAccessInput {
*
* @see IndexInput#prefetch
*/
- default void prefetch(long offset, long length) throws IOException {}
+ default boolean prefetch(long offset, long length) throws IOException {
+ return false;
+ }
/**
* Returns a hint whether all the contents of this input are resident in physical memory.
diff --git a/lucene/core/src/java/org/apache/lucene/util/IOBooleanSupplier.java b/lucene/core/src/java/org/apache/lucene/util/IOBooleanSupplier.java
index 4100c6c53c5..fd5d71d7307 100644
--- a/lucene/core/src/java/org/apache/lucene/util/IOBooleanSupplier.java
+++ b/lucene/core/src/java/org/apache/lucene/util/IOBooleanSupplier.java
@@ -34,4 +34,13 @@ public interface IOBooleanSupplier {
* @throws IOException if supplying the result throws an {@link IOException}
*/
boolean get() throws IOException;
+
+ /**
+ * Returns whether the TermState get should be deferred.
+ *
+ * @return {@code true} if the TermState get should be deferred, {@code false} otherwise
+ */
+ default boolean doDefer() {
+ return false;
+ }
}
diff --git a/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestLucene90StoredFieldsFormat.java b/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestLucene90StoredFieldsFormat.java
index ae9c6d7b53f..67ad3d54dd4 100644
--- a/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestLucene90StoredFieldsFormat.java
+++ b/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestLucene90StoredFieldsFormat.java
@@ -66,9 +66,10 @@ public class TestLucene90StoredFieldsFormat extends BaseStoredFieldsFormatTestCa
}
@Override
- public void prefetch(long offset, long length) throws IOException {
+ public boolean prefetch(long offset, long length) throws IOException {
in.prefetch(offset, length);
counter.incrementAndGet();
+ return true;
}
@Override
diff --git a/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestLucene90TermVectorsFormat.java b/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestLucene90TermVectorsFormat.java
index 8cc6dda1c1d..40ca3ab41d1 100644
--- a/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestLucene90TermVectorsFormat.java
+++ b/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestLucene90TermVectorsFormat.java
@@ -68,9 +68,10 @@ public class TestLucene90TermVectorsFormat extends BaseTermVectorsFormatTestCase
}
@Override
- public void prefetch(long offset, long length) throws IOException {
+ public boolean prefetch(long offset, long length) throws IOException {
in.prefetch(offset, length);
counter.incrementAndGet();
+ return true;
}
@Override
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/store/MockIndexInputWrapper.java b/lucene/test-framework/src/java/org/apache/lucene/tests/store/MockIndexInputWrapper.java
index fcf29fcb365..9e54cb83ac6 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/store/MockIndexInputWrapper.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/store/MockIndexInputWrapper.java
@@ -171,10 +171,10 @@ public class MockIndexInputWrapper extends FilterIndexInput {
}
@Override
- public void prefetch(long offset, long length) throws IOException {
+ public boolean prefetch(long offset, long length) throws IOException {
ensureOpen();
ensureAccessible();
- in.prefetch(offset, length);
+ return in.prefetch(offset, length);
}
@Override
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/store/SerialIOCountingDirectory.java b/lucene/test-framework/src/java/org/apache/lucene/tests/store/SerialIOCountingDirectory.java
index 778f7e68659..f087e7fd20f 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/store/SerialIOCountingDirectory.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/store/SerialIOCountingDirectory.java
@@ -126,7 +126,7 @@ public class SerialIOCountingDirectory extends FilterDirectory {
}
@Override
- public void prefetch(long offset, long length) throws IOException {
+ public boolean prefetch(long offset, long length) throws IOException {
final long firstPage = (sliceOffset + offset) >> PAGE_SHIFT;
final long lastPage = (sliceOffset + offset + length - 1) >> PAGE_SHIFT;
@@ -152,6 +152,8 @@ public class SerialIOCountingDirectory extends FilterDirectory {
for (long page = firstPage; page <= lastPage; ++page) {
pendingPages.add(page);
}
+
+ return true;
}
@Override