Compiler Optimized Function
A compiler optimized function is one of a fixed list of native functions that the Zend compiler recognizes by name at compile time, and turns into a dedicated, specialized opcode sequence instead of a generic function call. Because these functions are called extremely often (count(), is_array(), in_array(), strlen()…), skipping the usual INIT_FCALL / SEND_VAL / DO_FCALL machinery removes call overhead and lets the VM execute a shorter, more direct set of instructions.
This optimization only applies when every condition is met:
- The call is a direct, literal call:
count($a), not a variable function call like$f($a)or'count'($a). - The function has not been redeclared or disabled with
disable_functions: if the resolved function is not the original internal one, the compiler falls back to a normal call, so a polyfill or override always takes effect. - Arguments are not spread with
...or named:count(...$args)andcount(value: $a)both bypass the optimization. - The
ZEND_COMPILE_NO_BUILTINScompiler flag is not set (this flag is used in a few contexts, such as someeval()internals, to force plain calls).
As of PHP 8.5, the functions eligible for this treatment are: strlen, is_null, is_bool, is_long/is_int/is_integer, is_float/is_double, is_string, is_array, is_object, is_resource, is_scalar, boolval, intval, floatval/doubleval, strval, defined, chr, ord, call_user_func_array, call_user_func, in_array, count/sizeof, get_class, get_called_class, gettype, func_num_args, func_get_args, array_slice, and array_key_exists. assert() also receives dedicated compile-time handling, though through a separate code path, so that it can be stripped entirely when assertions are disabled.
This list is an implementation detail of the Zend Engine: it changes between PHP versions as functions are added or removed from it, and it is not part of the language specification. It has no direct impact on the code semantics, only on the emitted opcodes and, therefore, on performance.
<?php
$array = [1, 2, 3];
// Compiled to a dedicated COUNT opcode: fast path.
echo count($array);
// Still a plain function call: the compiler cannot special-case
// an indirect / dynamic call.
$fn = 'count';
echo $fn($array);
// Also a plain function call: named arguments disable the shortcut.
echo count(value: $array);
// If count() is redefined (e.g. via a polyfill after disable_functions),
// every call to it, direct or not, uses the redefinition instead of
// the optimized opcode.
?>