You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "monkeyboy123 (via GitHub)" <gi...@apache.org> on 2024/05/22 13:28:49 UTC

[PR] Using addAll instead of add function in fromAccumulatorInfos method of TaskMetrics Class [spark]

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

   <!--
   Thanks for sending a pull request!  Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
     2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
     3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
     4. Be sure to keep the PR description updated to reflect all changes.
     5. Please write your PR title to summarize what this PR proposes.
     6. If possible, provide a concise example to reproduce the issue for a faster review.
     7. If you want to add a new configuration, please read the guideline first for naming configurations in
        'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
     8. If you want to add or modify an error type or message, please read the guideline first in
        'common/utils/src/main/resources/error/README.md'.
   -->
   
   ### What changes were proposed in this pull request?
   <!--
   Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue. 
   If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
     1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
     2. If you fix some SQL features, you can provide some references of other DBMSes.
     3. If there is design documentation, please add the link.
     4. If there is a discussion in the mailing list, please add the link.
   -->
   Using addAll instead of add function in fromAccumulators method of TaskMetrics.
   
   ### Why are the changes needed?
   <!--
   Please clarify why the changes are needed. For instance,
     1. If you propose a new API, clarify the use case for a new API.
     2. If you fix a bug, you can clarify why it is a bug.
   -->
   To Improve performance.
    
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   Note that it means *any* user-facing change including all aspects such as the documentation fix.
   If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   yes.
   
   ### How was this patch tested?
   <!--
   If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
   If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why it was difficult to add.
   If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
   -->
   No Tests.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   <!--
   If generative AI tooling has been used in the process of authoring this patch, please include the
   phrase: 'Generated-by: ' followed by the name of the tool and its version.
   If no, write 'No'.
   Please refer to the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
   -->
   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-48391][CORE]Using addAll instead of add function in fromAccumulatorInfos method of TaskMetrics Class [spark]

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

   Thanks for the reivew, all of you.


-- 
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-48391][CORE]Using addAll instead of add function in fromAccumulatorInfos method of TaskMetrics Class [spark]

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


##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -328,16 +328,15 @@ private[spark] object TaskMetrics extends Logging {
    */
   def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
     val tm = new TaskMetrics
-    for (acc <- accums) {
+    val (innerAccums, externalAccums) = accums.
+      partition(t => t.name.isDefined && tm.nameToAccums.contains(t.name.get))

Review Comment:
   Just thinking aloud here:
   
   Do we do a lot of intermediate collections allocations in this `.partition` operation itself? e.g. under the hood, would we be resizing an array builder or doing a bunch of linked list construction operations?
   
   I am wondering whether instead it might be faster (or at least more predicable / easier to reason about) to construct a Java ArrayBuilder / ArrayBuffer, imperatively append the external accumulators to it, then do a single addAll call at the end, e.g. something like
   
   ```scala
   def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
       val tm = new TaskMetrics
       val externalAccums = new java.util.ArrayList[AccumulatorV2[Any, Any]]()
       for (acc <- accums) {
         val name = acc.name
         if (name.isDefined && tm.nameToAccums.contains(name.get)) {
           val tmAcc = tm.nameToAccums(name.get).asInstanceOf[AccumulatorV2[Any, Any]]
           tmAcc.metadata = acc.metadata
           tmAcc.merge(acc.asInstanceOf[AccumulatorV2[Any, Any]])
         } else {
           externalAccums.add(acc)
         }
       }
       tm._externalAccums.addAll(externalAccums)
       tm
     }
   ```
   
   This is less net code churn and is probably either comparable in performance or faster, and avoids any performance unpredictability as a function of the Seq type.



-- 
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-48391][CORE]Using addAll instead of add function in fromAccumulatorInfos method of TaskMetrics Class [spark]

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


##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -328,16 +328,15 @@ private[spark] object TaskMetrics extends Logging {
    */
   def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
     val tm = new TaskMetrics
-    for (acc <- accums) {
+    val (innerAccums, externalAccums) = accums.

Review Comment:
   Should we extract a local variable `val nameToAccums = tm.nameToAccums`? `tm.nameToAccums` is lazy and frequent access may have some cost.



-- 
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-48391][CORE]Using addAll instead of add function in fromAccumulatorInfos method of TaskMetrics Class [spark]

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


##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -328,16 +328,15 @@ private[spark] object TaskMetrics extends Logging {
    */
   def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
     val tm = new TaskMetrics
-    for (acc <- accums) {
+    val (innerAccums, externalAccums) = accums.

Review Comment:
   nit: `innerAccums` -> `internalAccums`



-- 
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-48391][CORE]Using addAll instead of add function in fromAccumulatorInfos method of TaskMetrics Class [spark]

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


##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -328,16 +328,15 @@ private[spark] object TaskMetrics extends Logging {
    */
   def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
     val tm = new TaskMetrics
-    for (acc <- accums) {
+    val (innerAccums, externalAccums) = accums.

Review Comment:
   > tm.nameToAccums is a fixed size LinkedHashMap, why do we need create another local variable?
   
   I think the concern was that each call to `.nameToAccums` under the hood goes through a `lazy val` which is slightly more expensive to access than an ordinary field.
   
   On the other hand, I think the old and new code are performing the same total number of `nameToAccums` accesses:
   
   - Before, we'd call `tm.nameToAccums` once per named accumulator to check whether it was a task metric, then a second time for each task metric to retrieve the actual value.
   - In the new code we perform the same calls but in a different sequence: we now do all of the initial calls in a batch then do the second "retrieve the actual accum" in a second pass.
   
   Given that we're performing the same total number of calls, I don't think that the new code represents a perf. regression w.r.t. those accesses, so the suggestion of storing `val nameToAccums = tm.nameToAccums` would just be a small performance optimization rather than a perf. regression fix.



-- 
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-48391][CORE]Using addAll instead of add function in fromAccumulatorInfos method of TaskMetrics Class [spark]

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


##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -328,16 +328,15 @@ private[spark] object TaskMetrics extends Logging {
    */
   def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
     val tm = new TaskMetrics
-    for (acc <- accums) {
+    val (innerAccums, externalAccums) = accums.
+      partition(t => t.name.isDefined && tm.nameToAccums.contains(t.name.get))

Review Comment:
   > Do we do a lot of intermediate collections allocations in this `.partition` operation itself? e.g. under the hood, would we be resizing an array builder or doing a bunch of link
   
   Good suggestion,  as I found that  it costs much time in  fromAccumulators function in some scenarios  before this pr (as Flame Graphs shows)
   ![image](https://github.com/apache/spark/assets/9074114/4dbb2d15-7d3e-47ad-bb05-2f83e8e7771b)
   .Also we do a test offline  `tm._externalAccums.addAll` VS `tm._externalAccums.add`, the first one  speeds up the compution.



-- 
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-48391][CORE]Using addAll instead of add function in fromAccumulatorInfos method of TaskMetrics Class [spark]

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


##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -328,16 +328,15 @@ private[spark] object TaskMetrics extends Logging {
    */
   def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
     val tm = new TaskMetrics
-    for (acc <- accums) {
+    val (innerAccums, externalAccums) = accums.
+      partition(t => t.name.isDefined && tm.nameToAccums.contains(t.name.get))

Review Comment:
   > Do we do a lot of intermediate collections allocations in this `.partition` operation itself? e.g. under the hood, would we be resizing an array builder or doing a bunch of link
   
   Good suggestion,  as I found that  it costs much time in  fromAccumulators function in some scenarios  (as Flame Graphs shows)
   ![image](https://github.com/apache/spark/assets/9074114/4dbb2d15-7d3e-47ad-bb05-2f83e8e7771b)
   .Also we do a test offline  `tm._externalAccums.addAll` VS `tm._externalAccums.add`, the first one  speed up the compution.



-- 
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-48391][CORE]Using addAll instead of add function in fromAccumulatorInfos method of TaskMetrics Class [spark]

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


##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -328,16 +328,15 @@ private[spark] object TaskMetrics extends Logging {
    */
   def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
     val tm = new TaskMetrics
-    for (acc <- accums) {
+    val (innerAccums, externalAccums) = accums.

Review Comment:
   Hmm, I does not understand what you mean? tm.nameToAccums is a fixed size
    LinkedHashMap, why do we need create another local variable?Did I miss something else?
   



-- 
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-48391][CORE]Using addAll instead of add function in fromAccumulatorInfos method of TaskMetrics Class [spark]

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


##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -328,16 +328,15 @@ private[spark] object TaskMetrics extends Logging {
    */
   def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
     val tm = new TaskMetrics
-    for (acc <- accums) {
+    val (innerAccums, externalAccums) = accums.
+      partition(t => t.name.isDefined && tm.nameToAccums.contains(t.name.get))

Review Comment:
   Just thinking aloud here:
   
   Do we do a lot of intermediate collections allocations in this `.partition` operation itself? e.g. under the hood, would we be resizing an array builder or doing a bunch of linked list construction operations?
   
   I am wondering whether instead it might be faster (or at least more predicable / easier to reason about) to construct a Java ArrayBuilder / ArrayBuffer, imperatively append the external accumulators to it, then do a single addAll call at the end, e.g. something like
   
   ```scala
   def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
       val tm = new TaskMetrics
       val externalAccums = new java.util.ArrayList[AccumulatorV2[Any, Any]]()
       for (acc <- accums) {
         val name = acc.name
         if (name.isDefined && tm.nameToAccums.contains(name.get)) {
           val tmAcc = tm.nameToAccums(name.get).asInstanceOf[AccumulatorV2[Any, Any]]
           tmAcc.metadata = acc.metadata
           tmAcc.merge(acc.asInstanceOf[AccumulatorV2[Any, Any]])
         } else {
           externalAccums.add(acc)
         }
       }
       tm._externalAccums.addAll(externalAccums)
       tm
     }
   ```
   
   This is less net code churn and is probably either comparable in performance or faster, and avoids any performance unpredictability as a function of the Seq type.
   
   Given that this code runs in a hot path, I think the use of imperative mutation vs. a more pure functional approach is fine.



-- 
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-48391][CORE]Using addAll instead of add function in fromAccumulatorInfos method of TaskMetrics Class [spark]

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


##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -328,16 +328,15 @@ private[spark] object TaskMetrics extends Logging {
    */
   def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
     val tm = new TaskMetrics
-    for (acc <- accums) {
+    val (innerAccums, externalAccums) = accums.

Review Comment:
   > nit: `innerAccums` -> `internalAccums`
   
   done.



-- 
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-48391][CORE]Using addAll instead of add function in fromAccumulatorInfos method of TaskMetrics Class [spark]

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


##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -328,16 +328,15 @@ private[spark] object TaskMetrics extends Logging {
    */
   def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
     val tm = new TaskMetrics
-    for (acc <- accums) {
+    val (innerAccums, externalAccums) = accums.
+      partition(t => t.name.isDefined && tm.nameToAccums.contains(t.name.get))
+    for (acc <- innerAccums) {
       val name = acc.name
-      if (name.isDefined && tm.nameToAccums.contains(name.get)) {
-        val tmAcc = tm.nameToAccums(name.get).asInstanceOf[AccumulatorV2[Any, Any]]
-        tmAcc.metadata = acc.metadata
-        tmAcc.merge(acc.asInstanceOf[AccumulatorV2[Any, Any]])
-      } else {
-        tm._externalAccums.add(acc)
-      }
+      val tmAcc = tm.nameToAccums(name.get).asInstanceOf[AccumulatorV2[Any, Any]]
+      tmAcc.metadata = acc.metadata
+      tmAcc.merge(acc.asInstanceOf[AccumulatorV2[Any, Any]])
     }
+    tm._externalAccums.addAll(externalAccums.asJava)

Review Comment:
   Yeah, it looks like `CopyOnWriteArrayList.addAll` performs at most one array copy: https://github.com/AdoptOpenJDK/openjdk-jdk12u/blame/master/src/java.base/share/classes/java/util/concurrent/CopyOnWriteArrayList.java#L724-L751



-- 
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-48391][CORE]Using addAll instead of add function in fromAccumulatorInfos method of TaskMetrics Class [spark]

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


##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -328,16 +328,15 @@ private[spark] object TaskMetrics extends Logging {
    */
   def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
     val tm = new TaskMetrics
-    for (acc <- accums) {
+    val (innerAccums, externalAccums) = accums.

Review Comment:
   
   > I think the concern was that each call to `.nameToAccums` under the hood goes through a `lazy val` which is slightly more expensive to access than an ordinary field.
   > 
   
   i gotten.



-- 
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-48391][CORE]Using addAll instead of add function in fromAccumulatorInfos method of TaskMetrics Class [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan closed pull request #46705: [SPARK-48391][CORE]Using addAll instead of add function in fromAccumulatorInfos method of TaskMetrics Class
URL: https://github.com/apache/spark/pull/46705


-- 
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-48391][CORE]Using addAll instead of add function in fromAccumulatorInfos method of TaskMetrics Class [spark]

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


##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -328,16 +328,15 @@ private[spark] object TaskMetrics extends Logging {
    */
   def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
     val tm = new TaskMetrics
-    for (acc <- accums) {
+    val (innerAccums, externalAccums) = accums.

Review Comment:
   Hmm, I does not understand what you mean? tm.nameToAccums is a fixed size
    LinkedHashMap, why do we need create another local variable?Did I miss something else?
   



##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -328,16 +328,15 @@ private[spark] object TaskMetrics extends Logging {
    */
   def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
     val tm = new TaskMetrics
-    for (acc <- accums) {
+    val (innerAccums, externalAccums) = accums.

Review Comment:
   > Should we extract a local variable `val nameToAccums = tm.nameToAccums`? `tm.nameToAccums` is lazy and frequent access may have some cost.
   
   Hmm, I does not understand what you mean? tm.nameToAccums is a fixed size
    LinkedHashMap, why do we need create another local variable?Did I miss something else?
   



-- 
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-48391][CORE]Using addAll instead of add function in fromAccumulatorInfos method of TaskMetrics Class [spark]

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


##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -328,16 +328,15 @@ private[spark] object TaskMetrics extends Logging {
    */
   def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
     val tm = new TaskMetrics
-    for (acc <- accums) {
+    val (innerAccums, externalAccums) = accums.
+      partition(t => t.name.isDefined && tm.nameToAccums.contains(t.name.get))
+    for (acc <- innerAccums) {
       val name = acc.name
-      if (name.isDefined && tm.nameToAccums.contains(name.get)) {
-        val tmAcc = tm.nameToAccums(name.get).asInstanceOf[AccumulatorV2[Any, Any]]
-        tmAcc.metadata = acc.metadata
-        tmAcc.merge(acc.asInstanceOf[AccumulatorV2[Any, Any]])
-      } else {
-        tm._externalAccums.add(acc)
-      }
+      val tmAcc = tm.nameToAccums(name.get).asInstanceOf[AccumulatorV2[Any, Any]]
+      tmAcc.metadata = acc.metadata
+      tmAcc.merge(acc.asInstanceOf[AccumulatorV2[Any, Any]])
     }
+    tm._externalAccums.addAll(externalAccums.asJava)

Review Comment:
   So this change reduce the copy times from `externalAccums#size` to one?



##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -328,16 +328,15 @@ private[spark] object TaskMetrics extends Logging {
    */
   def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
     val tm = new TaskMetrics
-    for (acc <- accums) {
+    val (innerAccums, externalAccums) = accums.
+      partition(t => t.name.isDefined && tm.nameToAccums.contains(t.name.get))
+    for (acc <- innerAccums) {
       val name = acc.name
-      if (name.isDefined && tm.nameToAccums.contains(name.get)) {
-        val tmAcc = tm.nameToAccums(name.get).asInstanceOf[AccumulatorV2[Any, Any]]
-        tmAcc.metadata = acc.metadata
-        tmAcc.merge(acc.asInstanceOf[AccumulatorV2[Any, Any]])
-      } else {
-        tm._externalAccums.add(acc)
-      }
+      val tmAcc = tm.nameToAccums(name.get).asInstanceOf[AccumulatorV2[Any, Any]]
+      tmAcc.metadata = acc.metadata
+      tmAcc.merge(acc.asInstanceOf[AccumulatorV2[Any, Any]])
     }
+    tm._externalAccums.addAll(externalAccums.asJava)

Review Comment:
   So this change reduces the copy times from `externalAccums#size` to one?



-- 
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-48391][CORE]Using addAll instead of add function in fromAccumulatorInfos method of TaskMetrics Class [spark]

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

   Retest this please.


-- 
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-48391][CORE]Using addAll instead of add function in fromAccumulatorInfos method of TaskMetrics Class [spark]

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


##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -328,16 +328,15 @@ private[spark] object TaskMetrics extends Logging {
    */
   def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
     val tm = new TaskMetrics
-    for (acc <- accums) {
+    val (innerAccums, externalAccums) = accums.
+      partition(t => t.name.isDefined && tm.nameToAccums.contains(t.name.get))

Review Comment:
   > Do we do a lot of intermediate collections allocations in this `.partition` operation itself? e.g. under the hood, would we be resizing an array builder or doing a bunch of link
   
   Good suggestion,  as I found that  it costs much time in  fromAccumulators function in some scenarios  before this pr (as Flame Graphs shows)
   ![image](https://github.com/apache/spark/assets/9074114/4dbb2d15-7d3e-47ad-bb05-2f83e8e7771b)
   .Also we do a test offline  `tm._externalAccums.addAll` VS `tm._externalAccums.add`, the first one  speed up the compution.



-- 
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-48391][CORE]Using addAll instead of add function in fromAccumulatorInfos method of TaskMetrics Class [spark]

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


##########
core/src/main/scala/org/apache/spark/executor/TaskMetrics.scala:
##########
@@ -328,16 +328,15 @@ private[spark] object TaskMetrics extends Logging {
    */
   def fromAccumulators(accums: Seq[AccumulatorV2[_, _]]): TaskMetrics = {
     val tm = new TaskMetrics
-    for (acc <- accums) {
+    val (innerAccums, externalAccums) = accums.

Review Comment:
   > I think the concern was that each call to `.nameToAccums` under the hood goes through a `lazy val` which is slightly more expensive to access than an ordinary field.
   > 
   
   i gotten, thanks.



-- 
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-48391][CORE]Using addAll instead of add function in fromAccumulatorInfos method of TaskMetrics Class [spark]

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

   thanks, merging to master/3.5!


-- 
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-48391][CORE]Using addAll instead of add function in fromAccumulatorInfos method of TaskMetrics Class [spark]

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

   @cloud-fan if you have time please take a look?


-- 
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