You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ho...@apache.org on 2026/10/22 00:02:51 UTC

(lucene) 06/07: Optimize byte array writes in OutputStreamIndexOutput to remove unnecessary overhead (#16119)

This is an automated email from the ASF dual-hosted git repository.

HoustonPutman pushed a commit to branch branch_10x
in repository https://gitbox.apache.org/repos/asf/lucene.git

commit 7b864e497b29fe455f29f621f295ba95c40a0abe
Author: neoReMinD <xu...@gmail.com>
AuthorDate: Wed Jun 3 01:54:43 2026 +0800

    Optimize byte array writes in OutputStreamIndexOutput to remove unnecessary overhead (#16119)
    
    * Optimize byte array writes in OutputStreamIndexOutput to remove unnecessary overhead
    
    (cherry picked from commit c129ceba7da2e438389b9d1c5a4927e138f12876)
---
 lucene/CHANGES.txt                                             |  2 ++
 lucene/core/src/java/org/apache/lucene/store/FSDirectory.java  |  7 +++++--
 .../java/org/apache/lucene/store/OutputStreamIndexOutput.java  | 10 ++++++++++
 3 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 164d98c9c63..fb2e2552603 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -288,6 +288,8 @@ Optimizations
 
 * GITHUB#16089: Upgrade DocIdSetBuilder to use FixedBitSet eagerly once exceeding the threshold (Ke Wei)
 
+* GITHUB#16119: Optimize byte array writes in OutputStreamIndexOutput to remove unnecessary overhead. (neoremind)
+
 Bug Fixes
 ---------------------
 
diff --git a/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java b/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java
index ec3a32f5781..dd422f3dc83 100644
--- a/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java
+++ b/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java
@@ -374,8 +374,11 @@ public abstract class FSDirectory extends BaseDirectory {
 
   final class FSIndexOutput extends OutputStreamIndexOutput {
     /**
-     * The maximum chunk size is 8192 bytes, because file channel mallocs a native buffer outside of
-     * stack if the write buffer size is larger.
+     * We set the chunk size to 8192 bytes as the maximum I/O unit. Under the hood, {@link
+     * java.nio.channels.FileChannel#write} with a heap ByteBuffer allocates a thread-local
+     * temporary direct ByteBuffer and copies data into it before issuing the native write. Capping
+     * the chunk size prevents bloating that native buffer, which is cached per-thread and reused
+     * across writes.
      */
     static final int CHUNK_SIZE = 8192;
 
diff --git a/lucene/core/src/java/org/apache/lucene/store/OutputStreamIndexOutput.java b/lucene/core/src/java/org/apache/lucene/store/OutputStreamIndexOutput.java
index 14b56a67e8a..195fb83dc74 100644
--- a/lucene/core/src/java/org/apache/lucene/store/OutputStreamIndexOutput.java
+++ b/lucene/core/src/java/org/apache/lucene/store/OutputStreamIndexOutput.java
@@ -149,5 +149,15 @@ public class OutputStreamIndexOutput extends IndexOutput {
         this.count = count + 1;
       }
     }
+
+    @Override
+    public void write(byte[] b, int off, int len) throws IOException {
+      if (len > buf.length - count) {
+        super.write(b, off, len);
+      } else {
+        System.arraycopy(b, off, buf, count, len);
+        count += len;
+      }
+    }
   }
 }