-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-18016][SQL] Code Generation: Constant Pool Limit - reduce entries for mutable state #19811
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
d441794
870d106
24d7087
3eb5842
074d711
eafa3f8
90d15f3
c456c07
9ca5ab3
5b36c61
fd51d75
effe918
9df109c
634d494
d3438fd
f4f3754
f1e1fca
0937ef2
4bfcc1a
49119a9
24f49c5
15e967e
d6c1a97
a9d40e9
31914c0
0e45c19
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -163,11 +163,24 @@ class CodegenContext { | |
| * the list of default imports available. | ||
| * Also, generic type arguments are accepted but ignored. | ||
| * @param variableName Name of the field. | ||
| * @param initCode The statement(s) to put into the init() method to initialize this field. | ||
| * @param codeFunctions Function includes statement(s) to put into the init() method to | ||
| * initialize this field. An argument is the name of the mutable state variable | ||
| * If left blank, the field will be default-initialized. | ||
| * @param inline whether the declaration and initialization code may be inlined rather than | ||
| * compacted. If true, the name is not changed | ||
| * @return the name of the mutable state variable, which is either the original name if the | ||
| * variable is inlined to the class, or an array access if the variable is to be stored | ||
| * in an array of variables of the same type and initialization. | ||
| */ | ||
| def addMutableState(javaType: String, variableName: String, initCode: String = ""): Unit = { | ||
| mutableStates += ((javaType, variableName, initCode)) | ||
| def addMutableState( | ||
| javaType: String, | ||
| variableName: String, | ||
| codeFunctions: String => String = _ => "", | ||
|
||
| inline: Boolean = false): String = { | ||
|
||
| val newVariableName = if (!inline) freshName(variableName) else variableName | ||
| val initCode = codeFunctions(newVariableName) | ||
| mutableStates += ((javaType, newVariableName, initCode)) | ||
| newVariableName | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -176,8 +189,7 @@ class CodegenContext { | |
| * data types like: UTF8String, ArrayData, MapData & InternalRow. | ||
| */ | ||
| def addBufferedState(dataType: DataType, variableName: String, initCode: String): ExprCode = { | ||
| val value = freshName(variableName) | ||
| addMutableState(javaType(dataType), value, "") | ||
| val value = addMutableState(javaType(dataType), variableName) | ||
| val code = dataType match { | ||
| case StringType => s"$value = $initCode.clone();" | ||
| case _: StructType | _: ArrayType | _: MapType => s"$value = $initCode.copy();" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -442,9 +442,9 @@ case class DayOfWeek(child: Expression) extends UnaryExpression with ImplicitCas | |
| override protected def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { | ||
| nullSafeCodeGen(ctx, ev, time => { | ||
| val cal = classOf[Calendar].getName | ||
| val c = ctx.freshName("cal") | ||
| val dtu = DateTimeUtils.getClass.getName.stripSuffix("$") | ||
| ctx.addMutableState(cal, c, s"""$c = $cal.getInstance($dtu.getTimeZone("UTC"));""") | ||
| val c = ctx.addMutableState(cal, "cal", | ||
| v => s"""$v = $cal.getInstance($dtu.getTimeZone("UTC"));""") | ||
| s""" | ||
| $c.setTimeInMillis($time * 1000L * 3600L * 24L); | ||
| ${ev.value} = $c.get($cal.DAY_OF_WEEK); | ||
|
|
@@ -484,13 +484,12 @@ case class WeekOfYear(child: Expression) extends UnaryExpression with ImplicitCa | |
| override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { | ||
| nullSafeCodeGen(ctx, ev, time => { | ||
| val cal = classOf[Calendar].getName | ||
| val c = ctx.freshName("cal") | ||
| val dtu = DateTimeUtils.getClass.getName.stripSuffix("$") | ||
| ctx.addMutableState(cal, c, | ||
| s""" | ||
| $c = $cal.getInstance($dtu.getTimeZone("UTC")); | ||
| $c.setFirstDayOfWeek($cal.MONDAY); | ||
| $c.setMinimalDaysInFirstWeek(4); | ||
| val c = ctx.addMutableState(cal, "cal", | ||
| v => s""" | ||
| $v = $cal.getInstance($dtu.getTimeZone("UTC")); | ||
|
||
| $v.setFirstDayOfWeek($cal.MONDAY); | ||
| $v.setMinimalDaysInFirstWeek(4); | ||
| """) | ||
|
||
| s""" | ||
| $c.setTimeInMillis($time * 1000L * 3600L * 24L); | ||
|
|
@@ -1014,12 +1013,12 @@ case class FromUTCTimestamp(left: Expression, right: Expression) | |
| |long ${ev.value} = 0; | ||
| """.stripMargin) | ||
| } else { | ||
| val tzTerm = ctx.freshName("tz") | ||
| val utcTerm = ctx.freshName("utc") | ||
| val tzClass = classOf[TimeZone].getName | ||
| val dtu = DateTimeUtils.getClass.getName.stripSuffix("$") | ||
| ctx.addMutableState(tzClass, tzTerm, s"""$tzTerm = $dtu.getTimeZone("$tz");""") | ||
| ctx.addMutableState(tzClass, utcTerm, s"""$utcTerm = $dtu.getTimeZone("UTC");""") | ||
| val tzTerm = ctx.addMutableState(tzClass, "tz", | ||
| v => s"""$v = $dtu.getTimeZone("$tz");""") | ||
| val utcTerm = ctx.addMutableState(tzClass, "utc", | ||
| v => s"""$v = $dtu.getTimeZone("UTC");""") | ||
| val eval = left.genCode(ctx) | ||
| ev.copy(code = s""" | ||
| |${eval.code} | ||
|
|
@@ -1190,12 +1189,12 @@ case class ToUTCTimestamp(left: Expression, right: Expression) | |
| |long ${ev.value} = 0; | ||
| """.stripMargin) | ||
| } else { | ||
| val tzTerm = ctx.freshName("tz") | ||
| val utcTerm = ctx.freshName("utc") | ||
| val tzClass = classOf[TimeZone].getName | ||
| val dtu = DateTimeUtils.getClass.getName.stripSuffix("$") | ||
| ctx.addMutableState(tzClass, tzTerm, s"""$tzTerm = $dtu.getTimeZone("$tz");""") | ||
| ctx.addMutableState(tzClass, utcTerm, s"""$utcTerm = $dtu.getTimeZone("UTC");""") | ||
| val tzTerm = ctx.addMutableState(tzClass, "tz", | ||
| v => s"""$v = $dtu.getTimeZone("$tz");""") | ||
| val utcTerm = ctx.addMutableState(tzClass, "utc", | ||
| v => s"""$v = $dtu.getTimeZone("UTC");""") | ||
| val eval = left.genCode(ctx) | ||
| ev.copy(code = s""" | ||
| |${eval.code} | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -199,8 +199,8 @@ case class Stack(children: Seq[Expression]) extends Generator { | |
|
|
||
| override protected def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { | ||
| // Rows - we write these into an array. | ||
| val rowData = ctx.freshName("rows") | ||
| ctx.addMutableState("InternalRow[]", rowData, s"$rowData = new InternalRow[$numRows];") | ||
| val rowData = ctx.addMutableState("InternalRow[]", "rows", | ||
| v => s"$v = new InternalRow[$numRows];") | ||
| val values = children.tail | ||
| val dataTypes = values.take(numFields).map(_.dataType) | ||
| val code = ctx.splitExpressionsWithCurrentInputs(Seq.tabulate(numRows) { row => | ||
|
|
@@ -212,12 +212,12 @@ case class Stack(children: Seq[Expression]) extends Generator { | |
| s"${eval.code}\n$rowData[$row] = ${eval.value};" | ||
| }) | ||
|
|
||
| // Create the collection. | ||
| // Create the collection. Inline to outer class. | ||
|
||
| val wrapperClass = classOf[mutable.WrappedArray[_]].getName | ||
| ctx.addMutableState( | ||
| s"$wrapperClass<InternalRow>", | ||
| ev.value, | ||
| s"${ev.value} = $wrapperClass$$.MODULE$$.make($rowData);") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can localize the global variable
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will do it in another PR after merging this. |
||
| _ => s"${ev.value} = $wrapperClass$$.MODULE$$.make($rowData);", inline = true) | ||
| ev.copy(code = code, isNull = "false") | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
forceInline?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we have a clear rule when to force inline it?