Spark-Sql源码解析之八 Codegen

本文深入探讨了Spark SQL中Codegen的作用,对比了动态字节码技术开启和关闭时Sort操作的性能差异。关闭时,Compare函数包含虚函数调用和装箱操作,消耗更多时间。而开启动态字节码技术后,性能得到显著提升。
摘要由CSDN通过智能技术生成
Codegen,动态字节码技术,那么什么是动态字节码技术呢?先看来一段代码,假设SparkPlanSort
case class Sort(
    sortOrder: Seq[SortOrder],
    global: Boolean,
    child: SparkPlan)
  extends UnaryNode {
  override def requiredChildDistribution: Seq[Distribution] =
    if (global) OrderedDistribution(sortOrder) :: Nil else UnspecifiedDistribution :: Nil

  protected override def doExecute(): RDD[Row] = attachTree(this, "sort") {
    child.execute().mapPartitions( { iterator =>
      val ordering = newOrdering(sortOrder, child.output)
      iterator.map(_.copy()).toArray.sorted(ordering).iterator
    }, preservesPartitioning = true)
  }

  override def output: Seq[Attribute] = child.output

  override def outputOrdering: Seq[SortOrder] = sortOrder
}

abstract class SparkPlan extends QueryPlan[SparkPlan] with Logging with Serializable {
protected def newOrdering(order: Seq[SortOrder], inputSchema: Seq[Attribute]): Ordering[Row] = {
  if (codegenEnabled) {//开启动态字节码技术
    GenerateOrdering.generate(order, inputSchema)
  } else {//否则关闭
    new RowOrdering(order, inputSchema)
  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值