Yuming Wang created SPARK-62399:
-----------------------------------

             Summary: Avoid materializing the Thrift fetch result page as a 
Scala List in SparkExecuteStatementOperation
                 Key: SPARK-62399
                 URL: https://issues.apache.org/jira/browse/SPARK-62399
             Project: Spark
          Issue Type: Improvement
          Components: SQL
    Affects Versions: 4.5.0
            Reporter: Yuming Wang


```
Avoid materializing the Thrift fetch result page as a Scala List in 
SparkExecuteStatementOperation
```

**Component/s:** SQL
**Affects Version/s:** 3.5.x, 4.0.x (present on branch-3.5 / fork based on 
3.5.9; not yet checked against master — please verify before fixing)
**Priority:** Minor (pure constant-factor perf fix, no correctness impact; see 
profiling note below for production motivation)

**Description:**

```
h3. Problem

SparkExecuteStatementOperation.getNextRowSetInternal materializes each fetched
result page as a singly-linked Scala {{List}}:

{code:scala}
// SparkExecuteStatementOperation.scala
val rows = iter.take(maxRows).toList
...
RowSetUtils.toTRowSet(offset, rows, dataTypes, getProtocolVersion, 
getTimeFormatters)
{code}

RowSetUtils.toTRowSet's column-based path (toColumnBasedSet, used for any
protocol version >= V6, i.e. effectively all modern clients) calls
{{rows.length}} once up front plus once per column, inside toTColumn /
getOrSetAsNull:

{code:scala}
private def toColumnBasedSet(..., rows: Seq[Row], ...): TRowSet = {
  val rowSize = rows.length   // (1)
  ...
}
private def toTColumn(rows: Seq[Row], ...): TColumn = {
  ...
  val values = getOrSetAsNull[...](rows, ordinal, nulls, ...)  // calls 
rows.length again
}
private def getOrSetAsNull[T](rows: Seq[Row], ...): java.util.List[T] = {
  val size = rows.length   // (2) one of these per column
  ...
}
{code}

{{scala.collection.immutable.List.length}} is O(n). For an m-column result
page that is (m+1) separate full O(n) traversals of the row list on every
single fetchResults RPC, on top of the O(n*m) work the column build already
does.

Note: this is *not* a duplicate of SPARK-49193 (fixed in 3.4.4/3.5.3/4.0.0),
which replaced *indexed* access (rows(idx) inside a while loop, O(n) per
access => O(n^2) overall) with rows.foreach. That fix is already present in
this code path. The {{.length}} calls addressed here are a separate, still
unfixed cost on the same hot path.

This was found via CPU + lock profiling of a production HiveServer2 process
serving a large-result-set query: the HiveServer2-Handler-Pool thread was the
top CPU consumer (~24% of samples), and within it
{{scala.collection.LinearSeqOptimized.length}} was the single hottest leaf
frame.

h3. Fix

Change the single call site to build an Array instead of a List:

{code:scala}
val rows = iter.take(maxRows).toArray
{code}

{{Array[Row]}} implicitly converts to {{Seq[Row]}} (backed by an
IndexedSeq-like wrapper) via Predef, so it can be passed into
{{RowSetUtils.toTRowSet}} without any change to RowSetUtils.scala. Every
{{.length}} call on the downstream path becomes O(1). Pure performance fix;
no behavior or API change.

h3. Benchmark

Micro-benchmark calling {{RowSetUtils.toTRowSet}} directly with List- vs
Array-backed rows (protocol V11, 30 columns: mix of Int/Double/String):

||Rows||List best (ms)||Array best (ms)||Speedup||
|100|0|0|1.5x|
|1,000|0|0|2.3x|
|10,000|3|1|2.4x|
|100,000|986|198|5.0x|
|1,000,000|10,297|2,048|5.0x|

The gap holds (and grows) at realistic/large maxRows fetch sizes, matching
the production profiling signal.
```




--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to