Skip to content

Is it possible to make first class callables faster? #16759

@adataxtra

Description

@adataxtra

Description

First class callables feel like the right way to do callbacks since you get propper completion from from your IDE/editor but the performance hit is rather bad. I'm sadly not very familiar with C or PHP internals but would it be possible for opcache to get performance parity with the "string" version of the callbacks? Maybe not for userland functions but at least for internal functions.

<?php

$a = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];

$s = microtime(true);

for ($i = 0; $i < 100_000; $i++) {
    $u = array_map(strtoupper(...), $a);
}

echo (microtime(true) - $s) . PHP_EOL;

$s = microtime(true);

for ($i = 0; $i < 100_000; $i++) {
    $u = array_map('strtoupper', $a);
}

echo (microtime(true) - $s) . PHP_EOL;

$s = microtime(true);

for ($i = 0; $i < 100_000; $i++) {
    $u = array_map(fn($v) => strtoupper($v), $a);
}

echo (microtime(true) - $s) . PHP_EOL;

3v4l.org with PHP 8.3.13

0.061099052429199
0.026753902435303
0.043981075286865

3v4l.org with git.master

0.051828861236572
0.030702114105225
0.04832911491394

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions