Github user JoshRosen commented on a diff in the pull request:
https://github.com/apache/spark/pull/13484#discussion_r65752894
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/trees/TreeNode.scala
---
@@ -169,11 +170,24 @@ abstract class TreeNode[BaseType <:
TreeNode[BaseType]] extends Product {
}
/**
+ * Efficient alternative to `productIterator.map(f).toArray`.
+ */
+ protected def mapProductIterator[B: ClassTag](f: Any => B): Array[B] = {
+ val arr = Array.ofDim[B](productArity)
+ var i = 0
+ while (i < arr.length) {
+ arr(i) = f(productElement(i))
--- End diff --
It turns out that ProductIterator was maintaining its own internal counter
and was calling `productElement(i)`:
```scala
/** An iterator over all the elements of this product.
* @return in the default implementation, an `Iterator[Any]`
*/
def productIterator: Iterator[Any] = new
scala.collection.AbstractIterator[Any] {
private var c: Int = 0
private val cmax = productArity
def hasNext = c < cmax
def next() = { val result = productElement(c); c += 1; result }
}
```
Calling `productElement` ourselves here avoids another layer of object
allocation and buys a bit of additional perf. boost.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]