You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by "chishui (via GitHub)" <gi...@apache.org> on 2026/10/14 07:25:54 UTC

[PR] [PERFORMANCE ] Speed up stored fields decompression by decoding LZ4 from a heap buffer [lucene]

chishui opened a new pull request, #16836:
URL: https://github.com/apache/lucene/pull/16836

   ### Description
   The change targets reducing stored fields retrieval performance through optimizing LZ4 decompression performance. Instead of reading data bytes to bytes, it reads a batch data and store into a heap object then every read is from the heap object.
   
   ### Benchmark results
   ```
   Real data (stock codec, the same index read by a main jar and a branch jar in alternating JVMs, one pinned core; checksums identical):
   
   ┌───────────────────────────────┬─────────┬───────┬───────────────────────┬────────────┐
   │            Dataset            │ Avg doc │ Ratio │     Random access     │ Merge read │
   ├───────────────────────────────┼─────────┼───────┼───────────────────────┼────────────┤
   │ msmarco sparse vectors (JSON) │ 1.7 KB  │ 1.6×  │ 27.5 → 18.9 µs (−32%) │ +42%       │
   ├───────────────────────────────┼─────────┼───────┼───────────────────────┼────────────┤
   │ geonames                      │ 306 B   │ 4.0×  │ 10.8 → 8.4 µs (−22%)  │ +30%       │
   ├───────────────────────────────┼─────────┼───────┼───────────────────────┼────────────┤
   │ http_logs                     │ 133 B   │ 4.3×  │ 10.4 → 8.2 µs (−22%)  │ +28%       │
   ├───────────────────────────────┼─────────┼───────┼───────────────────────┼────────────┤
   │ noaa                          │ 287 B   │ 2.6×  │ 14.5 → 10.1 µs (−30%) │ +42%       │
   ├───────────────────────────────┼─────────┼───────┼───────────────────────┼────────────┤
   │ nyc_taxis                     │ 498 B   │ 4.3×  │ 9.3 → 6.9 µs (−26%)   │ +36%       │
   ├───────────────────────────────┼─────────┼───────┼───────────────────────┼────────────┤
   │ so (text + code)              │ 690 B   │ 1.6×  │ 20.4 → 14.7 µs (−28%) │ +40%       │
   ├───────────────────────────────┼─────────┼───────┼───────────────────────┼────────────┤
   │ pmc (full-text articles)      │ 22.8 KB │ 1.8×  │ 68.8 → 52.4 µs (−24%) │ +35%       │
   ├───────────────────────────────┼─────────┼───────┼───────────────────────┼────────────┤
   │ dense vector, 768-d JSON      │ 8.6 KB  │ 1.4×  │ 53.2 → 35.5 µs (−33%) │ +51%       │
   ├───────────────────────────────┼─────────┼───────┼───────────────────────┼────────────┤
   │ dense vector, 768-d binary    │ 3 KB    │ 1.0×  │ 3.70 → 3.62 µs (−2%)  │ +1%        │
   ├───────────────────────────────┼─────────┼───────┼───────────────────────┼────────────┤
   │ random bytes                  │ 1 KB    │ 1.0×  │ 2.76 → 2.78 µs (+1%)  │ 0%         │
   └───────────────────────────────┴─────────┴───────┴───────────────────────┴────────────┘
   
   JMH, main vs branch (random access = µs per 10-doc batch; merge = full sequential read):
   
   ┌────────────────────┬──────────────────────┬───────────────┐
   │      docType       │     randomAccess     │ mergeInstance │
   ├────────────────────┼──────────────────────┼───────────────┤
   │ json_logs          │ 110.6 → 78.4 (−29%)  │ −28%          │
   ├────────────────────┼──────────────────────┼───────────────┤
   │ text               │ 174.9 → 112.3 (−36%) │ −32%          │
   ├────────────────────┼──────────────────────┼───────────────┤
   │ sparse_vector_json │ 310.5 → 216.1 (−30%) │ −29%          │
   ├────────────────────┼──────────────────────┼───────────────┤
   │ dense_vector_json  │ 514.5 → 350.6 (−32%) │ −32%          │
   ├────────────────────┼──────────────────────┼───────────────┤
   │ random             │ 25.8 → 24.9 (−4%)    │ −3%           │
   └────────────────────┴──────────────────────┴───────────────┘
   ```
   
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] [PERFORMANCE ] Speed up stored fields decompression by decoding LZ4 from a heap buffer [lucene]

Posted by "github-actions[bot] (via GitHub) via issues" <is...@lucene.apache.org>.
github-actions[bot] commented on PR #16836:
URL: https://github.com/apache/lucene/pull/16836#issuecomment-6591999501

   This PR has not had activity in the past 2 weeks, labeling it as stale. If the PR is waiting for review, notify the dev@lucene.apache.org list. Thank you for your contribution!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] [PERFORMANCE ] Speed up stored fields decompression by decoding LZ4 from a heap buffer [lucene]

Posted by "chishui (via GitHub)" <gi...@apache.org>.
chishui commented on code in PR #16836:
URL: https://github.com/apache/lucene/pull/16836#discussion_r4310951528


##########
lucene/core/src/java/org/apache/lucene/util/compress/LZ4.java:
##########
@@ -147,6 +147,95 @@ public static int decompress(DataInput compressed, int decompressedLen, byte[] d
     return dOff;
   }
 
+  /**
+   * Same as {@link #decompress(DataInput, int, byte[], int)}, but reads the compressed bytes from
+   * {@code compressed[cOff:cOff+cLen]}. Reading from a heap array avoids the per-byte overhead of
+   * {@link DataInput#readByte()}, which dominates decompression when the input is an {@code
+   * IndexInput}.
+   */
+  public static int decompress(

Review Comment:
   I ran a benchmark and the performance of using `ByteArrayDataInput` is worse
   ```
   ┌────────────────────┬───────────────┬────────────────────┬─────────────────────────────────┐
   │      docType       │   benchmark   │ ByteArrayDataInput │ ByteArrayDataInput & IndexInput │
   ├────────────────────┼───────────────┼────────────────────┼─────────────────────────────────┤
   │ text               │ randomAccess  │ +6.8%              │ +15.6% (223.6 → 258.5 µs)       │
   ├────────────────────┼───────────────┼────────────────────┼─────────────────────────────────┤
   │ text               │ mergeInstance │ +1.2%              │ +14.0%                          │
   ├────────────────────┼───────────────┼────────────────────┼─────────────────────────────────┤
   │ sparse_vector_json │ randomAccess  │ +5.7%              │ +20.3% (334.2 → 402.1 µs)       │
   ├────────────────────┼───────────────┼────────────────────┼─────────────────────────────────┤
   │ sparse_vector_json │ mergeInstance │ +6.1%              │ +18.4%                          │
   └────────────────────┴───────────────┴────────────────────┴─────────────────────────────────┘
   note: LZ4.decompress(DataInput) also decodes blocktree terms and doc values from an IndexInput
   ```
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] [PERFORMANCE ] Speed up stored fields decompression by decoding LZ4 from a heap buffer [lucene]

Posted by "kkewwei (via GitHub)" <gi...@apache.org>.
kkewwei commented on PR #16836:
URL: https://github.com/apache/lucene/pull/16836#issuecomment-6167089537

   Same with #14397


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] [PERFORMANCE ] Speed up stored fields decompression by decoding LZ4 from a heap buffer [lucene]

Posted by "kkewwei (via GitHub)" <gi...@apache.org>.
kkewwei commented on code in PR #16836:
URL: https://github.com/apache/lucene/pull/16836#discussion_r4281998421


##########
lucene/core/src/java/org/apache/lucene/util/compress/LZ4.java:
##########
@@ -147,6 +147,95 @@ public static int decompress(DataInput compressed, int decompressedLen, byte[] d
     return dOff;
   }
 
+  /**
+   * Same as {@link #decompress(DataInput, int, byte[], int)}, but reads the compressed bytes from
+   * {@code compressed[cOff:cOff+cLen]}. Reading from a heap array avoids the per-byte overhead of
+   * {@link DataInput#readByte()}, which dominates decompression when the input is an {@code
+   * IndexInput}.
+   */
+  public static int decompress(

Review Comment:
   Could we wrap the `compressed` in `ByteArrayDataInput` and reuse the existing`DataInput`, which can reduce code duplication?



##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/LZ4WithPresetDictCompressionMode.java:
##########
@@ -144,6 +154,29 @@ public void decompress(DataInput in, int originalLength, int offset, int length,
       assert bytes.isValid();
     }
 
+    /**
+     * Decompresses {@code decompressedLength} bytes of a block that holds {@code blockLength}
+     * bytes. Compressible blocks are read into a byte[] first, since decoding from a heap array is
+     * much faster than reading tokens one by one from the {@link DataInput}. Incompressible blocks
+     * are mostly literals that are already copied in bulk, so they are decoded from the {@link
+     * DataInput} to avoid copying them twice.
+     */
+    private int decompress(
+        DataInput in,
+        int compressedLength,
+        int blockLength,
+        int decompressedLength,
+        byte[] dest,
+        int dOff)
+        throws IOException {
+      if (compressedLength > blockLength - (blockLength >>> 3)) {
+        return LZ4.decompress(in, decompressedLength, dest, dOff);
+      }
+      compressed = ArrayUtil.growNoCopy(compressed, compressedLength);

Review Comment:
   For cluster with many segments, we have observed that the`buffer` can sometimes retain a significant amount of memory. I’m concerned that adding another`compressed`  may further increase memory usage.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] [PERFORMANCE ] Speed up stored fields decompression by decoding LZ4 from a heap buffer [lucene]

Posted by "chishui (via GitHub)" <gi...@apache.org>.
chishui commented on code in PR #16836:
URL: https://github.com/apache/lucene/pull/16836#discussion_r4310924535


##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/LZ4WithPresetDictCompressionMode.java:
##########
@@ -144,6 +154,29 @@ public void decompress(DataInput in, int originalLength, int offset, int length,
       assert bytes.isValid();
     }
 
+    /**
+     * Decompresses {@code decompressedLength} bytes of a block that holds {@code blockLength}
+     * bytes. Compressible blocks are read into a byte[] first, since decoding from a heap array is
+     * much faster than reading tokens one by one from the {@link DataInput}. Incompressible blocks
+     * are mostly literals that are already copied in bulk, so they are decoded from the {@link
+     * DataInput} to avoid copying them twice.
+     */
+    private int decompress(
+        DataInput in,
+        int compressedLength,
+        int blockLength,
+        int decompressedLength,
+        byte[] dest,
+        int dOff)
+        throws IOException {
+      if (compressedLength > blockLength - (blockLength >>> 3)) {
+        return LZ4.decompress(in, decompressedLength, dest, dOff);
+      }
+      compressed = ArrayUtil.growNoCopy(compressed, compressedLength);

Review Comment:
   `compressed` adds around `15kb` memory which is roughly 8–15% increase for the class. It's held by storeFields() which is per segment. In situation like normal fetch, segment is handled one by one, so it shouldn't be a problem. But in situation like segments are processed in parallel, your concern is valid that the added memory could times segment count.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org