Skip to content

Apache Lucene open-source search software

LUCENE-6569: Optimize MultiFunction.anyExists and allExists to eliminate excessive array creation in common 2 argument usage

Chris M. Hostetterb2394ff752f3
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index c15f4d3bd2a..36a35142263 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -158,6 +158,10 @@ Changes in Runtime Behavior
* LUCENE-6538: Also include java.vm.version and java.runtime.version
in per-segment diagnostics (Robert Muir, Mike McCandless)
+* LUCENE-6569: Optimize MultiFunction.anyExists and allExists to eliminate
+ excessive array creation in common 2 argument usage (Jacob Graves, hossman)
+
+
Build
* LUCENE-6518: Don't report false thread leaks from IBM J9
diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/MultiFunction.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/MultiFunction.java
index 053031d173f..34d792e4e7b 100644
--- a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/MultiFunction.java
+++ b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/MultiFunction.java
@@ -44,12 +44,12 @@ public abstract class MultiFunction extends ValueSource {
}
/**
- * Helper utility for {@link FunctionValues} wrapping multiple {@link FunctionValues}
+ * Helper utility for {@link FunctionValues}
*
- * @return true if <em>all</em> of the specified <code>values</code>
+ * @return true if <em>all</em> of the specified <code>values</code>
* {@link FunctionValues#exists} for the specified doc, else false.
*/
- public static boolean allExists(int doc, FunctionValues... values) {
+ public static boolean allExists(int doc, FunctionValues[] values) {
for (FunctionValues v : values) {
if ( ! v.exists(doc) ) {
return false;
@@ -57,13 +57,14 @@ public abstract class MultiFunction extends ValueSource {
}
return true;
}
+
/**
- * Helper utility for {@link FunctionValues} wrapping multiple {@link FunctionValues}
+ * Helper utility for {@link FunctionValues}
*
- * @return true if <em>any</em> of the specified <code>values</code>
+ * @return true if <em>any</em> of the specified <code>values</code>
* {@link FunctionValues#exists} for the specified doc, else false.
*/
- public static boolean anyExists(int doc, FunctionValues... values) {
+ public static boolean anyExists(int doc, FunctionValues[] values) {
for (FunctionValues v : values) {
if ( v.exists(doc) ) {
return true;
@@ -71,7 +72,31 @@ public abstract class MultiFunction extends ValueSource {
}
return false;
}
-
+
+ /**
+ * Equivilent the the {@code FunctionValues[]} method with the same name, but optimized for
+ * dealing with exactly 2 arguments.
+ *
+ * @return true if <em>both</em> of the specified <code>values</code>
+ * {@link FunctionValues#exists} for the specified doc, else false.
+ * @see #anyExists(int,FunctionValues[])
+ */
+ public static boolean allExists(int doc, FunctionValues values1, FunctionValues values2) {
+ return values1.exists(doc) && values2.exists(doc);
+ }
+
+ /**
+ * Equivilent the the {@code FunctionValues[]} method with the same name, but optimized for
+ * dealing with exactly 2 arguments.
+ *
+ * @return true if <em>either</em> of the specified <code>values</code>
+ * {@link FunctionValues#exists} for the specified doc, else false.
+ * @see #anyExists(int,FunctionValues[])
+ */
+ public static boolean anyExists(int doc, FunctionValues values1, FunctionValues values2) {
+ return values1.exists(doc) || values2.exists(doc);
+ }
+
public static String description(String name, List<ValueSource> sources) {
StringBuilder sb = new StringBuilder();
sb.append(name).append('(');
diff --git a/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java b/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java
index c4e77e0268c..f0fcf19be42 100644
--- a/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java
+++ b/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java
@@ -58,6 +58,7 @@ import org.apache.lucene.queries.function.valuesource.MaxDocValueSource;
import org.apache.lucene.queries.function.valuesource.MaxFloatFunction;
import org.apache.lucene.queries.function.valuesource.MinFloatFunction;
import org.apache.lucene.queries.function.valuesource.MultiFloatFunction;
+import org.apache.lucene.queries.function.valuesource.MultiFunction;
import org.apache.lucene.queries.function.valuesource.NormValueSource;
import org.apache.lucene.queries.function.valuesource.NumDocsValueSource;
import org.apache.lucene.queries.function.valuesource.PowFloatFunction;
@@ -527,7 +528,57 @@ public class TestValueSources extends LuceneTestCase {
assertHits(new FunctionQuery(vs), new float[] { 4f, 4f });
assertAllExist(vs);
}
-
+
+ public void testMultiFunctionHelperEquivilence() throws IOException {
+ // the 2 arg versions of these methods should return the exact same results as
+ // the multi arg versions with a 2 element array
+
+ // actual doc / index is not relevant for this test
+ final LeafReaderContext leaf = searcher.getIndexReader().leaves().get(0);
+ final Map context = ValueSource.newContext(searcher);
+
+ ALL_EXIST_VS.createWeight(context, searcher);
+ NONE_EXIST_VS.createWeight(context, searcher);
+
+ final FunctionValues ALL = ALL_EXIST_VS.getValues(context, leaf);
+ final FunctionValues NONE = NONE_EXIST_VS.getValues(context, leaf);
+
+ // quick sanity checks of explicit results
+ assertTrue(MultiFunction.allExists(1, ALL, ALL));
+ assertTrue(MultiFunction.allExists(1, new FunctionValues[] {ALL, ALL}));
+ assertTrue(MultiFunction.anyExists(1, ALL, NONE));
+ assertTrue(MultiFunction.anyExists(1, new FunctionValues[] {ALL, NONE}));
+ //
+ assertFalse(MultiFunction.allExists(1, ALL, NONE));
+ assertFalse(MultiFunction.allExists(1, new FunctionValues[] {ALL, NONE}));
+ assertFalse(MultiFunction.anyExists(1, NONE, NONE));
+ assertFalse(MultiFunction.anyExists(1, new FunctionValues[] {NONE, NONE}));
+
+
+
+ // iterate all permutations and verify equivilence
+ for (FunctionValues firstArg : new FunctionValues[] {ALL, NONE}) {
+ for (FunctionValues secondArg : new FunctionValues[] {ALL, NONE}) {
+ assertEquals("allExists("+firstArg+","+secondArg+")",
+ MultiFunction.allExists(1, firstArg,secondArg),
+ MultiFunction.allExists(1, new FunctionValues[] { firstArg,secondArg}));
+ assertEquals("anyExists("+firstArg+","+secondArg+")",
+ MultiFunction.anyExists(1, firstArg,secondArg),
+ MultiFunction.anyExists(1, new FunctionValues[] { firstArg,secondArg}));
+
+ // future proof against posibility of someone "optimizing" the array method
+ // if .length==2 ... redundent third arg should give same results as well...
+ assertEquals("allExists("+firstArg+","+secondArg+","+secondArg+")",
+ MultiFunction.allExists(1, firstArg,secondArg),
+ MultiFunction.allExists(1, new FunctionValues[] { firstArg,secondArg,secondArg}));
+ assertEquals("anyExists("+firstArg+","+secondArg+","+secondArg+")",
+ MultiFunction.anyExists(1, firstArg,secondArg),
+ MultiFunction.anyExists(1, new FunctionValues[] { firstArg,secondArg,secondArg}));
+
+ }
+ }
+ }
+
/**
* Asserts that for every doc, the {@link FunctionValues#exists} value
* from the {@link ValueSource} is <b>true</b>.