LUCENE-10605: fix error in 32bit jvm object alignment gap calculation (#949)

This commit is contained in:
Sun WuQiang 2022-06-10 09:07:37 +08:00 committed by GitHub
parent b1eddec821
commit 26f21ae36d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 7 deletions

View File

@ -99,6 +99,8 @@ Bug Fixes
* LUCENE-10563: Fix failure to tessellate complex polygon (Craig Taverner) * LUCENE-10563: Fix failure to tessellate complex polygon (Craig Taverner)
* LUCENE-10605: Fix error in 32bit jvm object alignment gap calculation (Sun Wuqiang)
Other Other
--------------------- ---------------------

View File

@ -191,17 +191,21 @@ public final class ArrayUtil {
return newSize; return newSize;
} }
} else { } 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) { switch (bytesPerElement) {
case 2:
// round up to multiple of 2
return (newSize + 1) & 0x7ffffffe;
case 1: case 1:
// round up to multiple of 4 // align with size of 4,12,20,28...
return (newSize + 3) & 0x7ffffffc; return ((newSize + 3) & 0x7ffffff8) + 4;
case 2:
// align with size of 6,10,14,18...
return ((newSize + 1) & 0x7ffffffc) + 2;
case 4: case 4:
// align with size of 5,7,9,11...
return (newSize & 0x7ffffffe) + 1;
case 8: case 8:
// no rounding // no processing required
default: default:
// odd (invalid?) size // odd (invalid?) size
return newSize; return newSize;