Optimize parsers: O(n²) → O(n) offset calculation#121
Open
rhukster wants to merge 1 commit into
Open
Conversation
RegexParser and WordpressParser recomputed each match's character offset with mb_strlen(substr($text, 0, $match[1])), rescanning the whole prefix for every match, which is O(n^2) in the number of shortcodes. Accumulate the character offset incrementally instead, measuring only the new segment since the previous match. Matches come back in ascending offset order, so the running total stays exact. On a 504 KB document with 1,500 shortcodes: RegexParser 223.5 ms -> 3.4 ms (66x) WordpressParser 221.7 ms -> 1.0 ms (222x) Also: - replace the hand-rolled per-character backslash escaping with preg_quote() in RegularParser and RegexBuilderUtility, - drop a no-op array_filter() in RegexParser::parse() (parseSingle() never returns null), - iterate replacements with a reverse for-loop instead of allocating an array_reverse() copy in Processor, - short-circuit the Shortcode parameter-type check on the first invalid value instead of array_filter over all of them. No behavior change: the full test suite passes (288 tests, 2256 assertions).
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.


Summary
RegexParserandWordpressParsercomputed each match's character offset withmb_strlen(substr($text, 0, $match[1])), which rescans the entire prefix for every match. That makes parsing O(n²) in the number of shortcodes, and on large documents it dominates the parse time.This PR accumulates the character offset incrementally, measuring only the new segment of text since the previous match.
preg_match_all(and the WordPress loop) return matches in ascending offset order, so the running total stays exact. The result is O(n).Benchmark
504 KB document, 1,500 shortcodes:
RegexParserWordpressParserRegularParseris unaffected by the offset change (it tokenizes differently).Also included (small, behavior-preserving)
preg_replace('/(.)/us', '\\$0', ...)) withpreg_quote()inRegularParserandRegexBuilderUtility.array_filter()inRegexParser::parse();parseSingle()always returns aParsedShortcode, nevernull.forloop instead of allocating anarray_reverse()copy inProcessor.Shortcodeparameter-type validation on the first invalid value instead ofarray_filterover all of them, and cache a fewSyntaxaccessors inRegexParser.Tests
No behavior change. The full suite passes: 288 tests, 2256 assertions.