You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "JoshRosen (via GitHub)" <gi...@apache.org> on 2024/06/06 23:55:17 UTC

[PR] [SPARK-37448][SQL] Multiple performance optimizations related to CurrentOrigin.withOrigin [spark]

JoshRosen opened a new pull request, #46908:
URL: https://github.com/apache/spark/pull/46908

   ### What changes were proposed in this pull request?
   
   This PR implements multiple performance optimizations related to `CurrentOrigin.withOrigin`.
   
   #### Avoiding `withOrigin` in cases where PartialFunctions are undefined
   
   `CurrentOrigin.withOrigin` can be expensive because it currently performs a ThreadLocal `get` and two ThreadLocal `set` calls on every call, plus the overhead of constructing the lambda to pass to `withOrigin` itself.
   
   Many parts of Catalyst have a pattern where a tree transformation method will contain code like
   
   ```scala
   val afterRule = CurrentOrigin.withOrigin {
     rule.applyOrElse(this, identity[BaseType])
   }
   ```
   
   where `rule` is a PartialFunction. With this code, we must pay the cost of `withOrigin` even in cases where the `orElse` branch (which returns `identity[BaseType](this)` a.k.a. `this`) is hit because the PartialFunction does not apply (e.g. because we have a transformation which is only defined for `Alias` but is invoked over an entire expression tree).
   
   If we instead rewrite this as
   
   ```scala
   val afterRule = if (rule.isDefinedAt(this)) {
     CurrentOrigin.withOrigin(origin) {
       rule.apply(this)
     }
   } else {
     this
   }
   ```
   
   we can avoid the `withOrigin` overhead in cases where the rule / PartialFunction is not defined.
   
   This is essentially free because the [default implementation of PartialFunction.applyOrElse](https://github.com/scala/scala/blob/08060e8148972fbec3aff3b56246852adf49605c/src/library/scala/PartialFunction.scala#L213-L214) is
   
   ```scala
   def applyOrElse[A1 <: A, B1 >: B](x: A1, default: A1 => B1): B1 =
       if (isDefinedAt(x)) apply(x) else default(x)
   ```
   
   I applied this style of optimization in `TreeNode`, `QueryPlan`, and `AnalysisHelper`.
   
   #### Apply similar skipping optimization in `ColumnResolutionHelper`:
   
   In `ColumnResolutionHelper` there were two methods that did something like
   
   ```scala
   CurrentOrigin.withOrigin(origin) {
     if (earlyExitCondition) return
     // rest of code
   }
   ```
   
   which I modified to
   
   ```scala
   if (earlyExitCondition) return
   CurrentOrigin.withOrigin(origin) {
     // rest of code
   }
   ```
   
   to avoid the `withOrigin` overhead in the early-exit case.
   
   #### Small optimization to `withOrigin` itself:
   
   `withOrigin` may often be setting a new origin that is exactly the same object as the current origin. We can detect this case with a cheap reference equality check and then skip a ThreadLocal set.
   
   ### Why are the changes needed?
   
   Improves Catalyst performance, both in production and in unit tests.
   
   ### Does this PR introduce _any_ user-facing change?
   
   No.
   
   
   ### How was this patch tested?
   
   I believe that should be covered by existing unit tests.
   
   With these changes, I observed a ~10%+ end-to-end runtime improvement on certain large test suites in our fork of Apache Spark.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   No.


-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-37448][SQL] Multiple performance optimizations related to CurrentOrigin.withOrigin [spark]

Posted by "JoshRosen (via GitHub)" <gi...@apache.org>.
JoshRosen commented on code in PR #46908:
URL: https://github.com/apache/spark/pull/46908#discussion_r1630802165


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ColumnResolutionHelper.scala:
##########
@@ -315,32 +317,34 @@ trait ColumnResolutionHelper extends Logging with DataTypeErrorsBase {
       }.map(e => Alias(e, nameParts.last)())
     }
 
-    def innerResolve(e: Expression, isTopLevel: Boolean): Expression = withOrigin(e.origin) {
+    def innerResolve(e: Expression, isTopLevel: Boolean): Expression = {
       if (e.resolved || !e.containsAnyPattern(UNRESOLVED_ATTRIBUTE, TEMP_RESOLVED_COLUMN)) return e

Review Comment:
   I think that I've introduced a subtle bug here due to non-local return behavior: previously, the return was nested inside of a closure passed to `withOrigin` and returned from that closure, but now the return is returning from the _outer_ method which contains `innerResolve` rather than returning from `innerResolve` itself. 
   
   An earlier version that I tested locally had used a return-free if-else pattern with additional braces to make the pattern clearer. Let me revert back to that approach here: https://github.com/apache/spark/pull/46908/commits/72cb44d98b0287db637a87c4bdb0ce8b6bd36220



-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-37448][SQL] Multiple performance optimizations related to CurrentOrigin.withOrigin [spark]

Posted by "JoshRosen (via GitHub)" <gi...@apache.org>.
JoshRosen commented on code in PR #46908:
URL: https://github.com/apache/spark/pull/46908#discussion_r1630802165


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ColumnResolutionHelper.scala:
##########
@@ -315,32 +317,34 @@ trait ColumnResolutionHelper extends Logging with DataTypeErrorsBase {
       }.map(e => Alias(e, nameParts.last)())
     }
 
-    def innerResolve(e: Expression, isTopLevel: Boolean): Expression = withOrigin(e.origin) {
+    def innerResolve(e: Expression, isTopLevel: Boolean): Expression = {
       if (e.resolved || !e.containsAnyPattern(UNRESOLVED_ATTRIBUTE, TEMP_RESOLVED_COLUMN)) return e

Review Comment:
   I think that I've introduced a subtle bug here due to non-local return behavior: previously, the return was nested inside of a closure passed to `withOrigin` and returned from that closure, but now the return is returning from the _outer_ method which contains `innerResolve` rather than returning from `innerResolve` itself. 
   
   An earlier version that I tested locally had used a return-free if-else pattern with additional braces to make the pattern clearer. Let me revert back to that approach here: https://github.com/apache/spark/pull/46908/commits/72cb44d98b0287db637a87c4bdb0ce8b6bd36220
   
   **Edit**: on reflection, I'm not actually sure whether there is a bug there but the non-local return in a nested method is hard to reason about.



-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [WIP][SPARK-37448][SQL] Multiple performance optimizations related to CurrentOrigin.withOrigin [spark]

Posted by "JoshRosen (via GitHub)" <gi...@apache.org>.
JoshRosen closed pull request #46908: [WIP][SPARK-37448][SQL] Multiple performance optimizations related to CurrentOrigin.withOrigin 
URL: https://github.com/apache/spark/pull/46908


-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-37448][SQL] Multiple performance optimizations related to CurrentOrigin.withOrigin [spark]

Posted by "JoshRosen (via GitHub)" <gi...@apache.org>.
JoshRosen commented on code in PR #46908:
URL: https://github.com/apache/spark/pull/46908#discussion_r1630419417


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ColumnResolutionHelper.scala:
##########
@@ -139,62 +139,64 @@ trait ColumnResolutionHelper extends Logging with DataTypeErrorsBase {
 
     val resolver = conf.resolver
 
-    def innerResolve(e: Expression, isTopLevel: Boolean): Expression = withOrigin(e.origin) {

Review Comment:
   Most of the changes in this file are from re-indentation, so I recommend reviewing the whitespace-free diff instead: https://github.com/apache/spark/pull/46908/files?diff=unified&w=1



-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-37448][SQL] Multiple performance optimizations related to CurrentOrigin.withOrigin [spark]

Posted by "JoshRosen (via GitHub)" <gi...@apache.org>.
JoshRosen commented on code in PR #46908:
URL: https://github.com/apache/spark/pull/46908#discussion_r1630802165


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ColumnResolutionHelper.scala:
##########
@@ -315,32 +317,34 @@ trait ColumnResolutionHelper extends Logging with DataTypeErrorsBase {
       }.map(e => Alias(e, nameParts.last)())
     }
 
-    def innerResolve(e: Expression, isTopLevel: Boolean): Expression = withOrigin(e.origin) {
+    def innerResolve(e: Expression, isTopLevel: Boolean): Expression = {
       if (e.resolved || !e.containsAnyPattern(UNRESOLVED_ATTRIBUTE, TEMP_RESOLVED_COLUMN)) return e

Review Comment:
   I think that I may introduced a subtle bug here due to non-local return behavior: previously, the return was nested inside of a closure passed to `withOrigin` and returned from that closure, but now the return potentially might be returning from the _outer_ method which contains `innerResolve` rather than returning from `innerResolve` itself?
   
   An earlier version that I tested locally had used a return-free if-else pattern with additional braces to make the pattern clearer. Let me revert back to that approach here: https://github.com/apache/spark/pull/46908/commits/72cb44d98b0287db637a87c4bdb0ce8b6bd36220
   
   **Edit**: on reflection, I'm not actually sure whether there is a bug there but the non-local return in a nested method is hard to reason about so I think the if-else is clearer.



-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-37448][SQL] Multiple performance optimizations related to CurrentOrigin.withOrigin [spark]

Posted by "JoshRosen (via GitHub)" <gi...@apache.org>.
JoshRosen commented on code in PR #46908:
URL: https://github.com/apache/spark/pull/46908#discussion_r1630821788


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/QueryPlan.scala:
##########
@@ -343,8 +343,14 @@ abstract class QueryPlan[PlanType <: QueryPlan[PlanType]]
             }
         }
 
-        val (planAfterRule, newAttrMapping) = CurrentOrigin.withOrigin(origin) {
-          rule.applyOrElse(newPlan, (plan: PlanType) => plan -> Nil)
+        val (planAfterRule, newAttrMapping) = {
+          if (rule.isDefinedAt(newPlan)) {
+            CurrentOrigin.withOrigin(origin) {
+              rule.apply(newPlan)
+            }
+          } else {
+            plan -> Nil

Review Comment:
   This should be `newPlan -> Nil`; fixed in 2cd4d9deaa1b08c89a513ab6ba9b61481884e754



-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-37448][SQL] Multiple performance optimizations related to CurrentOrigin.withOrigin [spark]

Posted by "JoshRosen (via GitHub)" <gi...@apache.org>.
JoshRosen commented on code in PR #46908:
URL: https://github.com/apache/spark/pull/46908#discussion_r1630802165


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ColumnResolutionHelper.scala:
##########
@@ -315,32 +317,34 @@ trait ColumnResolutionHelper extends Logging with DataTypeErrorsBase {
       }.map(e => Alias(e, nameParts.last)())
     }
 
-    def innerResolve(e: Expression, isTopLevel: Boolean): Expression = withOrigin(e.origin) {
+    def innerResolve(e: Expression, isTopLevel: Boolean): Expression = {
       if (e.resolved || !e.containsAnyPattern(UNRESOLVED_ATTRIBUTE, TEMP_RESOLVED_COLUMN)) return e

Review Comment:
   I think that Iv'e introduced a subtle bug here due to non-local return behavior: previously, the return was nested inside of a closure passed to `withOrigin` and returned from that closure, but now the return is returning from the _outer_ method which contains `innerResolve` rather than returning from `innerResolve` itself. 
   
   An earlier version that I tested locally had used a return-free if-else pattern with additional braces to make the pattern clearer. Let me revert back to that here.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ColumnResolutionHelper.scala:
##########
@@ -315,32 +317,34 @@ trait ColumnResolutionHelper extends Logging with DataTypeErrorsBase {
       }.map(e => Alias(e, nameParts.last)())
     }
 
-    def innerResolve(e: Expression, isTopLevel: Boolean): Expression = withOrigin(e.origin) {
+    def innerResolve(e: Expression, isTopLevel: Boolean): Expression = {
       if (e.resolved || !e.containsAnyPattern(UNRESOLVED_ATTRIBUTE, TEMP_RESOLVED_COLUMN)) return e

Review Comment:
   I think that I've introduced a subtle bug here due to non-local return behavior: previously, the return was nested inside of a closure passed to `withOrigin` and returned from that closure, but now the return is returning from the _outer_ method which contains `innerResolve` rather than returning from `innerResolve` itself. 
   
   An earlier version that I tested locally had used a return-free if-else pattern with additional braces to make the pattern clearer. Let me revert back to that here.



-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-37448][SQL] Multiple performance optimizations related to CurrentOrigin.withOrigin [spark]

Posted by "JoshRosen (via GitHub)" <gi...@apache.org>.
JoshRosen commented on code in PR #46908:
URL: https://github.com/apache/spark/pull/46908#discussion_r1630802165


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ColumnResolutionHelper.scala:
##########
@@ -315,32 +317,34 @@ trait ColumnResolutionHelper extends Logging with DataTypeErrorsBase {
       }.map(e => Alias(e, nameParts.last)())
     }
 
-    def innerResolve(e: Expression, isTopLevel: Boolean): Expression = withOrigin(e.origin) {
+    def innerResolve(e: Expression, isTopLevel: Boolean): Expression = {
       if (e.resolved || !e.containsAnyPattern(UNRESOLVED_ATTRIBUTE, TEMP_RESOLVED_COLUMN)) return e

Review Comment:
   I think that I've introduced a subtle bug here due to non-local return behavior: previously, the return was nested inside of a closure passed to `withOrigin` and returned from that closure, but now the return is returning from the _outer_ method which contains `innerResolve` rather than returning from `innerResolve` itself. 
   
   An earlier version that I tested locally had used a return-free if-else pattern with additional braces to make the pattern clearer. Let me revert back to that approach here: https://github.com/apache/spark/pull/46908/commits/72cb44d98b0287db637a87c4bdb0ce8b6bd36220
   
   **Edit**: on reflection, I'm not actually sure whether there is a bug there but the non-local return in a nested method is hard to reason about so I think the if-else is clearer.



-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [WIP][SPARK-37448][SQL] Multiple performance optimizations related to CurrentOrigin.withOrigin [spark]

Posted by "JoshRosen (via GitHub)" <gi...@apache.org>.
JoshRosen commented on PR #46908:
URL: https://github.com/apache/spark/pull/46908#issuecomment-2154434517

   I'm switching this back to WIP pending some further performance profiling over a wider array of workloads.
   
   It turns out that the Scala compiler generates specialized implementations of `applyOrElse` for `PartialFunction` instances which can be faster than calling `isDefinedAt()` followed by `apply()`. In the case where functions rarely apply my approach here may be faster, but if the case conditions are complex then my change may lead to duplicate evaluations of the case conditions and could potentially harm performance.
   
   Given this, I may end up splitting this PR into smaller pieces, opening a separate PR to land only the non `applyOrElse` parts of the changes (e.g. keeping the `ColumnResolutionHelper` changes and the changes to `withOrigin` itself but deferring the other changes.


-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org