You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by md...@apache.org on 2022/08/25 20:33:25 UTC

[lucene] branch branch_9x updated: Add Integer awareness to RamUsageEstimator.sizeOf (#11715)

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

mdrob pushed a commit to branch branch_9x
in repository https://gitbox.apache.org/repos/asf/lucene.git


The following commit(s) were added to refs/heads/branch_9x by this push:
     new 090b05033e7 Add Integer awareness to RamUsageEstimator.sizeOf (#11715)
090b05033e7 is described below

commit 090b05033e742c4db779dc3e0def83e8425b7ce3
Author: Mike Drob <md...@apache.org>
AuthorDate: Thu Aug 25 15:18:08 2022 -0500

    Add Integer awareness to RamUsageEstimator.sizeOf (#11715)
    
    Additionally, update comments to reflect that we have not been VM cache-aware for a long time now.
    
    (cherry picked from commit dbc7a9764a84fee989e8cb0faff365af62b131f8)
---
 lucene/CHANGES.txt                                   |  2 ++
 .../org/apache/lucene/util/RamUsageEstimator.java    | 20 +++++++++++++++-----
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 55f295a4269..360265e4600 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -22,6 +22,8 @@ Improvements
 * LUCENE-10216: Use MergePolicy to define and MergeScheduler to trigger the reader merges
   required by addIndexes(CodecReader[]) API. (Vigya Sharma, Michael McCandless)
 
+* GITHUB#11715: Add Integer awareness to RamUsageEstimator.sizeOf (Mike Drob)
+
 Optimizations
 ---------------------
 
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 c7f225b95ef..ab19765cbdb 100644
--- a/lucene/core/src/java/org/apache/lucene/util/RamUsageEstimator.java
+++ b/lucene/core/src/java/org/apache/lucene/util/RamUsageEstimator.java
@@ -107,8 +107,7 @@ public final class RamUsageEstimator {
     primitiveSizes = Collections.unmodifiableMap(primitiveSizesMap);
   }
 
-  /** JVMs typically cache small longs. This tries to find out what the range is. */
-  static final int LONG_SIZE, STRING_SIZE;
+  static final int INTEGER_SIZE, LONG_SIZE, STRING_SIZE;
 
   /** For testing only */
   static final boolean JVM_IS_HOTSPOT_64BIT;
@@ -187,6 +186,7 @@ public final class RamUsageEstimator {
       NUM_BYTES_ARRAY_HEADER = NUM_BYTES_OBJECT_HEADER + Integer.BYTES;
     }
 
+    INTEGER_SIZE = (int) shallowSizeOfInstance(Integer.class);
     LONG_SIZE = (int) shallowSizeOfInstance(Long.class);
     STRING_SIZE = (int) shallowSizeOfInstance(String.class);
   }
@@ -208,10 +208,18 @@ public final class RamUsageEstimator {
   }
 
   /**
-   * Return the size of the provided {@link Long} object, returning 0 if it is cached by the JVM and
-   * its shallow size otherwise.
+   * Return the shallow size of the provided {@link Integer} object. Ignores the possibility that
+   * this object is part of the VM IntegerCache
    */
-  public static long sizeOf(Long value) {
+  public static long sizeOf(Integer ignored) {
+    return INTEGER_SIZE;
+  }
+
+  /**
+   * Return the shallow size of the provided {@link Long} object. Ignores the possibility that this
+   * object is part of the VM LongCache
+   */
+  public static long sizeOf(Long ignored) {
     return LONG_SIZE;
   }
 
@@ -456,6 +464,8 @@ public final class RamUsageEstimator {
       size = sizeOf((float[]) o);
     } else if (o instanceof int[]) {
       size = sizeOf((int[]) o);
+    } else if (o instanceof Integer) {
+      size = sizeOf((Integer) o);
     } else if (o instanceof Long) {
       size = sizeOf((Long) o);
     } else if (o instanceof long[]) {