You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by "gsmiller (via GitHub)" <gi...@apache.org> on 2023/09/15 14:48:28 UTC

[GitHub] [lucene] gsmiller opened a new pull request, #12560: Defer #advanceExact on expression dependencies until their values are needed

gsmiller opened a new pull request, #12560:
URL: https://github.com/apache/lucene/pull/12560

   ### Description
   
   This extends the idea in GH#11878 to avoid advancing dependencies that are  never referenced because of expression branching (i.e., ternary expressions). I think we should be able to be a little more optimal in terms of not unnecessarily advancing dependent double values.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


[GitHub] [lucene] msokolov commented on a diff in pull request #12560: Defer #advanceExact on expression dependencies until their values are needed

Posted by "msokolov (via GitHub)" <gi...@apache.org>.
msokolov commented on code in PR #12560:
URL: https://github.com/apache/lucene/pull/12560#discussion_r1327797753


##########
lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionFunctionValues.java:
##########
@@ -39,21 +39,21 @@ class ExpressionFunctionValues extends DoubleValues {
   }
 
   @Override
-  public boolean advanceExact(int doc) {
+  public boolean advanceExact(int doc) throws IOException {
     if (currentDoc == doc) {
       return true;
     }
+    for (DoubleValues v : functionValues) {
+      v.advanceExact(doc);

Review Comment:
   I'm still a bit confused - it seems like we're pushing things around, but I know you have some compelling-looking results so it must be OK? But I think it would be great if you could distill whatever knowledge you've garnered into a comment here. The missing link for me is -- what is ExpressionFunctionValues representing? Is that used for functions only (like `max()`) or generally for any expression? I guess if it's only for functions then it makes sense that we are now getting lazy advance for other expressions that might not need all their subs.  But then have we lost some laziness for functions? Does this work for short-circuited booleans to? Like `(1 == 1) || expensive()`?



##########
lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionValueSource.java:
##########
@@ -90,16 +90,24 @@ public DoubleValues getValues(LeafReaderContext readerContext, DoubleValues scor
   static DoubleValues zeroWhenUnpositioned(DoubleValues in) {
     return new DoubleValues() {
 
-      boolean positioned = false;
+      int currentDoc = -1;
+      double value;
+      boolean computed = false;
 
       @Override
       public double doubleValue() throws IOException {
-        return positioned ? in.doubleValue() : 0;
+        if (computed == false) {
+          value = in.advanceExact(currentDoc) ? in.doubleValue() : 0;
+          computed = true;
+        }
+        return value;
       }
 
       @Override
-      public boolean advanceExact(int doc) throws IOException {
-        return positioned = in.advanceExact(doc);
+      public boolean advanceExact(int doc) {
+        currentDoc = doc;

Review Comment:
   is there any sense in checking if we are advancing to the same doc?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


[GitHub] [lucene] gsmiller merged pull request #12560: Defer #advanceExact on expression dependencies until their values are needed

Posted by "gsmiller (via GitHub)" <gi...@apache.org>.
gsmiller merged PR #12560:
URL: https://github.com/apache/lucene/pull/12560


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


[GitHub] [lucene] gsmiller commented on a diff in pull request #12560: Defer #advanceExact on expression dependencies until their values are needed

Posted by "gsmiller (via GitHub)" <gi...@apache.org>.
gsmiller commented on code in PR #12560:
URL: https://github.com/apache/lucene/pull/12560#discussion_r1327624072


##########
lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionFunctionValues.java:
##########
@@ -39,21 +39,21 @@ class ExpressionFunctionValues extends DoubleValues {
   }
 
   @Override
-  public boolean advanceExact(int doc) {
+  public boolean advanceExact(int doc) throws IOException {
     if (currentDoc == doc) {
       return true;
     }
+    for (DoubleValues v : functionValues) {
+      v.advanceExact(doc);

Review Comment:
   In this layer, yes. But all these DoubleValue instances come from `ExpressionValueSource#zeroWhenUnpositioned` (see the setup done in `ExpressionValueSource#getValues`). In this PR, I'm proposing pushing down the lazy advancing into that layer.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


[GitHub] [lucene] zhaih commented on a diff in pull request #12560: Defer #advanceExact on expression dependencies until their values are needed

Posted by "zhaih (via GitHub)" <gi...@apache.org>.
zhaih commented on code in PR #12560:
URL: https://github.com/apache/lucene/pull/12560#discussion_r1327778019


##########
lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionFunctionValues.java:
##########
@@ -39,21 +39,21 @@ class ExpressionFunctionValues extends DoubleValues {
   }
 
   @Override
-  public boolean advanceExact(int doc) {
+  public boolean advanceExact(int doc) throws IOException {
     if (currentDoc == doc) {
       return true;
     }
+    for (DoubleValues v : functionValues) {
+      v.advanceExact(doc);

Review Comment:
   I see, thank you for explaining, makes sense to me!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


[GitHub] [lucene] gsmiller commented on a diff in pull request #12560: Defer #advanceExact on expression dependencies until their values are needed

Posted by "gsmiller (via GitHub)" <gi...@apache.org>.
gsmiller commented on code in PR #12560:
URL: https://github.com/apache/lucene/pull/12560#discussion_r1327828413


##########
lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionFunctionValues.java:
##########
@@ -39,21 +39,21 @@ class ExpressionFunctionValues extends DoubleValues {
   }
 
   @Override
-  public boolean advanceExact(int doc) {
+  public boolean advanceExact(int doc) throws IOException {
     if (currentDoc == doc) {
       return true;
     }
+    for (DoubleValues v : functionValues) {
+      v.advanceExact(doc);

Review Comment:
   There's a bit of indirection here, so maybe this will help:
   1. `ExpressionFunctionValues` represents a computed expression value for a given doc. This comes from any instance of `Expression` (e.g., from `JavascriptCompiler#compile`). `Expression#getDoubleValuesSource` provides an instance of `ExpressionValueSource`, which in turn provides `ExpressionFunctionValues` when you call `#getValues` for a specific segment context. `ExpressionFunctionValues#advanceExact` always returns true (expressions produce results for all docs), and `ExpressionFunctionValues#doubleValue` computes the value for the doc.
   2. In `ExpressionValueSource#getValues`, we take all the expression's arguments (which are all instances of `DoubleValues`) and wrap them with an anonymous class provided by `#zeroWhenUnpositioned`.  The main purpose of this wrapper is to provide a value of `0` for any expression argument that isn't available for a doc (i.e., `advanceExact` returned false for the doc). This PR extends the functionality of this wrapper to lazily advance until the point when `doubleValues` is called.
   3. So without this PR, `ExpressionFunctionValues#advanceExact` is lazy but once `ExpressionFunctionValues#doubleValues` is called, it advances all of the arguments (regardless of whether-or-not that argument is ever needed for a given doc). With this change, the `advanceExact` also becomes lazy, so it gets deferred until a value is actually needed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


[GitHub] [lucene] zhaih commented on a diff in pull request #12560: Defer #advanceExact on expression dependencies until their values are needed

Posted by "zhaih (via GitHub)" <gi...@apache.org>.
zhaih commented on code in PR #12560:
URL: https://github.com/apache/lucene/pull/12560#discussion_r1327707056


##########
lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionFunctionValues.java:
##########
@@ -39,21 +39,21 @@ class ExpressionFunctionValues extends DoubleValues {
   }
 
   @Override
-  public boolean advanceExact(int doc) {
+  public boolean advanceExact(int doc) throws IOException {
     if (currentDoc == doc) {
       return true;
     }
+    for (DoubleValues v : functionValues) {
+      v.advanceExact(doc);

Review Comment:
   I see, but still a little confused about why this is supposed to be better?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


[GitHub] [lucene] gsmiller commented on a diff in pull request #12560: Defer #advanceExact on expression dependencies until their values are needed

Posted by "gsmiller (via GitHub)" <gi...@apache.org>.
gsmiller commented on code in PR #12560:
URL: https://github.com/apache/lucene/pull/12560#discussion_r1327828846


##########
lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionFunctionValues.java:
##########
@@ -39,21 +39,21 @@ class ExpressionFunctionValues extends DoubleValues {
   }
 
   @Override
-  public boolean advanceExact(int doc) {
+  public boolean advanceExact(int doc) throws IOException {
     if (currentDoc == doc) {
       return true;
     }
+    for (DoubleValues v : functionValues) {
+      v.advanceExact(doc);

Review Comment:
   UPDATE: I brought back the lazy `ExpressionFunctionValues#advanceExact` logic because it created confusion and isn't necessary to remove. It doesn't hurt to keep this lazy logic in place. I had removed it (making it greedy again) as an attempt to simplify, but since it created confusion, I figured I'd bring it back.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


[GitHub] [lucene] gsmiller commented on a diff in pull request #12560: Defer #advanceExact on expression dependencies until their values are needed

Posted by "gsmiller (via GitHub)" <gi...@apache.org>.
gsmiller commented on code in PR #12560:
URL: https://github.com/apache/lucene/pull/12560#discussion_r1327829161


##########
lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionFunctionValues.java:
##########
@@ -39,21 +39,21 @@ class ExpressionFunctionValues extends DoubleValues {
   }
 
   @Override
-  public boolean advanceExact(int doc) {
+  public boolean advanceExact(int doc) throws IOException {
     if (currentDoc == doc) {
       return true;
     }
+    for (DoubleValues v : functionValues) {
+      v.advanceExact(doc);

Review Comment:
   Oh, I also added another test-case to show that this laziness also works for condition short-circuiting (like you asked about @msokolov). And just to clarify, this is for _all_ expressions.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


[GitHub] [lucene] zhaih commented on a diff in pull request #12560: Defer #advanceExact on expression dependencies until their values are needed

Posted by "zhaih (via GitHub)" <gi...@apache.org>.
zhaih commented on code in PR #12560:
URL: https://github.com/apache/lucene/pull/12560#discussion_r1327603749


##########
lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionFunctionValues.java:
##########
@@ -39,21 +39,21 @@ class ExpressionFunctionValues extends DoubleValues {
   }
 
   @Override
-  public boolean advanceExact(int doc) {
+  public boolean advanceExact(int doc) throws IOException {
     if (currentDoc == doc) {
       return true;
     }
+    for (DoubleValues v : functionValues) {
+      v.advanceExact(doc);

Review Comment:
   I'm confused... Isn't this calling `advanceExact` more eagerly?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


[GitHub] [lucene] gsmiller commented on pull request #12560: Defer #advanceExact on expression dependencies until their values are needed

Posted by "gsmiller (via GitHub)" <gi...@apache.org>.
gsmiller commented on PR #12560:
URL: https://github.com/apache/lucene/pull/12560#issuecomment-1722012958

   Thanks @zhaih / @msokolov ! Merging.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


[GitHub] [lucene] gsmiller commented on a diff in pull request #12560: Defer #advanceExact on expression dependencies until their values are needed

Posted by "gsmiller (via GitHub)" <gi...@apache.org>.
gsmiller commented on code in PR #12560:
URL: https://github.com/apache/lucene/pull/12560#discussion_r1327725540


##########
lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionFunctionValues.java:
##########
@@ -39,21 +39,21 @@ class ExpressionFunctionValues extends DoubleValues {
   }
 
   @Override
-  public boolean advanceExact(int doc) {
+  public boolean advanceExact(int doc) throws IOException {
     if (currentDoc == doc) {
       return true;
     }
+    for (DoubleValues v : functionValues) {
+      v.advanceExact(doc);

Review Comment:
   If you have an expression like `a == 0 ? b : c` where `a`, `b` and `c` are all other references in your bindings, only `b` or `c` need to be evaluated for any given doc, not both. But today, we greedily call `advanceExact` on all three up-front, for every doc. So we're doing wasted advancing work for one of `b` or `c` for every single doc. I added a similar example in a test case.
   
   Depending on what the advancing is doing, this could come with significant cost (e.g., if `b` or `c` is another complex expression tree that ends up propagating the advance down all of its dependencies)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


[GitHub] [lucene] gsmiller commented on pull request #12560: Defer #advanceExact on expression dependencies until their values are needed

Posted by "gsmiller (via GitHub)" <gi...@apache.org>.
gsmiller commented on PR #12560:
URL: https://github.com/apache/lucene/pull/12560#issuecomment-1731814078

   Circling back on this: For Amazon's Product Search engine, we make fairly heavy use of these expression implementations. I pulled this change into our Lucene fork early (currently on 9.7) and ran internal benchmarks we have, and this produced a ~23% redline QPS improvement. Milage may vary of course, but the impact was significant so others may find a nice win as well (if heavy expression users).


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


[GitHub] [lucene] gsmiller commented on a diff in pull request #12560: Defer #advanceExact on expression dependencies until their values are needed

Posted by "gsmiller (via GitHub)" <gi...@apache.org>.
gsmiller commented on code in PR #12560:
URL: https://github.com/apache/lucene/pull/12560#discussion_r1327831100


##########
lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionValueSource.java:
##########
@@ -90,16 +90,24 @@ public DoubleValues getValues(LeafReaderContext readerContext, DoubleValues scor
   static DoubleValues zeroWhenUnpositioned(DoubleValues in) {
     return new DoubleValues() {
 
-      boolean positioned = false;
+      int currentDoc = -1;
+      double value;
+      boolean computed = false;
 
       @Override
       public double doubleValue() throws IOException {
-        return positioned ? in.doubleValue() : 0;
+        if (computed == false) {
+          value = in.advanceExact(currentDoc) ? in.doubleValue() : 0;
+          computed = true;
+        }
+        return value;
       }
 
       @Override
-      public boolean advanceExact(int doc) throws IOException {
-        return positioned = in.advanceExact(doc);
+      public boolean advanceExact(int doc) {
+        currentDoc = doc;

Review Comment:
   Thanks for the suggestion. I'll include that logic.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


[GitHub] [lucene] gsmiller commented on a diff in pull request #12560: Defer #advanceExact on expression dependencies until their values are needed

Posted by "gsmiller (via GitHub)" <gi...@apache.org>.
gsmiller commented on code in PR #12560:
URL: https://github.com/apache/lucene/pull/12560#discussion_r1327624072


##########
lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionFunctionValues.java:
##########
@@ -39,21 +39,21 @@ class ExpressionFunctionValues extends DoubleValues {
   }
 
   @Override
-  public boolean advanceExact(int doc) {
+  public boolean advanceExact(int doc) throws IOException {
     if (currentDoc == doc) {
       return true;
     }
+    for (DoubleValues v : functionValues) {
+      v.advanceExact(doc);

Review Comment:
   In this layer, yes. But all these DoubleValue instances come from `ExpressionValueSource#zeroWhenUnpositioned` (see the setup done in `ExpressionValueSource#getValues`). In this PR, I'm proposing pushing down the lazy advancing into that layer. Does this make sense? We could keep the lazy advance logic up here as well, but I didn't think it was really necessary since the delegation to the underlying advancing is now lazy...



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


[GitHub] [lucene] msokolov commented on a diff in pull request #12560: Defer #advanceExact on expression dependencies until their values are needed

Posted by "msokolov (via GitHub)" <gi...@apache.org>.
msokolov commented on code in PR #12560:
URL: https://github.com/apache/lucene/pull/12560#discussion_r1327858524


##########
lucene/expressions/src/java/org/apache/lucene/expressions/ExpressionFunctionValues.java:
##########
@@ -39,21 +39,21 @@ class ExpressionFunctionValues extends DoubleValues {
   }
 
   @Override
-  public boolean advanceExact(int doc) {
+  public boolean advanceExact(int doc) throws IOException {
     if (currentDoc == doc) {
       return true;
     }
+    for (DoubleValues v : functionValues) {
+      v.advanceExact(doc);

Review Comment:
   thank you, nice!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org