{"action":"created","issue":{"url":"https://api.github.com/repos/apache/spark/issues/23032","repository_url":"https://api.github.com/repos/apache/spark","labels_url":"https://api.github.com/repos/apache/spark/issues/23032/labels{/name}","comments_url":"https://api.github.com/repos/apache/spark/issues/23032/comments","events_url":"https://api.github.com/repos/apache/spark/issues/23032/events","html_url":"https://github.com/apache/spark/pull/23032","id":380623866,"node_id":"MDExOlB1bGxSZXF1ZXN0MjMwNzc2NjAz","number":23032,"title":"[SPARK-26061][SQL][MINOR] Reduce the number of unused UnsafeRowWriters created in whole-stage codegen","user":{"login":"rednaxelafx","id":107834,"node_id":"MDQ6VXNlcjEwNzgzNA==","avatar_url":"https://avatars1.githubusercontent.com/u/107834?v=4","gravatar_id":"","url":"https://api.github.com/users/rednaxelafx","html_url":"https://github.com/rednaxelafx","followers_url":"https://api.github.com/users/rednaxelafx/followers","following_url":"https://api.github.com/users/rednaxelafx/following{/other_user}","gists_url":"https://api.github.com/users/rednaxelafx/gists{/gist_id}","starred_url":"https://api.github.com/users/rednaxelafx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rednaxelafx/subscriptions","organizations_url":"https://api.github.com/users/rednaxelafx/orgs","repos_url":"https://api.github.com/users/rednaxelafx/repos","events_url":"https://api.github.com/users/rednaxelafx/events{/privacy}","received_events_url":"https://api.github.com/users/rednaxelafx/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-11-14T10:08:38Z","updated_at":"2018-11-14T10:12:39Z","closed_at":null,"author_association":"NONE","pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/23032","html_url":"https://github.com/apache/spark/pull/23032","diff_url":"https://github.com/apache/spark/pull/23032.diff","patch_url":"https://github.com/apache/spark/pull/23032.patch"},"body":"## What changes were proposed in this pull request?\r\n\r\nReduce the number of unused `UnsafeRowWriter`s created in whole-stage generated code.\r\nThey come from the `CodegenSupport.consume()` calling `prepareRowVar()`, which uses `GenerateUnsafeProjection.createCode()` and registers an `UnsafeRowWriter` mutable state, regardless of whether or not the downstream (parent) operator will use the `rowVar` or not.\r\nEven when the downstream `doConsume` function doesn't use the `rowVar` (i.e. doesn't put `row.code` as a part of this operator's codegen template), the registered `UnsafeRowWriter` stays there, which makes the init function of the generated code a bit bloated.\r\n\r\nThis PR doesn't heal the root issue, but makes it slightly less painful: when the `doConsume` function is split out, the `prepareRowVar()` function is called twice, so it's double the pain of unused `UnsafeRowWriter`s. This PR simply moves the original call to `prepareRowVar()` down into the `doConsume` split/no-split branch so that we're back to just 1x the pain.\r\n\r\nTo fix the root issue, something that allows the `CodegenSupport` operators to indicate whether or not they're going to use the `rowVar` would be needed. That's a much more elaborate change so I'd like to just make a minor fix first.\r\n\r\ne.g. for this query: `spark.range(10).as[Long].map(x => x + 1).map(x => x * x + 2)`\r\n**Before** (in Spark 2.4.0):\r\n```java\r\n/* 023 */   public void init(int index, scala.collection.Iterator[] inputs) {\r\n/* 024 */     partitionIndex = index;\r\n/* 025 */     this.inputs = inputs;\r\n/* 026 */\r\n/* 027 */     range_taskContext_0 = TaskContext.get();\r\n/* 028 */     range_inputMetrics_0 = range_taskContext_0.taskMetrics().inputMetrics();\r\n/* 029 */     range_mutableStateArray_0[0] = new org.apache.spark.sql.catalyst.expressions.codegen.UnsafeRowWriter(1, 0);\r\n/* 030 */     range_mutableStateArray_0[1] = new org.apache.spark.sql.catalyst.expressions.codegen.UnsafeRowWriter(1, 0);\r\n/* 031 */     range_mutableStateArray_0[2] = new org.apache.spark.sql.catalyst.expressions.codegen.UnsafeRowWriter(1, 0);\r\n/* 032 */     range_mutableStateArray_0[3] = new org.apache.spark.sql.catalyst.expressions.codegen.UnsafeRowWriter(1, 0);\r\n/* 033 */     range_mutableStateArray_0[4] = new org.apache.spark.sql.catalyst.expressions.codegen.UnsafeRowWriter(1, 0);\r\n/* 034 */     range_mutableStateArray_0[5] = new org.apache.spark.sql.catalyst.expressions.codegen.UnsafeRowWriter(1, 0);\r\n/* 035 */     range_mutableStateArray_0[6] = new org.apache.spark.sql.catalyst.expressions.codegen.UnsafeRowWriter(1, 0);\r\n/* 036 */     range_mutableStateArray_0[7] = new org.apache.spark.sql.catalyst.expressions.codegen.UnsafeRowWriter(1, 0);\r\n/* 037 */     range_mutableStateArray_0[8] = new org.apache.spark.sql.catalyst.expressions.codegen.UnsafeRowWriter(1, 0);\r\n/* 038 */\r\n/* 039 */   }\r\n```\r\n9 `UnsafeRowWriter`s created, 1 actually used.\r\n\r\n**After**:\r\n```java\r\n/* 023 */   public void init(int index, scala.collection.Iterator[] inputs) {\r\n/* 024 */     partitionIndex = index;\r\n/* 025 */     this.inputs = inputs;\r\n/* 026 */\r\n/* 027 */     range_taskContext_0 = TaskContext.get();\r\n/* 028 */     range_inputMetrics_0 = range_taskContext_0.taskMetrics().inputMetrics();\r\n/* 029 */     deserializetoobject_mutableStateArray_0[0] = new org.apache.spark.sql.catalyst.expressions.codegen.UnsafeRowWriter(1, 0);\r\n/* 030 */     deserializetoobject_mutableStateArray_0[1] = new org.apache.spark.sql.catalyst.expressions.codegen.UnsafeRowWriter(1, 0);\r\n/* 031 */     deserializetoobject_mutableStateArray_0[2] = new org.apache.spark.sql.catalyst.expressions.codegen.UnsafeRowWriter(1, 0);\r\n/* 032 */     deserializetoobject_mutableStateArray_0[3] = new org.apache.spark.sql.catalyst.expressions.codegen.UnsafeRowWriter(1, 0);\r\n/* 033 */     deserializetoobject_mutableStateArray_0[4] = new org.apache.spark.sql.catalyst.expressions.codegen.UnsafeRowWriter(1, 0);\r\n/* 034 */\r\n/* 035 */   }\r\n```\r\n5 `UnsafeRowWriter`s created, 1 actually used.\r\n\r\n## How was this patch tested?\r\n\r\nExisting tests."},"comment":{"url":"https://api.github.com/repos/apache/spark/issues/comments/438609464","html_url":"https://github.com/apache/spark/pull/23032#issuecomment-438609464","issue_url":"https://api.github.com/repos/apache/spark/issues/23032","id":438609464,"node_id":"MDEyOklzc3VlQ29tbWVudDQzODYwOTQ2NA==","user":{"login":"SparkQA","id":8108735,"node_id":"MDQ6VXNlcjgxMDg3MzU=","avatar_url":"https://avatars1.githubusercontent.com/u/8108735?v=4","gravatar_id":"","url":"https://api.github.com/users/SparkQA","html_url":"https://github.com/SparkQA","followers_url":"https://api.github.com/users/SparkQA/followers","following_url":"https://api.github.com/users/SparkQA/following{/other_user}","gists_url":"https://api.github.com/users/SparkQA/gists{/gist_id}","starred_url":"https://api.github.com/users/SparkQA/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SparkQA/subscriptions","organizations_url":"https://api.github.com/users/SparkQA/orgs","repos_url":"https://api.github.com/users/SparkQA/repos","events_url":"https://api.github.com/users/SparkQA/events{/privacy}","received_events_url":"https://api.github.com/users/SparkQA/received_events","type":"User","site_admin":false},"created_at":"2018-11-14T10:12:39Z","updated_at":"2018-11-14T10:12:39Z","author_association":"NONE","body":"**[Test build #98820 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/98820/testReport)** for PR 23032 at commit [`e48ec9d`](https://github.com/apache/spark/commit/e48ec9da9977846213044c5f6d093202ed0efd60)."}}
{"actor":{"display_login":"SparkQA"}}