Use zip_longest for more readable and faster code in the paired end iterator#21
Merged
Conversation
marcelm
reviewed
Dec 15, 2021
marcelm
reviewed
Dec 15, 2021
Owner
|
Good improvement, thanks once again! I think the comment at the top of |
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.
Sorry for the PR spam. I just suddenly find so much stuff to optimize in this library. And even more is coming, but that depends on still pending PRs #14 and #20. (I want to make writing faster still, by avoiding creating new bytes objects).
This is an easy one to review. I know you are busy!
Ideally we would want to use
zip(it1, it2, strict=True), but that is only available from python 3.10 and higher. (3.9 on my Debian 11 machine).Since try ... except has some expense I tried using
nextdifferently. It has a default argument. So you can usenext(it1, None)and it will yield None if the iterator is exhausted. This leads to a cleaner looking code when checking.I had a look at itertools if there was something in the standard library, and yes there was: zip_longest. Keeps on yielding until the longest iterator is exhausted and yields None for the shortest iterator.
The code looks cleaner now now all the exhaustion checking is in zip_longest instead of in try except loops. As an added bonus, itertools is fully implemented in C so it is faster as well:
before:
after
It is 2-3% faster. Not bad for such a small change.