-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-22570][SQL] Avoid to create a lot of global variables by using a local variable with allocation of an object in generated code #19797
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 11 commits
41a591a
de5cd38
1046e1d
13e05b7
072cdd2
d4e4b07
36a330d
3b8459d
420b10d
cd4c696
a455ac3
747e215
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ import java.util.{Calendar, Locale, TimeZone} | |
| import org.apache.spark.SparkFunSuite | ||
| import org.apache.spark.sql.Row | ||
| import org.apache.spark.sql.catalyst.InternalRow | ||
| import org.apache.spark.sql.catalyst.expressions.codegen.CodegenContext | ||
| import org.apache.spark.sql.catalyst.util.DateTimeTestUtils._ | ||
| import org.apache.spark.sql.catalyst.util.DateTimeUtils | ||
| import org.apache.spark.sql.catalyst.util.DateTimeUtils.TimeZoneGMT | ||
|
|
@@ -845,4 +846,11 @@ class CastSuite extends SparkFunSuite with ExpressionEvalHelper { | |
| val outputOuter = Row.fromSeq((1 to N).map(_ => outputInner)) | ||
| checkEvaluation(cast(Literal.create(inputOuter, fromOuter), toOuter), outputOuter) | ||
| } | ||
|
|
||
| test("SPARK-22570: Cast should not create a lot of instance variables") { | ||
|
||
| val ctx = new CodegenContext | ||
| cast("1", IntegerType).genCode(ctx).code | ||
| cast("2", LongType).genCode(ctx).code | ||
|
||
| assert(ctx.mutableStates.length == 0) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ package org.apache.spark.sql.catalyst.expressions | |
| import org.apache.spark.SparkFunSuite | ||
| import org.apache.spark.sql.AnalysisException | ||
| import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
| import org.apache.spark.sql.catalyst.expressions.codegen.{CodeAndComment, CodegenContext, CodeGenerator} | ||
|
||
| import org.apache.spark.sql.types.{IntegerType, StringType} | ||
|
|
||
| /** | ||
|
|
@@ -178,6 +179,14 @@ class RegexpExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper { | |
| checkEvaluation(nonNullExpr, "num-num", row1) | ||
| } | ||
|
|
||
| test("SPARK-22570: RegExpReplace should not create a lot of global variables") { | ||
| val ctx = new CodegenContext | ||
| RegExpReplace(Literal("100"), Literal("(\\d+)"), Literal("num")).genCode(ctx) | ||
| // four global variables (lastRegex, pattern, lastReplacement, and lastReplacementInUTF8) | ||
| // are always required | ||
| assert(ctx.mutableStates.length == 4) | ||
| } | ||
|
|
||
| test("RegexExtract") { | ||
| val row1 = create_row("100-200", "(\\d+)-(\\d+)", 1) | ||
| val row2 = create_row("100-200", "(\\d+)-(\\d+)", 2) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ package org.apache.spark.sql.catalyst.optimizer | |
| import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
| import org.apache.spark.sql.catalyst.dsl.plans._ | ||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.expressions.codegen.{CodeAndComment, CodegenContext, CodeGenerator} | ||
|
||
| import org.apache.spark.sql.catalyst.plans.PlanTest | ||
| import org.apache.spark.sql.catalyst.plans.logical.{LocalRelation, LogicalPlan, Range} | ||
| import org.apache.spark.sql.catalyst.rules.RuleExecutor | ||
|
|
@@ -164,6 +165,12 @@ class ComplexTypesSuite extends PlanTest{ | |
| comparePlans(Optimizer execute query, expected) | ||
| } | ||
|
|
||
| test("SPARK-22570: CreateArray should not create a lot of global variables") { | ||
| val ctx = new CodegenContext | ||
|
||
| CreateArray(Seq(Literal(1))).genCode(ctx).code | ||
|
||
| assert(ctx.mutableStates.length == 0) | ||
| } | ||
|
|
||
| test("simplify map ops") { | ||
| val rel = relation | ||
| .select( | ||
|
|
||
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.
what if we create a new wrapper every time?
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.
It would work well, too. We may have some overhead to create a small object
IntWrapperand collect it at GC.Uh oh!
There was an error while loading. Please reload this page.
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.
shouldn't GC work very well in this case? My concern is, if there is no significant performance benefit, reusing global variables may be too hacky.
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.
I see. If we do not reuse global variables, we can use only a local variable.