Watch how many times you access the DI, or how you used Shared DI Services.

I was noticing a small app slowing down for no real reason on AB tests...

I was loading a list of information like so:

// Retrieve Cache list of Modules and Namespaces
$modules = $di->getCache()->get("modules");
$namespaces = $di->getCache()->get("namespaces");
$events = $di->getCache()->get("events");

This was giving me about 500 Requests Per Second on average......

Grabbing the DI once

$memcached = $di->getCache();
// Retrieve Cache list of Modules and Namespaces
$modules = $memcached->get("modules");
$namespaces = $memcached->get("namespaces");
$events = $memcached->get("events");

This Bumped it up to 700 Requests Per Second...

Don't get carried away on accessing the DI directly, load it once if you can



183
edited Jul '18

That's obvious. I personally don't see any reason of why you would use the first way.

Well i have the $di being accessed throughout my system, this is an example.

Didn't think it would cause such a downward spiral in response time to access something that is supposed to be available anywhere anytime..... had to do clean up.



183

What I meant is since PHP isn't compiled I don't expect code optimization taking place. In a normal compiled language those getCache() calls might be optimized away in a single call but since we are using PHP there will be nothing of that and I'd rather think a bit ahead. If getCache() takes it's times to execute, you are adding extra time every time you call it.



57.2k

I don't think that they can be optimized away in a single call unless the function can be prohibited from having side effects and few popular languages have that ability. PHP certainly doesn't.