You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by pa...@apache.org on 2022/06/10 01:07:45 UTC

[lucene] branch main updated: LUCENE-10605: fix error in 32bit jvm object alignment gap calculation (#949)

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

patrickz 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 26f21ae36d0 LUCENE-10605: fix error in 32bit jvm object alignment gap calculation (#949)
26f21ae36d0 is described below

commit 26f21ae36d00c77e48a0390073912b837ee01ce1
Author: Sun WuQiang <su...@gmail.com>
AuthorDate: Fri Jun 10 09:07:37 2022 +0800

    LUCENE-10605: fix error in 32bit jvm object alignment gap calculation (#949)
---
 lucene/CHANGES.txt                                     |  2 ++
 .../src/java/org/apache/lucene/util/ArrayUtil.java     | 18 +++++++++++-------
 2 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 59969a90a90..a87e12e610e 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -99,6 +99,8 @@ Bug Fixes
 
 * LUCENE-10563: Fix failure to tessellate complex polygon (Craig Taverner)
 
+* LUCENE-10605: Fix error in 32bit jvm object alignment gap calculation (Sun Wuqiang)
+
 Other
 ---------------------
 
diff --git a/lucene/core/src/java/org/apache/lucene/util/ArrayUtil.java b/lucene/core/src/java/org/apache/lucene/util/ArrayUtil.java
index 635baa7547f..8d43cb958dc 100644
--- a/lucene/core/src/java/org/apache/lucene/util/ArrayUtil.java
+++ b/lucene/core/src/java/org/apache/lucene/util/ArrayUtil.java
@@ -191,17 +191,21 @@ public final class ArrayUtil {
           return newSize;
       }
     } else {
-      // round up to 4 byte alignment in 64bit env
+      // In 32bit jvm, it's still 8-byte aligned,
+      // but the array header is 12 bytes, not a multiple of 8.
+      // So saving 4,12,20,28... bytes of data is the most cost-effective.
       switch (bytesPerElement) {
-        case 2:
-          // round up to multiple of 2
-          return (newSize + 1) & 0x7ffffffe;
         case 1:
-          // round up to multiple of 4
-          return (newSize + 3) & 0x7ffffffc;
+          // align with size of 4,12,20,28...
+          return ((newSize + 3) & 0x7ffffff8) + 4;
+        case 2:
+          // align with size of 6,10,14,18...
+          return ((newSize + 1) & 0x7ffffffc) + 2;
         case 4:
+          // align with size of 5,7,9,11...
+          return (newSize & 0x7ffffffe) + 1;
         case 8:
-          // no rounding
+          // no processing required
         default:
           // odd (invalid?) size
           return newSize;