Skip to content

fix: search walks hold far less, and count/find bounds match CPython - #274

Merged
jonruttan merged 1 commit into
mainfrom
fix/str-count-cost
Nov 28, 2026
Merged

jonruttan merged 1 commit into
mainfrom
fix/str-count-cost

Conversation

@jonruttan

Copy link
Copy Markdown
Owner

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 if over null?, = 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:

  • The search walks (%pb-at?, find, rfind, count), %pb-drop, %pb-take, python-rev-onto, %py-count-from and pb-len are now match over eq? and the int doors: 24 and 15 objects per character.
  • A window that runs to the end of the string is its own tail, not a copy (%pb-window).
  • count takes the needle's length once, not at every match.

Peak footprint on 100,000 characters:

probe before after
s.count('ab'), 50,000 matches 4430 MB 1009 MB
s.count('x') 2977 MB 713 MB
s.find('x') 3496 MB 1308 MB
len(s) 1165 MB 599 MB

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) and b'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

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>
@jonruttan
jonruttan merged commit ac0929d into main Nov 28, 2026
12 checks passed
@jonruttan
jonruttan deleted the fix/str-count-cost branch November 28, 2026 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant