chore: Use ArrayBuilder to build the array#564
Merged
stephenamar-db merged 1 commit intodatabricks:masterfrom Dec 9, 2025
Merged
chore: Use ArrayBuilder to build the array#564stephenamar-db merged 1 commit intodatabricks:masterfrom
stephenamar-db merged 1 commit intodatabricks:masterfrom
Conversation
He-Pin
commented
Dec 9, 2025
JoshRosen
reviewed
Dec 21, 2025
| for (v <- arrValue) { | ||
| if (out.isEmpty) { | ||
| out.append(v) | ||
| val outResult = out.result() |
Contributor
There was a problem hiding this comment.
I think that this change is introducing an O(n^2) bug because now we're making a fresh copy of the array on every loop iteration.
I've opened #575 with a suggested fix.
Contributor
Author
There was a problem hiding this comment.
Yes, we need just use the length to fix this.
stephenamar-db
pushed a commit
that referenced
this pull request
Dec 22, 2025
…ns (#575) This PR implements two performance optimizations in `uniqueArr`: - **Fix an O(n^2) performance bug**: #564 changed this code to call `out.result()` on every loop iteration, resulting in copying of an `O(n)` sized array on each loop iteration. We can avoid this by keeping a reference to the last output element. - **Avoid duplicate keyF evaluations**: in the old code, each loop iteration would repeat the `keyF` evaluation for the last element of the output array; the new code calls `keyF` exactly once per element by maintaining a `lastAddedKey` variable during the loop. - This is similar in spirit to #245 Claude (Opus 4.5) spotted the O(n^2) bug and designed that part of the fix. I spotted the duplicate keyF evaluation and suggested this fix (plus the switch to a `while` loop). Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The ArrayBuffer#toArray will do another copy.