Skip to content

Commit ed8ba0c

Browse files
committed
perf: generate the columnar-to-row projection once per executor
CometColumnarToRowExec.doExecute built its UnsafeProjection with UnsafeProjection.create in every partition, which regenerates the projection's Java source each time. The plan runs that path whenever it is outside whole-stage codegen, which a schema with more nested fields than spark.sql.codegen.maxFields always is. For 100 columns nested three levels deep, generating the source took about 19 ms per partition, half the time of a 201-partition JVM shuffle of 1,000 rows. Cache the compiled class per executor by column layout and create a new instance for each partition.
1 parent dc880d6 commit ed8ba0c

3 files changed

Lines changed: 325 additions & 1 deletion

File tree

‎spark/src/main/scala/org/apache/spark/sql/comet/CometColumnarToRowExec.scala‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ case class CometColumnarToRowExec(child: SparkPlan)
7777
// plan (this) in the closure.
7878
val localOutput = this.output
7979
child.executeColumnar().mapPartitionsInternal { batches =>
80-
val toUnsafe = UnsafeProjection.create(localOutput, localOutput)
80+
// Outside whole-stage codegen this runs once per partition, so reuse the generated class.
81+
val toUnsafe = CometUnsafeProjection.create(localOutput)
8182
batches.flatMap { batch =>
8283
numInputBatches += 1
8384
numOutputRows += batch.numRows().toLong
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.spark.sql.comet
21+
22+
import java.util.{LinkedHashMap => JLinkedHashMap, Map => JMap}
23+
import java.util.concurrent.atomic.AtomicLong
24+
25+
import org.apache.spark.sql.catalyst.expressions.{Attribute, BoundReference, CodeGeneratorWithInterpretedFallback, InterpretedUnsafeProjection, UnsafeProjection}
26+
import org.apache.spark.sql.catalyst.expressions.codegen.{CodeAndComment, CodeFormatter, CodegenContext, CodeGenerator, GeneratedClass, GenerateUnsafeProjection}
27+
import org.apache.spark.sql.internal.SQLConf
28+
import org.apache.spark.sql.types.DataType
29+
30+
/**
31+
* Creates the projection that copies each row of a batch into an UnsafeRow, as
32+
* `UnsafeProjection.create(output, output)` does, but generates and compiles its class only once
33+
* per executor for each column layout.
34+
*
35+
* `UnsafeProjection.create` generates the projection's Java source on every call, and only the
36+
* compiled class is cached. `CometColumnarToRowExec` calls it once per partition whenever it runs
37+
* outside whole-stage codegen, which Spark skips for a schema with more fields than
38+
* `spark.sql.codegen.maxFields`, nested fields included. Spark's own scans fall back to rows for
39+
* such schemas, so its `ColumnarToRowExec` rarely meets one, but Comet's operators always produce
40+
* batches. The source grows with the number of nested fields, and each level of nesting repeats
41+
* the work of splitting its children into methods: for 100 columns nested three levels deep,
42+
* generating it took about 19 ms in every partition.
43+
*
44+
* Every call returns a new instance of the shared class, so callers own their projection as they
45+
* do one from `UnsafeProjection.create`.
46+
*/
47+
private[comet] object CometUnsafeProjection
48+
extends CodeGeneratorWithInterpretedFallback[Seq[BoundReference], UnsafeProjection] {
49+
50+
/**
51+
* What the generated source depends on: the type and nullability of each column, and the method
52+
* size at which `CodegenContext.splitExpressions` splits the field writes.
53+
*/
54+
private case class Layout(columns: Seq[(DataType, Boolean)], methodSplitThreshold: Int)
55+
56+
/** The same bound as Spark's compiled class cache, `spark.sql.codegen.cache.maxEntries`. */
57+
private[comet] val MaxCachedClasses = 100
58+
59+
/** Least recently used first. Guarded by `classes.synchronized`. */
60+
private val classes = new JLinkedHashMap[Layout, GeneratedClass](16, 0.75f, true) {
61+
override def removeEldestEntry(eldest: JMap.Entry[Layout, GeneratedClass]): Boolean =
62+
size() > MaxCachedClasses
63+
}
64+
65+
private val classesGenerated = new AtomicLong(0)
66+
67+
/** How many projection classes this executor has generated, for tests. */
68+
private[comet] def generatedClassCount: Long = classesGenerated.get()
69+
70+
/** A projection of rows with the columns of `output` to UnsafeRows. */
71+
def create(output: Seq[Attribute]): UnsafeProjection =
72+
createObject(output.zipWithIndex.map { case (attr, ordinal) =>
73+
BoundReference(ordinal, attr.dataType, attr.nullable)
74+
})
75+
76+
override protected def createCodeGeneratedObject(
77+
columns: Seq[BoundReference]): UnsafeProjection = {
78+
val layout =
79+
Layout(columns.map(c => (c.dataType, c.nullable)), SQLConf.get.methodSplitThreshold)
80+
val cached = classes.synchronized(classes.get(layout))
81+
if (cached != null) {
82+
cached.generate(Array.empty[Any]).asInstanceOf[UnsafeProjection]
83+
} else {
84+
// Generated outside the lock: tasks that miss on the same layout at once each generate
85+
// it, as every task does with UnsafeProjection.create.
86+
val (generated, references) = generate(columns)
87+
// A stored class is instantiated without references, so store only one that needs none.
88+
// GenerateUnsafeProjection references no objects for bound columns.
89+
if (references.isEmpty) {
90+
val _ = classes.synchronized(classes.putIfAbsent(layout, generated))
91+
}
92+
generated.generate(references).asInstanceOf[UnsafeProjection]
93+
}
94+
}
95+
96+
override protected def createInterpretedObject(columns: Seq[BoundReference]): UnsafeProjection =
97+
InterpretedUnsafeProjection.createProjection(columns)
98+
99+
/**
100+
* Generates and compiles the class that `GenerateUnsafeProjection.create` does, from the same
101+
* template, which is repeated here because that method returns only an instance. Returns the
102+
* class with the objects its instances reference.
103+
*/
104+
private def generate(columns: Seq[BoundReference]): (GeneratedClass, Array[Any]) = {
105+
val ctx = new CodegenContext
106+
val eval = GenerateUnsafeProjection.createCode(ctx, columns)
107+
val body =
108+
s"""
109+
|public java.lang.Object generate(Object[] references) {
110+
| return new SpecificUnsafeProjection(references);
111+
|}
112+
|
113+
|class SpecificUnsafeProjection extends ${classOf[UnsafeProjection].getName} {
114+
|
115+
| private Object[] references;
116+
| ${ctx.declareMutableStates()}
117+
|
118+
| public SpecificUnsafeProjection(Object[] references) {
119+
| this.references = references;
120+
| ${ctx.initMutableStates()}
121+
| }
122+
|
123+
| public void initialize(int partitionIndex) {
124+
| ${ctx.initPartition()}
125+
| }
126+
|
127+
| // Scala.Function1 need this
128+
| public java.lang.Object apply(java.lang.Object row) {
129+
| return apply((InternalRow) row);
130+
| }
131+
|
132+
| public UnsafeRow apply(InternalRow ${ctx.INPUT_ROW}) {
133+
| ${eval.code}
134+
| return ${eval.value};
135+
| }
136+
|
137+
| ${ctx.declareAddedFunctions()}
138+
|}
139+
""".stripMargin
140+
val code = CodeFormatter.stripOverlappingComments(
141+
new CodeAndComment(body, ctx.getPlaceHolderToComments()))
142+
val (generatedClass, _) = CodeGenerator.compile(code)
143+
classesGenerated.incrementAndGet()
144+
(generatedClass, ctx.references.toArray)
145+
}
146+
}
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.spark.sql.comet
21+
22+
import java.sql.Timestamp
23+
import java.util.UUID
24+
25+
import org.apache.spark.sql.{CometTestBase, Row}
26+
import org.apache.spark.sql.catalyst.{CatalystTypeConverters, InternalRow}
27+
import org.apache.spark.sql.catalyst.expressions.{AttributeReference, InterpretedUnsafeProjection, UnsafeProjection}
28+
import org.apache.spark.sql.execution.WholeStageCodegenExec
29+
import org.apache.spark.sql.execution.adaptive.AdaptiveSparkPlanHelper
30+
import org.apache.spark.sql.internal.SQLConf
31+
import org.apache.spark.sql.types._
32+
33+
class CometUnsafeProjectionSuite extends CometTestBase with AdaptiveSparkPlanHelper {
34+
35+
/** A schema with every writer GenerateUnsafeProjection nests: structs, arrays and maps. */
36+
private def schema(nestedName: String = "a"): StructType = StructType(
37+
Seq(
38+
StructField("i", IntegerType),
39+
StructField("s", StringType),
40+
StructField("d", DecimalType(38, 10)),
41+
StructField(
42+
"st",
43+
StructType(
44+
Seq(StructField(nestedName, LongType), StructField("b", ArrayType(StringType))))),
45+
StructField(
46+
"m",
47+
MapType(StringType, ArrayType(StructType(Seq(StructField("x", DoubleType)))))),
48+
StructField("aa", ArrayType(ArrayType(IntegerType))),
49+
StructField("bin", BinaryType),
50+
StructField("ts", TimestampType)))
51+
52+
private def attributes(nestedName: String = "a"): Seq[AttributeReference] =
53+
schema(nestedName).fields.toSeq.map(f => AttributeReference(f.name, f.dataType, f.nullable)())
54+
55+
/** Attributes whose layout no earlier call has cached, since a nested name is part of it. */
56+
private def newLayout(): Seq[AttributeReference] =
57+
attributes(s"a_${UUID.randomUUID().toString.replace("-", "")}")
58+
59+
private lazy val rows: Seq[InternalRow] = {
60+
val toCatalyst = CatalystTypeConverters.createToCatalystConverter(schema())
61+
Seq(
62+
Row(
63+
1,
64+
"a",
65+
BigDecimal("1.5"),
66+
Row(2L, Seq("x", null)),
67+
Map("k" -> Seq(Row(1.0), null)),
68+
Seq(Seq(1, null), null),
69+
Array[Byte](1, 2),
70+
new Timestamp(0)),
71+
Row(null, null, null, null, null, null, null, null),
72+
Row(
73+
3,
74+
"字" * 100,
75+
BigDecimal("-1234567890123456789012345678.1234567890"),
76+
Row(null, Seq.empty),
77+
Map.empty,
78+
Seq.empty,
79+
Array.emptyByteArray,
80+
null)).map(toCatalyst(_).asInstanceOf[InternalRow])
81+
}
82+
83+
private def withCodegenOnly(confs: (String, String)*)(f: => Unit): Unit =
84+
withSQLConf((SQLConf.CODEGEN_FACTORY_MODE.key -> "CODEGEN_ONLY") +: confs: _*)(f)
85+
86+
test("rows match UnsafeProjection.create") {
87+
// A small threshold splits the field writes into many methods.
88+
Seq("1024", "64").foreach { threshold =>
89+
withCodegenOnly(SQLConf.CODEGEN_METHOD_SPLIT_THRESHOLD.key -> threshold) {
90+
val attrs = newLayout()
91+
val expected = UnsafeProjection.create(attrs, attrs)
92+
val actual = CometUnsafeProjection.create(attrs)
93+
rows.foreach(row => assert(actual(row) == expected(row)))
94+
}
95+
}
96+
}
97+
98+
test("NO_CODEGEN creates an interpreted projection") {
99+
withSQLConf(SQLConf.CODEGEN_FACTORY_MODE.key -> "NO_CODEGEN") {
100+
val attrs = attributes()
101+
val projection = CometUnsafeProjection.create(attrs)
102+
assert(projection.isInstanceOf[InterpretedUnsafeProjection])
103+
val expected = UnsafeProjection.create(attrs, attrs)
104+
rows.foreach(row => assert(projection(row) == expected(row)))
105+
}
106+
}
107+
108+
test("generates the class once per layout and a new projection per call") {
109+
withCodegenOnly() {
110+
val attrs = newLayout()
111+
val before = CometUnsafeProjection.generatedClassCount
112+
val first = CometUnsafeProjection.create(attrs)
113+
val second = CometUnsafeProjection.create(attrs)
114+
assert(CometUnsafeProjection.generatedClassCount == before + 1)
115+
assert(first ne second)
116+
// Each projection writes into its own row, so the first row survives the second call.
117+
val firstRow = first(rows.head)
118+
val secondRow = second(rows(1))
119+
val expected = UnsafeProjection.create(attrs, attrs)
120+
assert(firstRow == expected(rows.head))
121+
assert(secondRow == expected(rows(1)))
122+
}
123+
}
124+
125+
test("a different method split threshold generates its own class") {
126+
val attrs = newLayout()
127+
val before = CometUnsafeProjection.generatedClassCount
128+
Seq("1024", "64", "1024").foreach { threshold =>
129+
withCodegenOnly(SQLConf.CODEGEN_METHOD_SPLIT_THRESHOLD.key -> threshold) {
130+
CometUnsafeProjection.create(attrs)
131+
}
132+
}
133+
assert(CometUnsafeProjection.generatedClassCount == before + 2)
134+
}
135+
136+
test("keeps the most recently used classes") {
137+
withCodegenOnly() {
138+
val layouts = Seq.fill(CometUnsafeProjection.MaxCachedClasses + 1) {
139+
val name = s"f_${UUID.randomUUID().toString.replace("-", "")}"
140+
Seq(AttributeReference("c", StructType(Seq(StructField(name, IntegerType))))())
141+
}
142+
layouts.foreach(CometUnsafeProjection.create)
143+
val before = CometUnsafeProjection.generatedClassCount
144+
CometUnsafeProjection.create(layouts.last)
145+
assert(CometUnsafeProjection.generatedClassCount == before)
146+
CometUnsafeProjection.create(layouts.head)
147+
assert(CometUnsafeProjection.generatedClassCount == before + 1)
148+
}
149+
}
150+
151+
test("columnar to row outside whole-stage codegen reuses the generated class") {
152+
withTempPath { dir =>
153+
val path = dir.getCanonicalPath
154+
spark
155+
.range(200)
156+
.selectExpr(
157+
"id",
158+
"named_struct('a', id, 'b', array(cast(id as string), null)) as st",
159+
"map(cast(id as string), array(id)) as m")
160+
.repartition(4)
161+
.write
162+
.parquet(path)
163+
// More fields than this keep a plan out of whole-stage codegen, as a wide schema would.
164+
withSQLConf(SQLConf.WHOLESTAGE_MAX_NUM_FIELDS.key -> "2") {
165+
val df = spark.read.parquet(path)
166+
checkSparkAnswerAndOperator(df)
167+
val plan = df.queryExecution.executedPlan
168+
assert(collect(plan) { case c: CometColumnarToRowExec => c }.nonEmpty, plan)
169+
assert(collect(plan) { case w: WholeStageCodegenExec => w }.isEmpty, plan)
170+
// The first run generated the class, so another run over every partition generates none.
171+
val before = CometUnsafeProjection.generatedClassCount
172+
assert(df.collect().length == 200)
173+
assert(CometUnsafeProjection.generatedClassCount == before)
174+
}
175+
}
176+
}
177+
}

0 commit comments

Comments
 (0)