Skip to content

Commit 90ea648

Browse files
committed
Try to find first index of no_pad more intelligently.
1 parent a33b578 commit 90ea648

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

toolz/itertoolz.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -720,12 +720,22 @@ def partition_all(n, seq):
720720
yield prev
721721
prev = item
722722
if prev[-1] is no_pad:
723-
# Get first index of no_pad without using .index()
724-
# https://github.com/pytoolz/toolz/issues/387
725-
for ind, item in enumerate(reversed(prev)):
726-
if item is not no_pad:
727-
yield prev[:len(prev)-ind]
728-
break
723+
try:
724+
# If seq defines __len__, then we can quickly calculate where no_pad starts
725+
yield prev[:len(seq) % n]
726+
except TypeError:
727+
# Get first index of no_pad without using .index()
728+
# https://github.com/pytoolz/toolz/issues/387
729+
# We can employ modified binary search here to speed things up from O(n) to O(log n)
730+
# Binary search from CPython's bisect module, modified for identity testing.
731+
lo, hi = 0, len(prev)
732+
while lo < hi:
733+
mid = (lo + hi) // 2
734+
if prev[mid] is no_pad:
735+
hi = mid
736+
else:
737+
lo = mid + 1
738+
yield prev[:lo]
729739
else:
730740
yield prev
731741

0 commit comments

Comments
 (0)