Perf: hoist loop-invariant option getters out of the main loop - #61
Merged
Conversation
getSeparators(), getBannedChars(), and getUseWhitespaceAsSeparator() were called inside the per-character loop (up to a few times per character across the hot states). They return immutable config that cannot change during a parse, so they are now fetched once into locals before the loop, removing a method call and its surrounding opcodes from every character iteration. ~19% faster on a mixed-input micro-benchmark (87.8us -> 71.0us/parse), on top of the mb_str_split change. No behavioral change — all 91 tests pass.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #61 +/- ##
============================================
- Coverage 93.17% 92.89% -0.29%
Complexity 380 380
============================================
Files 6 6
Lines 982 985 +3
============================================
Hits 915 915
- Misses 67 70 +3
🚀 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.
Profile-driven follow-up to the
mb_str_splitchange (#60). An Xdebug profile showed the remaining cost concentrated in the state-machine loop body, withParseOptionsgetters (getSeparators1.5%,getBannedChars,getUseWhitespaceAsSeparator) called inside the per-character loop.Change
Those three getters return immutable config that cannot change during a
parse()call, yet they were invoked on every character (a few times per character in the hot states —isset(getSeparators()[$curChar])etc.). Fetch them once into locals before the loop:This removes a method call and its surrounding opcodes from every character iteration — the gain is larger than the getters' own self-time because it also shrinks the enclosing
parse()loop-body opcode count.Result
~19% faster on a mixed-input micro-benchmark (87.8 → 71.0 μs/parse), on top of #60. The benchmark CI job on this PR measures head-vs-master per subject automatically.
Safety
ParseOptionssetter runs mid-parse(), so the hoisted values are loop-invariant.Context: diminishing returns
For transparency — the profile also confirmed the low-hanging fruit is now gone:
mb_str_splitis only ~0.3% of parse time; an ASCIIstr_splitfast-path measured net-negative (the ASCII-detection check costs more than it saves).idn_to_asciiis ~0.7% (already ASCII-fast-pathed), NFC only runs in intl modes.Remaining cost is spread across the state machine and per-address object construction. Further gains would need structural changes (e.g. reworking the result-object building) with more risk for less reward.