lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hoss...@apache.org
Subject svn commit: r1685687 - in /lucene/dev/trunk/lucene: CHANGES.txt queries/src/java/org/apache/lucene/queries/function/valuesource/MultiFunction.java queries/src/test/org/apache/lucene/queries/function/TestValueSources.java
Date Mon, 15 Jun 2015 23:26:15 GMT
Author: hossman
Date: Mon Jun 15 23:26:14 2015
New Revision: 1685687

URL: http://svn.apache.org/r1685687
Log:
LUCENE-6569: Optimize MultiFunction.anyExists and allExists to eliminate excessive array creation
in common 2 argument usage

Modified:
    lucene/dev/trunk/lucene/CHANGES.txt
    lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/MultiFunction.java
    lucene/dev/trunk/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java

Modified: lucene/dev/trunk/lucene/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/CHANGES.txt?rev=1685687&r1=1685686&r2=1685687&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/CHANGES.txt (original)
+++ lucene/dev/trunk/lucene/CHANGES.txt Mon Jun 15 23:26:14 2015
@@ -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

Modified: lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/MultiFunction.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/MultiFunction.java?rev=1685687&r1=1685686&r2=1685687&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/MultiFunction.java
(original)
+++ lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/MultiFunction.java
Mon Jun 15 23:26:14 2015
@@ -44,12 +44,12 @@ public abstract class MultiFunction exte
   }
 
   /**
-   * 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 exte
     }
     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 exte
     }
     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('(');

Modified: lucene/dev/trunk/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java?rev=1685687&r1=1685686&r2=1685687&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java
(original)
+++ lucene/dev/trunk/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java
Mon Jun 15 23:26:14 2015
@@ -58,6 +58,7 @@ import org.apache.lucene.queries.functio
 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 Lu
     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>.



Mime
View raw message