DEV Community

marius-ciclistu
marius-ciclistu

Posted on • Originally published at marius-ciclistu.Medium on

Maravel Framework 10.63 avoids runtime reflection on DI


Maravel-Framework DI autoriring:cache

Maravel-Framework 10.63 improves the autowiring:cache command to include also constructors. Until this version, only the methods like boot, handle, __invoke and the controllers’ route methods were cached to not use reflection at runtime.

Additional directories and classes can be cached via config/app.php

/**
 * artisan autowiring:cache source paths for public methods (except __construct which is implicitly handled)
 * The CallQueuedHandler, controllers, middlewares, bult-in commands, service providers
 * + other classes resolved from Container during the autowiring:cache command execution are handled automatically
 * 'path' can be a single class or a directory
 */
'autowiring' => [
    [
        'path' => \app()->path() . DIRECTORY_SEPARATOR . 'Console' . DIRECTORY_SEPARATOR . 'Commands',
        'methods' => ['handle', '__invoke'],
    ],
    [
        'path' => \app()->path() . DIRECTORY_SEPARATOR . 'Jobs',
        'methods' => ['handle', '__invoke'],
    ],
    [
        'path' => \app()->path() . DIRECTORY_SEPARATOR . 'Http' . DIRECTORY_SEPARATOR . 'Requests',
        'methods' => ['validator', 'authorize', 'after', 'rules'],
    ],
Enter fullscreen mode Exit fullscreen mode

Benchmark for “Hello world” with optimizations (except Lumen):

This actual improvement is not visible in a single controller and route scenario but for complex projects where many classes are instantiated, this feature is a must.

Note:

Both Maravelith and Maravel templates can take advantage of this improvement.

If the concrete has contextual bindings (and constructor parameters) the old reflection is still used.

If the parameters are sent as array list , concrete will be instantiated directly with them. On failure, it will default to the old reflection but at the cost of building an Exception.

If the first parameters are sent as list and the last one(s) need to be auto-resolved, the above exception scenario will happen, which is slow. Always send all parameters as list, in the right order.

The method autowiring ( so non __construct ) does not support parameters as list!

Top comments (0)