Repository navigation
Fix quadratic parsing of PHPDocs with many generics - #326
Merged
Merged
Conversation
TypeParser::isHtml() looked for the end tag of every "<name>" by walking the rest of the tokens, so a PHPDoc with many generics like list<string> took quadratic time. The iterator now remembers once where each "/name>" is written, and the walk only runs when one is written ahead. Amp-Thread-ID: https://ampcode.com/threads/T-01a17820-94d1-75c8-9206-2e802d9c0556
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.
TypeParser::isHtml()runs for everyName<to tell a generic from an HTML tag such as<br>. Forlist<string>, the text<string>looks like a tag, so it walked all remaining tokens looking for</string>. That's one walk to the end of the PHPDoc per generic, so PHPDocs with many generics took quadratic time.TokenIteratornow records, once per PHPDoc, the last token containing each/name>.isHtml()returns early when no such text is written at or after the current token. Every end tag the walk looks for contains/name>, so the early return can't change a result. The walk itself is unchanged. The newTokenIterator::hasEndTagCandidateAhead()is@internal.Parsing a PHPDoc with this line repeated N times:
* @return array{a: int, b?: list<string>}|(callable(int): void)|nullTypical PHPDocs are unchanged within noise: a corpus of 5371 real-world PHPDocs takes 92–97 ms on both versions.
Verification:
<br>/</br>-style tags, end tags before and after the<, and broken tags.make checkpasses.