Hi list,
As previously planned, I'm opening today a new topic regarding PSR-11: the possibility of having more than one container running in an application, and the performance impact of it.
One of PSR-11's key point is allowing 2 PHP dependency injection containers to work side-by-side (and to share entries). This is done through a mechanism called « delegate dependency lookup ». The whole mechanism is
detailed in the META document. During
the acceptance vote of PSR-11, I encountered quite a lot of criticism regarding the idea of having 2 or more containers side-by-side. Most comments were in one of those 2 categories:
- You should not do this because
[insert philosophical reason here]- You should not do this because it will bad for performances
Regarding philosophical comment you might have, I will just say this: a composite container is wrapping many containers. The application needs to know only about one container: the composite container. Therefore, there is really only one container that is used by the application.
Now, regarding performance: I do care a lot about performance. The comment about performance makes sense. If the composite container must test if an entry is available in many containers instead of one, there has to be a performance impact. The question is: is it big enough to be noticeable? So I decided to run a test to see if I could detect any impact on performances from an application with multiple containers.
For this test, I decided to work on a Symfony 2 application. My idea was to test the default « demo » page that comes with Symfony when you install the framework (the one described in the
Quick tour document). I decided to test this page because it is quite close to a "real world use case", and it is not something that I could have modified to make the benchmark yield better results.
I modified Symfony 2 to make it PSR-11 compatible. This was
surprisingly simple to do, even for a complex compiled container like Symfony.
Then I added a composite container, and many other PSR-11 compatible containers. I used Blackfire to test the difference.
The result? I was
unable to measure any meaningful impact of running 21 containers together in Symfony demo page.
I wrote the details into
a blog post that can be found here: http://www.thecodingmachine.com/psr-11-performance-impact-of-the-delegate-lookup-feature/Both repositories used for this test are public on Github so that anyone can reproduce the tests.
I'm open to any suggestion or comment that would help improve these tests. Unless anyone objects or wants to run further tests, I would be willing to close this "performance" issue and move to the next topic.
Best regards,
David Négrier
http://mouf-php.comGithub: moufmouf
Twitter: @david_negrier