Perf: split input once with mb_str_split in the main parse loop - #60
Merged
Conversation
The main loop called mb_substr($emails, $i, 1, $encoding) for every character, plus for each comment/quoted look-ahead. For multi-byte encodings mb_substr rescans from the start of the string to locate the Nth character — O(n) per call, O(n^2) across the loop. Splitting once with mb_str_split() and indexing the resulting array is a single O(n) pass. ~10-27% faster across the benchmark suite (composer bench:compare vs the v3.3.2 baseline), largest gains on longer inputs: batch 100 stream 13,415us -> 9,783us (-27%), name-addr -24%, obs-route -26%, comment extraction -25%. No behavioral change — all 91 tests pass unchanged.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #60 +/- ##
============================================
- Coverage 93.17% 93.07% -0.10%
Complexity 380 380
============================================
Files 6 6
Lines 981 982 +1
============================================
Hits 914 914
- Misses 67 68 +1
🚀 New features to boost your workflow:
|
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.
Realizes the ROADMAP main-loop performance item — and the new benchmark CI job (#59) measures it automatically.
Change
The main parse loop called
mb_substr($emails, $i, 1, $encoding)for every character, plus once per character for each comment/quoted look-ahead. For multi-byte encodingsmb_substrrescans from the start of the string to locate the Nth character — O(n) per call, O(n²) across the loop. This splits the input once withmb_str_split()and indexes the resulting array: a single O(n) pass.The diff is confined to the main loop — the three
mb_substr($emails, …)sites become$chars[$i]/$chars[$j].Results (
composer bench:comparevs v3.3.2 baseline, same host)Consistent 10–27% across the suite; biggest gains on longer inputs, as expected for an O(n²)→O(n) fix.
Safety
mb_str_split()is encoding-aware (same$encoding), so multi-byte handling is identical — verified by the existing UTF-8/ISO-8859-1/Shift-JIS tests.$emails/$lenare not mutated inside the loop, so pre-splitting is safe.One trade-off worth noting: the character array costs memory proportional to input length (bounded by input size). For very large single batches a chunked reader would cap that — noted as a ROADMAP follow-up.