Compile recursively all references found during compilation#566
Compile recursively all references found during compilation#566
Conversation
…rmances This restores some kind of cache like there was in PHP-DI 4 and 5 (which was removed during v6's development). However this is not a full restore, this new cache is very simple and targeted at specific use cases. Related to #543 and #547. Compiling the container is the most efficient solution, but it has some limits. The following cases are not optimized: - autowired (or annotated) classes that are not declared in the configuration - wildcard definitions - usage of `Container::make()` or `Container::injectOn()` (because those are not using the compiled code) If you make heavy use of those features, and if it slows down your application you can enable the caching system. The cache will ensure annotations or the reflection is not read again on every request. The cache relies on APCu directly because it is the only cache system that makes sense (very fast to write and read). Other caches are not good options, this is why PHP-DI does not use PSR-6 or PSR-16. To enable the cache: ```php $containerBuilder = new \DI\ContainerBuilder(); if (/* is production */) { $containerBuilder->enableDefinitionCache(); } ``` To be clear: compilation should be used first. This cache is only for specific use cases.
Since the container is compiled we will store much less entries (definitions) in the cache. So it makes sense to separate each entry because there should not be a lot of entries. That will prevent cache stamped.
|
@mnapoli thanks for the quick turnaround!
|
I'll look into that (this was a quick POC so I didn't check in details yet), thanks for the feedback. For non-instantiable classes if you use Now if I understand you get exceptions at the compilation because of these? Maybe we could skip compiling those definitions? (without errors) |
I can't skip those. These are my entry point I list in the php-di config. If I skip those, I have nothing to compile. Or I will have to list all 1st-level dependencies in the config - which is much harder to maintain then the list of entry points. |
|
This PR is ready. @amakhrov why are you adding abstract classes to your config? I'm guessing since you mention the ZF-Bridge it's about controllers: you shouldn't add the abstract controller class in the config. Instead you should add all the controllers if you want their dependencies to be compiled. That way you will not get an error because of the I still believe the config should not contain an abstract class because it makes no sense, so the exception should stay IMO. |
|
@mnapoli I don't add abstract controllers to the config - I add my real controllers. So you see - the abstract controller has abstract dependencies. Now I add the real controller to the config. |
So if I understand correctly, currently ZF1 controllers "should" / can not be added to the PHP-DI configuration to prevent them from being compiled (which results in an error)? I also get errors with the recursive compilation approach for entries that are not part of my configuration (Entities): Is there any way to debug this / find out which configuration entry is causing this exactly? For now I disabled compilation, the re-added cache ensures performance remains at a good level. @mnapoli should an issue be opened for this? |
|
@holtkamp yes please. Could it be a dependency of one of your service? Because the only explanation I see is that this is caused by the recursive compilation. We could ignore errors in definitions during compilation, not sure if that's the best though 🤔 Let's discuss that in the new issue. |
Work in progress
Fixes #565
This pull request includes #564 to allow testing will all the performance improvements.
TODO: