dingo / api

A RESTful API package for the Laravel and Lumen frameworks.
BSD 3-Clause "New" or "Revised" License
9.33k stars 1.25k forks source link

LaravelServiceProvider::register and LaraverServiceProvider::boot are too slow! #1593

Closed xiaohuilam closed 5 years ago

xiaohuilam commented 6 years ago
Q A
Bug? yes
New Feature? no
Framework Larave
Framework version 5.6.*#38d838bab9434af79e8ab274ae63f52f7ed45d6e
Package version ~v2.0.0-alpha2
PHP version 7.2.5

Actual Behaviour

Expected Behaviour

Register and boot takes more than 40 ms, was too expensive. need to optimize.

Steps to Reproduce

change vendor/laravel/framework/src/Illuminate/Foundation/Application.php 's register method:

    /**
     * Register a service provider with the application.
     *
     * @param  \Illuminate\Support\ServiceProvider|string  $provider
     * @param  array  $options
     * @param  bool   $force
     * @return \Illuminate\Support\ServiceProvider
     */
    public function register($provider, $options = [], $force = false)
    {
        ...
        $time1 = microtime(true) * 1000; // add this line
        $this->markAsRegistered($provider);
        $time2 = microtime(true) * 1000; // add this line
        dump(get_class($provider) . '::register ' . ($time2 - $time1)); // add this line
        ...
    }

and change the bootProvider method in the same file


    /**
     * Boot the given service provider.
     *
     * @param  \Illuminate\Support\ServiceProvider  $provider
     * @return mixed
     */
    protected function bootProvider(ServiceProvider $provider)
    {
        if (method_exists($provider, 'boot')) {
            $time1 = microtime(true) * 1000;
            $re = $this->call([$provider, 'boot']);
            $time2 = microtime(true) * 1000;
            dump(get_class($provider) . ': ' . ($time2 - $time1));
            return $re;
        }
    }

and my analysis:

LaraveServiceProvider's first line takes 15.1708984375 microseconds. https://github.com/dingo/api/blob/d46792602a838d399dc2c84642bb1cddde1a6183/src/Provider/LaravelServiceProvider.php#L24

LaraveServiceProvider's line 30~32 takes 9.296875 microseconds https://github.com/dingo/api/blob/d46792602a838d399dc2c84642bb1cddde1a6183/src/Provider/LaravelServiceProvider.php#L30-L32

specialtactics commented 6 years ago

I'll watch this space, but not entirely sure there is much to optimise there.

specialtactics commented 5 years ago

Going to close this, not really clear there's anything on this we can/should do.