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.

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

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.

1 participant