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.

@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

@Kocal
Kocal force-pushed the perf/intl-number-formatter-prototype branch from 2acd540 to cd01fb4 Compare November 17, 2026 13:48

if ('rounding_mode' === $name) {
if (!isset(self::NUMBER_ROUNDING_ATTRIBUTES[$value])) {
throw new RuntimeError(\sprintf('The number formatter rounding mode "%s" does not exist, known modes are: "%s".', $value, implode('", "', array_keys(self::NUMBER_ROUNDING_ATTRIBUTES))));

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.

should we unset the cache for that hash for such errors ? If no, the cached formatter will still exist, but will have a config that does not match its cache key as applying the config would have been done only partially.
Previously this was fine as the next usage would reach the exception again, without skipping logic.

@stof

stof commented Nov 20, 2026

Copy link
Copy Markdown
Member

@nicolas-grekas this would be much harder, as the cache key would have to be tied to the config of the prototype formatter (while the whole point of this cache is to skip the introspection that would be needed to build the cache key). Using the object id as cache key would not help for worker mode as the prototype would be recreated as often as the extension in which it is injected.

fabpot added a commit that referenced this pull request Nov 23, 2026
…butes, or symbols (nicolas-grekas)

This PR was merged into the 3.x branch.

Discussion
----------

Don't cache Intl formatters built from long patterns, attributes, or symbols

The formatter caches can contain up to 100 entries, but ICU stores the pattern of each date formatter in memory that `memory_limit` doesn't account for. Formatters with a cache key longer than 4 KB are now created on each call instead of being cached. This applies to long patterns, attributes and symbols.

#5037 changes the same method, so the PR merged second will need a rebase.

Commits
-------

44a3c72 Don't cache Intl formatters built from long patterns, attributes, or symbols
With a `NumberFormatter` prototype, every `format_number` call read 45 attributes, text attributes and symbols from the prototype. It then set all 45 again on the formatter it had just taken from its cache.

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

`format_number` with a prototype goes from 51.9-64.4 -> 3.5-3.6 µs on PHP 8.4 and from 50.3-51.8 -> 3.1-5.0 µs on PHP 8.5, and without one from 1044-1103 -> 845-879 ns on PHP 8.4 and from 851-945 -> 697-745 ns on PHP 8.5. Caveat: a prototype changed after the extension was constructed is no longer read again.
@Kocal
Kocal force-pushed the perf/intl-number-formatter-prototype branch from cd01fb4 to 672c791 Compare November 25, 2026 12:12
@Kocal

Kocal commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

PR rebased and conflicts fixed

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.

3 participants