perf: stop text extraction after finding the requested match
AF
2h ago+36+36+01234567890123456789-0-0-0123456789
Summary
extract_text_before() and extract_text_after() find the requested regex occurrence but continue enumerating every later match. For a delimiter near the start of a long document, the remaining scan does no useful work.
Stop the iterator as soon as the requested match is found. The returned slices, stripping behavior and out-of-range reporting remain unchanged. Regression tests use the real regex iterator and count consumed matches, including indexes beyond the first.
Validation
- This change's seven cases produce 6 failures and 1 passing control on the original source.
- A combined fork validation branch checks the same regressions against the pinned original production sources first: 11 failures, 2 passing controls. After restoring the final sources, the new cases and existing cleaner and NDJSON partition suites report 107 passed on Python 3.12.
- Ruff check and format check pass with the repository configuration. A source-integrity check verifies that baseline probing leaves the final files unchanged.
- Validation run, complete commands and JUnit artifacts.
- The same runner measures a 3,000,013-character input with 500,001 delimiters, selecting index 0 and disabling stripping. Median per-call timings over five repeats of five calls:
| Function | Original | Updated |
|---|---|---|
extract_text_before | 59.34 ms | 0.00142 ms |
extract_text_after | 59.45 ms | 0.1263 ms |
These figures describe that particular workload. Copying the returned suffix still costs time proportional to its length; the improvement removes unnecessary regex enumeration.
Checklist
- User-facing conventional-commit title.
- Maintainer: please apply the appropriate category label.