[SPARK-49977][SQL] Use stack-based iterative computation to avoid creating many Scala List objects for deep expression trees #48481
+15
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
In some use cases with deep expression trees, the driver's heap shows many
scala.collection.immutable.$colon$colon
objects from the heap. The objects are allocated due to deep recursion in thegatherCommutative
method which usesflatmap
recursively. Each invocation offlatmap
creates a new temporary Scala collection. Our claim is based on the following stack trace (>1K lines) of a thread in the driver below, truncated here for brevity:This PR fixes the issue by using a stack-based iterative computation, completely avoiding the creation of temporary Scala objects.
Why are the changes needed?
Reduce heap usage of the driver
Does this PR introduce any user-facing change?
No
How was this patch tested?
Existing tests, refactor
Was this patch authored or co-authored using generative AI tooling?
No