Skip to content

Speed up html_attr() for string values and repeated attribute names - #5038

Open
Kocal wants to merge 1 commit into
twigphp:3.xfrom
Kocal:perf/html-attr-string-values
Open

Kocal wants to merge 1 commit into
twigphp:3.xfrom
Kocal:perf/html-attr-string-values

Conversation

@Kocal

@Kocal Kocal commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

For each attribute, html_attr() passed the value through htmlAttrValue(), which returns a string unchanged, and escaped the attribute name again although names repeat (class, id).

String values now skip htmlAttrValue(), and escaped names are remembered per charset (at most 1,000 per charset).

4 string attributes go from 3521-3772 -> 1609-1947 ns, and 6 mixed attributes from 6223-8056 -> 4104-6152 ns. Caveat: the memo uses $env->getCharset(), so calling setCharset() after the escaper runtime was created could make them differ.

@Kocal

Kocal commented Nov 15, 2026

Copy link
Copy Markdown
Contributor Author

FYI we used the same "trick" to check for string values before calling for html_attr() in symfony/ux#4275, and we got nice results

Comment thread extra/html-extra/HtmlExtension.php Outdated

$result = '';
$runtime = $env->getRuntime(EscaperRuntime::class);
static $escapedNames = [];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would make it a static class property instead of a static variable to make it clearer that there is some global state involved here (the runtime behavior will be the same, the impact is only on readability)

@Kocal
Kocal force-pushed the perf/html-attr-string-values branch from 3ae9bb2 to 7eb9272 Compare November 17, 2026 13:48
Comment thread extra/html-extra/HtmlExtension.php Outdated

final class HtmlExtension extends AbstractExtension
{
private static array $escapedNames = [];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private static array $escapedNames = [];
/**
* @var array<string, array<string, string>>
*/
private static array $escapedNameCache = [];

This makes the intent clearer (and the type as well, for static analysis of the code, either by phpstan or by contributors)

For each attribute, `html_attr()` passed the value through `htmlAttrValue()`, which returns a string unchanged, and escaped the attribute name again although names repeat (`class`, `id`).

String values now skip `htmlAttrValue()`, and escaped names are remembered per charset (at most 1,000 per charset).

4 string attributes go from 3521-3772 -> 1609-1947 ns, and 6 mixed attributes from 6223-8056 -> 4104-6152 ns. Caveat: the memo uses `$env->getCharset()`, so calling `setCharset()` after the escaper runtime was created could make them differ.
@Kocal
Kocal force-pushed the perf/html-attr-string-values branch from 7eb9272 to 96a25b7 Compare November 25, 2026 12:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants