perf: avoid copying chunked matches before sorting - #34
Merged
Conversation
cgfixit
marked this pull request as ready for review
September 6, 2026 15:34
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
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.
Proposed changes
ChunkedKeywordPattern.finditeralready owns a private list of all regex matches.It previously passed that list to
sorted, allocating a second list of references.Sort the existing list in place and yield it, retaining the same
(start, end)keyand stable pattern priority for ties.
Add one parametrized regression covering empty input, no matches, overlapping spans,
empty matches, and duplicate spans from different patterns. No match deduplication
or public behavior is changed.
Invariant/governance impact: lazy model loading, signatures, output/state formats,
thresholds, dependency declarations, and CI gates are unchanged.
Types of changes
Benefits / why
Windows Python 3.12.0rc3, tracemalloc around full iterator consumption, identical
compiled regex patterns/text for before and after:
Synthetic input repeats
term0 ... termN4,000 times, one compiled word-boundarypattern per chunk. Timing uses nine alternating before/after rounds of five calls:
2 chunks 10.37→11.53 ms, 8 chunks 106.28→87.81 ms. Timing is mixed/noisy; this PR claims
reduced allocation, not a throughput speedup. The before method comes from an archive
of the exact base commit. 300 deterministic generated cases also preserve identical
match spans, text, and pattern priority.
A further check on all 323,590 characters of
src/sample_input.txtwith 12 customkeywords split across two chunks produced identical 215 matches: peak allocation
76,798→74,895 B (2.5% lower), median 167.12→158.27 ms over three single-call rounds.
Keywords: ransomware, CVE, BERT, RAG, OSINT, malware, model, safety, alignment,
embedding, agent, privacy. The slower 363-keyword corpus timing run was interrupted;
no result is claimed for that configuration.
Risks to monitor
This is a small memory reduction. Matching still materializes all matches and sorts
them; it does not provide bounded-memory streaming. Normal single-pattern matching
does not use this adapter. The optional integration suite remains broken at its
existing AutoTokenizer mock boundary; errors were reproduced on clean origin/main.
Completed checklist
errors; after 122 passed / same 16 errors (121 unit + 1 integration success)
Further comments
Python 3.12 is the host's existing 3.12.0rc3; Python 3.13 is stable 3.13.12.
Only stemmer.py and its unit tests are committed; benchmark logs, baseline archive,
model-free smoke outputs, and virtual environments remain ignored local artifacts.
Branch naming / merge order / base commit
Branch:
codex/optimize-chunk-sort. Target:main.Base:
8b2087191122770449e704da19f0f2d47de16ae9(merged PR #33).Standalone draft PR; no unmerged dependencies.