Skip to content

Apache Lucene open-source search software

Avoid double-counting Automaton in CompiledAutomaton.ramBytesUsed (#16046)

Eugene Rizhkovef20aa518bf9
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index f2340a43df5..f71fa746827 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -498,6 +498,8 @@ Bug Fixes
* GITHUB#15878: Fix LargeNumHitsTopDocsCollector not handling requested hit count correctly (Binlong Gao)
+* GITHUB#16046: Fix double-counting of underlying Automaton in CompiledAutomaton#ramBytesUsed. (Eugene Rizhkov)
+
Other
---------------------
* GITHUB#15586: Document that scoring and ranking may change across major Lucene versions, and that applications requiring stable ranking should explicitly configure Similarity. (Parveen Saini)
diff --git a/lucene/core/src/java/org/apache/lucene/util/automaton/CompiledAutomaton.java b/lucene/core/src/java/org/apache/lucene/util/automaton/CompiledAutomaton.java
index f414b90b3e4..5ded854f129 100644
--- a/lucene/core/src/java/org/apache/lucene/util/automaton/CompiledAutomaton.java
+++ b/lucene/core/src/java/org/apache/lucene/util/automaton/CompiledAutomaton.java
@@ -535,8 +535,8 @@ public class CompiledAutomaton implements Accountable {
@Override
public long ramBytesUsed() {
+ // this.automaton is accounted via runAutomaton.ramBytesUsed()
return BASE_RAM_BYTES
- + RamUsageEstimator.sizeOfObject(automaton)
+ RamUsageEstimator.sizeOfObject(commonSuffixRef)
+ RamUsageEstimator.sizeOfObject(runAutomaton)
+ RamUsageEstimator.sizeOfObject(nfaRunAutomaton)
diff --git a/lucene/core/src/test/org/apache/lucene/util/automaton/TestCompiledAutomaton.java b/lucene/core/src/test/org/apache/lucene/util/automaton/TestCompiledAutomaton.java
index 2bf94194638..db5bb22e6f5 100644
--- a/lucene/core/src/test/org/apache/lucene/util/automaton/TestCompiledAutomaton.java
+++ b/lucene/core/src/test/org/apache/lucene/util/automaton/TestCompiledAutomaton.java
@@ -28,6 +28,7 @@ import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryVisitor;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.tests.util.LuceneTestCase;
+import org.apache.lucene.tests.util.RamUsageTester;
import org.apache.lucene.tests.util.TestUtil;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.BytesRefBuilder;
@@ -256,6 +257,18 @@ public class TestCompiledAutomaton extends LuceneTestCase {
assertTrue(matchingCalled[0]);
}
+ public void testRamBytesUsed() {
+ Automaton a =
+ Operations.determinize(new RegExp(".*foo.*bar.*baz.*").toAutomaton(), Integer.MAX_VALUE);
+ CompiledAutomaton ca = new CompiledAutomaton(a, false, true, false);
+ assertEquals(CompiledAutomaton.AUTOMATON_TYPE.NORMAL, ca.type);
+ assertNotNull(ca.runAutomaton);
+
+ long reported = ca.ramBytesUsed();
+ long actual = RamUsageTester.ramUsed(ca);
+ assertEquals((double) actual, (double) reported, (double) actual * 0.10);
+ }
+
private static Automaton makeSimpleNfa(char ch) {
return makeDeterministicNormalAutomaton(ch, ch);
}