Skip to content

Apache Lucene open-source search software

Fix inconsistent null handling between array and single object version RAM estimation (#16296)

Tim Grein4856c11d1c00
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 125fd7cf6d8..d2943472576 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -178,6 +178,9 @@ Optimizations
Bug Fixes
---------------------
+* Fix missing null check in RamUsageEstimator.sizeOf(Accountable) to be consistent
+ with sizeOf(String) and sizeOf(Accountable[]). (Tim Grein)
+
* GITHUB#14049: Randomize KNN codec params in RandomCodec. Fixes scalar quantization div-by-zero
when all values are identical. (Mike Sokolov)
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 08b3cd1fb3e..9041581efa4 100644
--- a/lucene/core/src/java/org/apache/lucene/util/RamUsageEstimator.java
+++ b/lucene/core/src/java/org/apache/lucene/util/RamUsageEstimator.java
@@ -425,9 +425,12 @@ public final class RamUsageEstimator {
/**
* Returns the size in bytes of the {@link Accountable} object, using its {@link
- * Accountable#ramBytesUsed()} method.
+ * Accountable#ramBytesUsed()} method. Returns 0, if the {@link Accountable} object is null.
*/
public static long sizeOf(Accountable accountable) {
+ if (accountable == null) {
+ return 0;
+ }
return accountable.ramBytesUsed();
}
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 8e0bd13507e..5ffb7e9c3e0 100644
--- a/lucene/core/src/test/org/apache/lucene/util/TestRamUsageEstimator.java
+++ b/lucene/core/src/test/org/apache/lucene/util/TestRamUsageEstimator.java
@@ -150,6 +150,11 @@ public class TestRamUsageEstimator extends LuceneTestCase {
assertEquals((double) actual, (double) estimated, (double) actual * errorFactor);
}
+ public void testAccountable() {
+ assertEquals(0L, RamUsageEstimator.sizeOf((Accountable) null));
+ assertEquals(1L, RamUsageEstimator.sizeOf(new DummyAccountable()));
+ }
+
public void testCollection() {
List<Object> list = new ArrayList<>();
list.add(1234L);
@@ -252,4 +257,11 @@ public class TestRamUsageEstimator extends LuceneTestCase {
private static class HolderSubclass2 extends Holder {
// empty, only inherits all fields -> size should be identical to superclass
}
+
+ private static class DummyAccountable implements Accountable {
+ @Override
+ public long ramBytesUsed() {
+ return 1L;
+ }
+ }
}