laravel injection core have any effect in performance?
laravel injection core have any effect in performance?
I inject my repository into my controller through a serviceProvider.
question 1: What is the performance impact of injection through the reflection of laravel injection core? Is it different from creating a new instance object inside the controller?
What is the impact if my serviceProvider binds many classes through Laravel's singleton pattern?
question 2: In singleton pattern, is an object creates in every request to the server?
After a finished request, is the singleton object destroyed or cached and reused?
1 Answer
1
The general advice when it comes to performance, is to only optimise for it when it becomes a problem - and even then, only do so with performance metrics in hand.
As for the performance impact of using the Laravel injection container, it's going to depend on how you use it.
When relying on it's auto-wiring functionality, you may indeed see some performance loss, because the container needs to use Reflection to determine the type of parameters needed to construct the object.
It may be faster to manually bind the service, in which case only a lookup in a hash map is required.
As for question 2, php operates a bit in the middle of the two. Most php servers will make a cache with the bytecode, so that interpreting will go faster after the first time. However PHP is built in such a way that all objects are removed from memory after the request is finished.
This can cause some performance loss, but it is also a nice way to ensure there is no memory leakage and fits well in the HTTP/REST concepts of statelessness.
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
For each request the whole stack is ran again (if you let app error out you can see whoops message + whole stack trace, so every single time you hit a server this stack is ran over and over again). Its all about readability & testability not what is more performant. If you want to have cutting edge performance use C instead.
– Kyslik
Sep 9 '18 at 14:41