Repository navigation
fix: search walks hold far less, and count/find bounds match CPython - #274
Merged
Merged
Conversation
s.count('ab') on 100,000 characters peaked at 4.4 GB. A builtin's walk
runs inside one Python call, where no collect tick reaches, so every
object it makes is held until it returns -- and the walks were nested
`if` over null?, = and +, some 157 objects a character for a find and
141 for the slice copy before it.
The search walks (at?, find, rfind, count), drop, take, the bundle's
reverse and count, and pb-len are now `match` over eq? and the int
doors: 24 and 15 objects a character. A window that runs to the end is
the list's own tail, not a copy, and count takes the needle's length
once. The same probe now peaks at 1.0 GB; find with no match went from
3.5 GB to 1.3 GB.
find, count and their mirrors on str, bytes and bytearray share one
window, which also fixes the bounds CPython answers differently: an
empty needle finds nothing past the end or in an inverted window
('abc'.count('', 5) is 0, 'abc'.find('', 2, 1) and b'abc'.find(b'', 5)
are -1), rfind finds it at the very end ('abc'.rfind('') is 3), and the
needle is checked before the bounds ('abc'.find(5, 10) is a TypeError).
Co-Authored-By: Claude Opus 5.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.
s.count('ab')on a 100,000-character string peaked at 4.4 GB.Why: a builtin method's walk runs inside one Python call, and the runtime's collect ticks never fire inside it, so everything the walk allocates is held until it returns. The walks were nested
ifovernull?,=and+. Counted with(Heap count)on 10,000 elements, that was 157 objects per character for a find and 141 for the slice copy before it.Change:
%pb-at?, find, rfind, count),%pb-drop,%pb-take,python-rev-onto,%py-count-fromandpb-lenare nowmatchovereq?and the int doors: 24 and 15 objects per character.%pb-window).counttakes the needle's length once, not at every match.Peak footprint on 100,000 characters:
s.count('ab'), 50,000 matchess.count('x')s.find('x')len(s)CPython differences fixed: find, count and their mirrors on str, bytes and bytearray now share one window helper,
%py-span. That fixes the bounds where x-python answered differently from CPython 3.14:'abc'.count('', 5)and'abc'.count('', 2, 1)were 1; now 0.'abc'.find('', 2, 1)andb'abc'.find(b'', 5)were 2 and 3; now -1.'abc'.rfind('')was 2; now 3.'abc'.find(5, 10)returned -1; now it raises TypeError, because the needle is checked before the bounds.Specs: two cases added to
tests/specs/40-string-search.spec.md, with expected output from python3.14. One covers the bounds above, the other a 20,000-character string.Tests: suite 1293 passed / 0 failed; conformance 644 / 10 failed, the same 10 as main.
🤖 Generated with Claude Code