diff --git a/lucene/core/src/java21/org/apache/lucene/store/MemorySegmentIndexInputProvider.java b/lucene/core/src/java21/org/apache/lucene/store/MemorySegmentIndexInputProvider.java index 35f1d53ee51c..bcf58d95d015 100644 --- a/lucene/core/src/java21/org/apache/lucene/store/MemorySegmentIndexInputProvider.java +++ b/lucene/core/src/java21/org/apache/lucene/store/MemorySegmentIndexInputProvider.java @@ -120,10 +120,15 @@ private final MemorySegment[] map( } private OptionalInt mapContextToMadvise(IOContext context) { + // Merging always wins and implies sequential access, because kernel is advised to free pages + // after use: + if (context.context == Context.MERGE) { + return OptionalInt.of(NativeAccess.POSIX_MADV_SEQUENTIAL); + } if (context.randomAccess) { return OptionalInt.of(NativeAccess.POSIX_MADV_RANDOM); } - if (context.readOnce || context.context == Context.MERGE) { + if (context.readOnce) { return OptionalInt.of(NativeAccess.POSIX_MADV_SEQUENTIAL); } return OptionalInt.empty();