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

(lucene) branch main updated: Use shallow size for all boxed primitives in RamUsageEstimator.sizeOfObject (#16743)

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

javanna 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 c177939f5eb Use shallow size for all boxed primitives in RamUsageEstimator.sizeOfObject (#16743)
c177939f5eb is described below

commit c177939f5eb0cfb079b9d416654be4f051cc55ae
Author: Eugene Rizhkov <ye...@elastic.co>
AuthorDate: Fri Oct 9 12:57:02 2026 +0300

    Use shallow size for all boxed primitives in RamUsageEstimator.sizeOfObject (#16743)
    
    RamUsageEstimator.sizeOfObject returns the shallow size for Integer and Long, but Boolean, Byte, Character, Short, Float and Double fall through to UNKNOWN_DEFAULT_RAM_BYTES_USED (256 bytes). These classes hold a single primitive field, so their shallow size is their whole retained size (16 or 24 bytes). A collection of boxed doubles is over-counted by about 10x as a result.
---
 lucene/CHANGES.txt                                 |  3 +++
 .../org/apache/lucene/util/RamUsageEstimator.java  | 26 +++++++++++++++++++---
 .../apache/lucene/util/TestRamUsageEstimator.java  |  7 ++++++
 3 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index d122c580bc6..fd562625fe5 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -458,6 +458,9 @@ New Features
 
 Improvements
 ---------------------
+* GITHUB#16743: RamUsageEstimator#sizeOfObject now estimates boxed Boolean, Byte, Character, Short,
+  Float and Double at their shallow size instead of the default size. (Eugene Rizhkov)
+
 * GITHUB#16718: Modernize ThaiAnalyzer default stopwords list based on contemporary Thai corpus
   frequency analysis and linguistically validated stop words. (Kamthorn Krairaksa)
 
diff --git a/lucene/core/src/java/org/apache/lucene/util/RamUsageEstimator.java b/lucene/core/src/java/org/apache/lucene/util/RamUsageEstimator.java
index 9041581efa4..1533d332f47 100644
--- a/lucene/core/src/java/org/apache/lucene/util/RamUsageEstimator.java
+++ b/lucene/core/src/java/org/apache/lucene/util/RamUsageEstimator.java
@@ -102,7 +102,15 @@ public final class RamUsageEstimator {
     primitiveSizes = Collections.unmodifiableMap(primitiveSizesMap);
   }
 
-  static final int INTEGER_SIZE, LONG_SIZE, STRING_SIZE;
+  static final int BOOLEAN_SIZE,
+      BYTE_SIZE,
+      CHARACTER_SIZE,
+      SHORT_SIZE,
+      INTEGER_SIZE,
+      FLOAT_SIZE,
+      LONG_SIZE,
+      DOUBLE_SIZE,
+      STRING_SIZE;
 
   /** For testing only */
   static final boolean JVM_IS_HOTSPOT_64BIT;
@@ -133,8 +141,14 @@ public final class RamUsageEstimator {
       NUM_BYTES_ARRAY_HEADER = NUM_BYTES_OBJECT_HEADER + Integer.BYTES;
     }
 
+    BOOLEAN_SIZE = (int) shallowSizeOfInstance(Boolean.class);
+    BYTE_SIZE = (int) shallowSizeOfInstance(Byte.class);
+    CHARACTER_SIZE = (int) shallowSizeOfInstance(Character.class);
+    SHORT_SIZE = (int) shallowSizeOfInstance(Short.class);
     INTEGER_SIZE = (int) shallowSizeOfInstance(Integer.class);
+    FLOAT_SIZE = (int) shallowSizeOfInstance(Float.class);
     LONG_SIZE = (int) shallowSizeOfInstance(Long.class);
+    DOUBLE_SIZE = (int) shallowSizeOfInstance(Double.class);
     STRING_SIZE = (int) shallowSizeOfInstance(String.class);
   }
 
@@ -399,15 +413,21 @@ public final class RamUsageEstimator {
       case Accountable a -> size = a.ramBytesUsed();
       case String s -> size = sizeOf(s);
       case boolean[] ba -> size = sizeOf(ba);
+      case Boolean _ -> size = BOOLEAN_SIZE;
       case byte[] ba -> size = sizeOf(ba);
+      case Byte _ -> size = BYTE_SIZE;
       case char[] ca -> size = sizeOf(ca);
+      case Character _ -> size = CHARACTER_SIZE;
       case double[] da -> size = sizeOf(da);
+      case Double _ -> size = DOUBLE_SIZE;
       case float[] fa -> size = sizeOf(fa);
+      case Float _ -> size = FLOAT_SIZE;
       case int[] ia -> size = sizeOf(ia);
-      case Integer i -> size = sizeOf(i);
-      case Long l -> size = sizeOf(l);
+      case Integer _ -> size = INTEGER_SIZE;
+      case Long _ -> size = LONG_SIZE;
       case long[] la -> size = sizeOf(la);
       case short[] sa -> size = sizeOf(sa);
+      case Short _ -> size = SHORT_SIZE;
       case String[] sa -> size = sizeOf(sa);
       case Query q -> size = sizeOf(q, defSize);
       case Map<?, ?> m -> size = sizeOfMap(m, ++depth, defSize);
diff --git a/lucene/core/src/test/org/apache/lucene/util/TestRamUsageEstimator.java b/lucene/core/src/test/org/apache/lucene/util/TestRamUsageEstimator.java
index 5ffb7e9c3e0..03aba775c09 100644
--- a/lucene/core/src/test/org/apache/lucene/util/TestRamUsageEstimator.java
+++ b/lucene/core/src/test/org/apache/lucene/util/TestRamUsageEstimator.java
@@ -120,6 +120,13 @@ public class TestRamUsageEstimator extends LuceneTestCase {
     assertEquals(actual, estimated);
   }
 
+  public void testBoxedPrimitives() {
+    for (Object value : new Object[] {true, (byte) 1, 'c', (short) 1, 1, 1.0f, 1L, 1.0d}) {
+      assertEquals(
+          value.getClass().getName(), ramUsed(value), RamUsageEstimator.sizeOfObject(value));
+    }
+  }
+
   public void testBytesRefHash() {
     BytesRefHash bytes = new BytesRefHash();
     for (int i = 0; i < 100; i++) {