You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by jp...@apache.org on 2021/10/20 08:30:17 UTC

[lucene] branch main updated: LUCENE-10187: Reduce DirectWriter's padding. (#398)

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

jpountz 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 f13a400  LUCENE-10187: Reduce DirectWriter's padding. (#398)
f13a400 is described below

commit f13a400b9a7ab02aadd685061a1cfa955513c17a
Author: Adrien Grand <jp...@gmail.com>
AuthorDate: Wed Oct 20 10:30:09 2021 +0200

    LUCENE-10187: Reduce DirectWriter's padding. (#398)
    
    It would make us more likely to detect out-of-bounds access in the future.
---
 .../org/apache/lucene/util/packed/DirectWriter.java | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/lucene/core/src/java/org/apache/lucene/util/packed/DirectWriter.java b/lucene/core/src/java/org/apache/lucene/util/packed/DirectWriter.java
index 414f710..9d846d3 100644
--- a/lucene/core/src/java/org/apache/lucene/util/packed/DirectWriter.java
+++ b/lucene/core/src/java/org/apache/lucene/util/packed/DirectWriter.java
@@ -149,8 +149,25 @@ public final class DirectWriter {
     }
     assert !finished;
     flush();
-    // pad for fast io: we actually only need this for certain BPV, but its just 3 bytes...
-    for (int i = 0; i < 3; i++) {
+    // add padding bytes for fast io
+    // for every number of bits per value, we want to be able to read the entire value in a single
+    // read e.g. for 20 bits per value, we want to be able to read values using ints so we need
+    // 32 - 20 = 12 bits of padding
+    int paddingBitsNeeded;
+    if (bitsPerValue > Integer.SIZE) {
+      paddingBitsNeeded = Long.SIZE - bitsPerValue;
+    } else if (bitsPerValue > Short.SIZE) {
+      paddingBitsNeeded = Integer.SIZE - bitsPerValue;
+    } else if (bitsPerValue > Byte.SIZE) {
+      paddingBitsNeeded = Short.SIZE - bitsPerValue;
+    } else {
+      paddingBitsNeeded = 0;
+    }
+    assert paddingBitsNeeded >= 0;
+    final int paddingBytesNeeded = (paddingBitsNeeded + Byte.SIZE - 1) / Byte.SIZE;
+    assert paddingBytesNeeded <= 3;
+
+    for (int i = 0; i < paddingBytesNeeded; i++) {
       output.writeByte((byte) 0);
     }
     finished = true;