Skip to content

Speed up in, first, last, and join on arrays - #5030

Open
Kocal wants to merge 1 commit into
twigphp:3.xfrom
Kocal:perf/core-filters-array-fast-paths
Open

Kocal wants to merge 1 commit into
twigphp:3.xfrom
Kocal:perf/core-filters-array-fast-paths

Conversation

@Kocal

@Kocal Kocal commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

in called CoreExtension::compare() for each element even when both sides are strings. first and last went through slice(), and join copied the array with array_values() before implode().

Two strings are now compared with ==, which is what compare() ends up doing for them. first and last call array_first() and array_last() on an array on PHP 8.5, and read the first or last key directly before that (the \PHP_VERSION_ID check is resolved at compile time), and join no longer copies an array before joining it, with or without an and separator.

in on 8 strings goes from 656-690 -> 243-317 ns, first from 167-169 -> 78-79 ns on PHP 8.4 and from 121-122 -> 53-54 ns on PHP 8.5, and join of 20 items from 316-319 -> 236-262 ns (433-440 -> 364-367 ns with an and separator). A 300-row page using the four goes from 0.864-0.894 -> 0.749-0.785 ms. New fixture cases (numeric strings, empty arrays) pass on 3.x too.

first / last on a 20-item array, time per call (best of 7 passes of 2,000,000 calls):

PHP 8.4 PHP 8.5
3.x 167 / 166 ns 122 / 123 ns
array_first() everywhere, with symfony/polyfill-php85 124 / 139 ns 52 / 52 ns
\PHP_VERSION_ID >= 80500 ? array_first($item) : $item[array_key_first($item)] (this PR) 78 / 79 ns 54 / 54 ns

The polyfill costs one more userland call before PHP 8.5, so this PR does not require it. The version check is free at runtime: the opcodes of first() on PHP 8.4 only contain the array_key_first() call.

join on a 20-item array, time per call on PHP 8.4 (measured in a separate, slower session):

join(', ') join(', ', ' and ')
3.x 316-319 ns 433-440 ns
This PR 236-262 ns 364-367 ns

Comment thread src/Extension/CoreExtension.php Outdated
public static function first(string $charset, $item)
{
if (\is_array($item)) {
return [] === $item ? false : $item[array_key_first($item)];

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Could we add https://github.com/symfony/polyfill-php85? I see that we already requires polyfills for mbstring and ctype, so I would say yes.

Updating the PR and benchmarks ⏳

@Kocal Kocal Nov 15, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hum, shouldn't we check for \PHP_VERSION_ID >= 80500 before calling array_first() and array_last, instead of using the polyfill? I updated the PR's description and the polyfill is slower than the previous $item[array_key_first($item)] 🤔

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

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.

Check polyfill dev-main for this

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Tested with symfony/polyfill-php85 at 94c794cb5b (1.x head, with symfony/polyfill#726), on PHP 8.4:

first last
3.x 170-173 ns 169-171 ns
polyfill 123-126 ns 139-142 ns
this PR 82-83 ns 82-85 ns

What remains is the two userland calls (bootstrap.php, then Php85::array_first()), I would kept the \PHP_VERSION_ID check in Twig

Comment thread tests/Fixtures/filters/first.test Resolved
@Kocal
Kocal force-pushed the perf/core-filters-array-fast-paths branch from dd3c755 to d7464ba Compare November 15, 2026 08:23
Comment thread src/Extension/CoreExtension.php Resolved
@Kocal
Kocal force-pushed the perf/core-filters-array-fast-paths branch from d7464ba to 9d1aca9 Compare November 15, 2026 08:31
Comment thread src/Extension/CoreExtension.php Outdated
Comment on lines +796 to +799
if (\is_array($item)) {
if ([] === $item) {
return false;
}

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.

Less nested if and an optimisation for the empty string.

Suggested change
if (\is_array($item)) {
if ([] === $item) {
return false;
}
if ([] === $item || '' === $item) {
return false;
}
if (\is_array($item)) {

@Kocal Kocal Nov 15, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

''|first outputs '' and not false in 3.x, this would be a BC break right?

However I'm moving the [] === $item check outside, thx

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Btw @fabpot, I tried to reproduce {{ ''|first }} on the Twig Playground, but I got the following error:

⚠️ An exception has been thrown during the rendering of a template ("Call to undefined function Twig\Extension\mb_substr()") in "index.twig" at line 1.

@Kocal
Kocal force-pushed the perf/core-filters-array-fast-paths branch from 9d1aca9 to 0ace475 Compare November 15, 2026 08:47
Comment thread src/Extension/CoreExtension.php Outdated

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.

This single change would do the same optimisation without duplicating some logic.

Suggested change
if (!\is_array($value)) {
$value = self::toArray($value, false);
}

}

// two strings always end up on the "<=>" fallback of compare(), where "0 === <=>" is "=="
$valueIsString = \is_string($value);

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.

\is_string($value) is already optimized by PHP, I'm not sure this is worth keeping the result in a variable.

`in` called `CoreExtension::compare()` for each element even when both sides are strings. `first` and `last` went through `slice()`, and `join` copied the array with `array_values()` before `implode()`.

Two strings are now compared with `==`, which is what `compare()` ends up doing for them. `first` and `last` call `array_first()` and `array_last()` on an array on PHP 8.5, and read the first or last key directly before that (the `\PHP_VERSION_ID` check is resolved at compile time), and `join` no longer copies an array before joining it, with or without an `and` separator.

`in` on 8 strings goes from 656-690 -> 243-317 ns, `first` from 167-169 -> 78-79 ns on PHP 8.4 and from 121-122 -> 53-54 ns on PHP 8.5, and `join` of 20 items from 316-319 -> 236-262 ns (433-440 -> 364-367 ns with an `and` separator). A 300-row page using the four goes from 0.864-0.894 -> 0.749-0.785 ms. New fixture cases (numeric strings, empty arrays) pass on 3.x too.
@Kocal
Kocal force-pushed the perf/core-filters-array-fast-paths branch from 0ace475 to 8f20336 Compare November 15, 2026 10:03
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