Performance of call_user_func_array. Why do libraries use it with only 5 arguments or more?

1

Let's go to the discussion: These days ago I took a look at the source code of a ZendFramework2 class. There was an implementation of the __call magic method. In this method there was something that caught my attention: The use of call_user_func_array was restricted only when the method attempted to call had more than 4 arguments. There was also a comment in English, which I forgot which it is, which explains why.

To confirm that this was not my fault (nor from ZendFramework), I went to look at the source code of Laravel4 and I came across the same thing (but without the comment specifying why).

See the snippet of code:

public static function __callStatic($method, $args)
    {
        $instance = static::getFacadeRoot();
        if (! $instance) {
            throw new RuntimeException('A facade root has not been set.');
        }
        switch (count($args)) {
            case 0:
                return $instance->$method();
            case 1:
                return $instance->$method($args[0]);
            case 2:
                return $instance->$method($args[0], $args[1]);
            case 3:
                return $instance->$method($args[0], $args[1], $args[2]);
            case 4:
                return $instance->$method($args[0], $args[1], $args[2], $args[3]);
            default:
                return call_user_func_array([$instance, $method], $args);
        }
    }

Note that Laravel only considers using call_user_func_array if the call of __callStatic has a passing of 5 or more arguments.

Interesting of this is that personally I would consider this useless typing of code, since call_user_func_array would make the call of a method or function much more dynamic, but two known libraries did just the way that seemed more complex.

So, I'd like to know why.

Is there a problem in performative loss (at a meaningful level) that would induce frameworks to do this?

Is there any hint of PHP itself that indicates this?

Note : Who wants to check the code, see here: link

    
asked by anonymous 12.12.2015 / 20:52

1 answer

1

Answering your question. Yes it is slower.

This question similar to yours in the gringo Stackoverflow has as one of the answers that the call is 15 times slower using call_user_func_array but does not put references to how it reached that number.

I kept searching for the answer until I found this post that actually does an analysis regarding the speed of direct or using this function.

He concludes that call_user_func is twice as slow and call_user_func_array is 10% -20% slower than call_user_func .

I did not find any official PHP information. However, remember that whoever is behind PHP and ZendFramework is the same organization as Zend, so if they use this code it is not typing useless.

    
13.12.2015 / 00:56