Skip to content

Speed up the number formatting filters of twig/intl-extra - #5037

Open
Kocal wants to merge 1 commit into
twigphp:3.xfrom
Kocal:perf/intl-number-formatter-prototype
Open

Kocal wants to merge 1 commit into
twigphp:3.xfrom
Kocal:perf/intl-number-formatter-prototype

Conversation

@Kocal

@Kocal Kocal commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

With a NumberFormatter prototype, every format_number call read 45 attributes, text attributes and symbols from the prototype, then set all 45 again on the formatter it had just taken from its cache.

The prototype is now read once, and a formatter is configured only when it is created (everything set is part of its cache key).

format_number with a prototype goes from 49.7-55.3 -> 4.0-4.3 µs, and without one from 842-1162 -> 742-773 ns. Caveat: a prototype changed after the extension was constructed is no longer read again.

With a `NumberFormatter` prototype, every `format_number` call read 45 attributes, text attributes and symbols from the prototype, then set all 45 again on the formatter it had just taken from its cache.

The prototype is now read once, and a formatter is configured only when it is created (everything set is part of its cache key).

`format_number` with a prototype goes from 49.7-55.3 -> 4.0-4.3 µs, and without one from 842-1162 -> 742-773 ns. Caveat: a prototype changed after the extension was constructed is no longer read again.

@nicolas-grekas nicolas-grekas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

another optim could be to make the caching props static, for worker mode

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