[SPARK-54384][PYTHON] Modernize the _batched method for BatchedSerializer #53086
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?
Use the modern itertools to do
_batchfunction inBatchedSerializerto make code cleaner and faster.The code is about 170% faster than the original implementation.
Result with the following code
Notice that
__getslice__is removed since Python 3.0, so the optimization for known size iterators like lists is not working at all. There's no simple way to know if an iterator supports slice operation now. The most straightforward way is to try it out likeiterator[:1]- I don't know how frequent we are dealing with lists, if the iterator is often lists, then we can do it. The raw[:]operation is 22% faster than this implementation.I like the simplicity without the
try ... except ...block.Why are the changes needed?
Most importantly, the code is less verbose. Also it's much faster.
Does this PR introduce any user-facing change?
No.
How was this patch tested?
The script above checks if the result is the same as before. Also we will have CI.
Was this patch authored or co-authored using generative AI tooling?
No