Skip to content

HybridParser now handles unbalanced closing tags in linear time - #131

Merged
thunderer merged 1 commit into
masterfrom
tk-hybrid-unbalanced
Nov 7, 2026
Merged

thunderer merged 1 commit into
masterfrom
tk-hybrid-unbalanced

Conversation

@thunderer

Copy link
Copy Markdown
Owner

HybridParser searched the whole stack of open tags for every closing tag, also when no open tag had that name, so text with many unmatched closing tags took quadratic time. 16000 opening tags followed by 16000 unmatched closing tags took 3.3s on PHP 8.4 and 21.9s on PHP 5.6. HybridParser is not in any tag yet.

  • HybridParser now counts open tags per name and skips the search when no open tag has the closing tag's name,
  • closed tags are removed from the stack by moving its end instead of copying the stack with array_slice(),
  • added a test that compares HybridParser with RegularParser on unbalanced tags,
  • verified the same HybridParser output as master on 30000 random texts with unbalanced tags for the default and [[ ]] syntaxes on PHP 5.5-8.5,
  • verified PHPUnit on PHP 5.5-8.5, Psalm on PHP 7.4-8.5, Mago and Infection.

Best of 3 runs for 16000 opening and 16000 unmatched closing tags: PHP 8.4 3330ms → 19ms, PHP 7.4 3711ms → 24ms, PHP 5.6 21.9s → 146ms. 5000 nested tags take 15.3ms → 3.9ms on PHP 8.4, and 1MB of mixed text takes the same time.

@thunderer
thunderer merged commit ae38166 into master Nov 7, 2026
16 checks passed
@thunderer
thunderer deleted the tk-hybrid-unbalanced branch November 7, 2026 21:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant