Why are PHP function calls *so* expensive?

Function calls are expensive in PHP because there’s lot of stuff being done.

Note that isset is not a function (it has a special opcode for it), so it’s faster.

For a simple program like this:

<?php
func("arg1", "arg2");

There are six (four + one for each argument) opcodes:

1      INIT_FCALL_BY_NAME                                       'func', 'func'
2      EXT_FCALL_BEGIN                                          
3      SEND_VAL                                                 'arg1'
4      SEND_VAL                                                 'arg2'
5      DO_FCALL_BY_NAME                              2          
6      EXT_FCALL_END                                            

You can check the implementations of the opcodes in zend_vm_def.h. Prepend ZEND_ to the names, e.g. for ZEND_INIT_FCALL_BY_NAME and search.

ZEND_DO_FCALL_BY_NAME is particularly complicated. Then there’s the the implementation of the function itself, which must unwind the stack, check the types, convert the zvals and possibly separate them and to the actual work…

Leave a Comment