Skip to content

Commit ef20aa5

Browse files
authored
Avoid double-counting Automaton in CompiledAutomaton.ramBytesUsed (#16046)
1 parent 2fc795f commit ef20aa5

3 files changed

Lines changed: 16 additions & 1 deletion

File tree

lucene/CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,8 @@ Bug Fixes
498498

499499
* GITHUB#15878: Fix LargeNumHitsTopDocsCollector not handling requested hit count correctly (Binlong Gao)
500500

501+
* GITHUB#16046: Fix double-counting of underlying Automaton in CompiledAutomaton#ramBytesUsed. (Eugene Rizhkov)
502+
501503
Other
502504
---------------------
503505
* 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)

lucene/core/src/java/org/apache/lucene/util/automaton/CompiledAutomaton.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,8 @@ public boolean equals(Object obj) {
535535

536536
@Override
537537
public long ramBytesUsed() {
538+
// this.automaton is accounted via runAutomaton.ramBytesUsed()
538539
return BASE_RAM_BYTES
539-
+ RamUsageEstimator.sizeOfObject(automaton)
540540
+ RamUsageEstimator.sizeOfObject(commonSuffixRef)
541541
+ RamUsageEstimator.sizeOfObject(runAutomaton)
542542
+ RamUsageEstimator.sizeOfObject(nfaRunAutomaton)

lucene/core/src/test/org/apache/lucene/util/automaton/TestCompiledAutomaton.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.lucene.search.QueryVisitor;
2929
import org.apache.lucene.search.TermQuery;
3030
import org.apache.lucene.tests.util.LuceneTestCase;
31+
import org.apache.lucene.tests.util.RamUsageTester;
3132
import org.apache.lucene.tests.util.TestUtil;
3233
import org.apache.lucene.util.BytesRef;
3334
import org.apache.lucene.util.BytesRefBuilder;
@@ -256,6 +257,18 @@ public void consumeTermsMatching(
256257
assertTrue(matchingCalled[0]);
257258
}
258259

260+
public void testRamBytesUsed() {
261+
Automaton a =
262+
Operations.determinize(new RegExp(".*foo.*bar.*baz.*").toAutomaton(), Integer.MAX_VALUE);
263+
CompiledAutomaton ca = new CompiledAutomaton(a, false, true, false);
264+
assertEquals(CompiledAutomaton.AUTOMATON_TYPE.NORMAL, ca.type);
265+
assertNotNull(ca.runAutomaton);
266+
267+
long reported = ca.ramBytesUsed();
268+
long actual = RamUsageTester.ramUsed(ca);
269+
assertEquals((double) actual, (double) reported, (double) actual * 0.10);
270+
}
271+
259272
private static Automaton makeSimpleNfa(char ch) {
260273
return makeDeterministicNormalAutomaton(ch, ch);
261274
}

0 commit comments

Comments
 (0)