[TwigComponent] Reduce per-character work in the pre-lexer scan loops - #3776
Open
Kocal wants to merge 1 commit into
Open
[TwigComponent] Reduce per-character work in the pre-lexer scan loops#3776Kocal wants to merge 1 commit into
Kocal wants to merge 1 commit into
Conversation
| Q | A
| -------------- | ---
| Bug fix? | no
| New feature? | no
| Deprecations? | no
| Documentation? | no
| Issues | -
| License | MIT
`consumeUntilEndBlock()` ran six `substr()` comparisons on every character
of a block body, and the main loop ran a `preg_match()` per character to
test for whitespace.
Guard the delimiter comparisons behind the only three characters that can
start one (`{`, `<`, `#`), and swap the regex for `ctype_space()`.
Pre-lexing a 167 KB template made of components with blocks goes from
~64 ms to ~16 ms.
Benchmarked from the repository root with `blackfire run symfony php bench.php`:
```php
<?php
require __DIR__.'/src/TwigComponent/vendor/autoload.php';
use Symfony\UX\TwigComponent\Twig\TwigPreLexer;
$body = str_repeat(" <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>\n", 40);
$chunk = "<twig:Card>\n {% block header %}\n{$body}\n {% endblock %}\n</twig:Card>\n\n";
$input = str_repeat($chunk, 60); // ~167 KB
(new TwigPreLexer())->preLexComponents($input);
```
Blackfire:
- before — 1.06s wall / 1.05s CPU: https://blackfire.io/profiles/d4ce5972-9eb7-4796-beac-5f1f5921b5db/graph
- after — 72.1ms wall / 71.2ms CPU: https://blackfire.io/profiles/fe19e6f8-5d57-4d42-b270-6e741c87af20/graph
- diff: https://app.blackfire.io/profiles/compare/d4ce5972-9eb7-4796-beac-5f1f5921b5db...fe19e6f8-5d57-4d42-b270-6e741c87af20/graph
Analysis, implementation and benchmarks by Claude Opus 5.
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.
consumeUntilEndBlock()ran sixsubstr()comparisons on every characterof a block body, and the main loop ran a
preg_match()per character totest for whitespace.
Guard the delimiter comparisons behind the only three characters that can
start one (
{,<,#), and swap the regex forctype_space().Pre-lexing a 167 KB template made of components with blocks goes from
~64 ms to ~16 ms.
Benchmarked from the repository root with
blackfire run symfony php bench.php:Blackfire:
Analysis, implementation and benchmarks by Claude Opus 5.