Skip to content

Apache Lucene open-source search software

fix silly minor performance bug in CompiledAutomaton/Terms.intersect implementations

Michael McCandlessf49f2b2af6ff
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 c13ea9e4492..90a1654081b 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
@@ -78,7 +78,8 @@ public class CompiledAutomaton {
/**
* Shared common suffix accepted by the automaton. Only valid
* for {@link AUTOMATON_TYPE#NORMAL}, and only when the
- * automaton accepts an infinite language.
+ * automaton accepts an infinite language. This will be null
+ * if the common prefix is length 0.
*/
public final BytesRef commonSuffixRef;
@@ -202,7 +203,12 @@ public class CompiledAutomaton {
commonSuffixRef = null;
} else {
// NOTE: this is a very costly operation! We should test if it's really warranted in practice...
- commonSuffixRef = Operations.getCommonSuffixBytesRef(binary, maxDeterminizedStates);
+ BytesRef suffix = Operations.getCommonSuffixBytesRef(binary, maxDeterminizedStates);
+ if (suffix.length == 0) {
+ commonSuffixRef = null;
+ } else {
+ commonSuffixRef = suffix;
+ }
}
// This will determinize the binary automaton for us:
diff --git a/lucene/core/src/java/org/apache/lucene/util/automaton/Operations.java b/lucene/core/src/java/org/apache/lucene/util/automaton/Operations.java
index 95981158528..d57184606d2 100644
--- a/lucene/core/src/java/org/apache/lucene/util/automaton/Operations.java
+++ b/lucene/core/src/java/org/apache/lucene/util/automaton/Operations.java
@@ -1063,7 +1063,7 @@ final public class Operations {
* Returns the longest string that is a prefix of all accepted strings and
* visits each state at most once. The automaton must be deterministic.
*
- * @return common prefix
+ * @return common prefix, which can be an empty (length 0) String (never null)
*/
public static String getCommonPrefix(Automaton a) {
if (a.isDeterministic() == false) {
@@ -1097,7 +1097,7 @@ final public class Operations {
* Returns the longest BytesRef that is a prefix of all accepted strings and
* visits each state at most once. The automaton must be deterministic.
*
- * @return common prefix
+ * @return common prefix, which can be an empty (length 0) BytesRef (never null)
*/
public static BytesRef getCommonPrefixBytesRef(Automaton a) {
BytesRefBuilder builder = new BytesRefBuilder();
@@ -1159,7 +1159,7 @@ final public class Operations {
* @param maxDeterminizedStates maximum number of states determinizing the
* automaton can result in. Set higher to allow more complex queries and
* lower to prevent memory exhaustion.
- * @return common suffix
+ * @return common suffix, which can be an empty (length 0) BytesRef (never null)
*/
public static BytesRef getCommonSuffixBytesRef(Automaton a, int maxDeterminizedStates) {
// reverse the language of the automaton, then reverse its common prefix.