-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Use PHP 8.4 lazy ghosts for Dependency injection #52538
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This breaks because we use stuff like this:
Here the ghost of |
Here the test expects a query to throw because a dependency is not found, but it will only throw if the object is used, not before. |
Maybe make it opt out then? I'm not sure how often this pattern occurs as I've never come across it so far. I think architecture wise it's already pretty bad that we load a class and depend on the code in the constructor to do something without using the object afterwards. Ideally the constructor stuff is moved to a separate method that is then called after Server::get(). |
Yeah, for me opt-out makes no sense either. Adding the opt-out flag or fixing the code to call a method is just as easy. But there may be other subtle side effect like this one here and there, so I suppose we’ll need to hide this under an opt-in config flag at the beginning and test it thouroughly before switching. Maybe it can be tested on our instance at some point. |
Yeah we should make sure to merge it early and test it for a long while and even then we might need to put it behind a config flag and enable it by default in 33 or later... |
I thought about this and I’m not sure it’s possible to put this behind a config flag: When the config is read, part of the DI services are already initiated and would not benefit from it. |
a669687
to
ab38e14
Compare
In the end I managed to put it behind a config flag. The flag is read early enough that it does not impact the performance boost. |
Note: because the config flag is opt-in, the CI will run the tests without the feature enabled, that’s a bit of a shame 🤔 |
We need to enable it in CI, otherwise we'd miss some things. We also need to do the same for very important and complex apps to check for any issues. |
Should I switch it to opt-out and enabled by default? |
Yes and if there are any concerns during RC phase we can still disable it. I just hope we find all problems that might be out there until then. |
237197e
to
278fdc3
Compare
Rebased and changed to enabled by default. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, just wondering about the option.
2b62b0a
to
d06cda4
Compare
Here is a comparison of running I would have expected a significant difference, because the occ commands all have a deep hierarchy of dependencies, and with the ghosts we should instantiate fewer classes. Did I miss something?
Is it something you can share here or privately? |
I did not test with occ. I think the issue here is that
Sending you. |
This will only work with PHP 8.4, so we’ll need to put it behind a version check later. Signed-off-by: Côme Chilliet <[email protected]>
Signed-off-by: Côme Chilliet <[email protected]>
Signed-off-by: Côme Chilliet <[email protected]>
Signed-off-by: Côme Chilliet <[email protected]>
Signed-off-by: Côme Chilliet <[email protected]>
Signed-off-by: Côme Chilliet <[email protected]>
Signed-off-by: Côme Chilliet <[email protected]>
Signed-off-by: Côme Chilliet <[email protected]>
d06cda4
to
78ff8e2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🐘
Summary
Use the new feature from PHP 8.4 Lazy objects for our Dependency injections system.
It’s not possible to make a ghost for an interface, so it’s only after aliases and so on are resolved that this happens, directly on the class.
It’s active for all classes. It should improve performances either for classes doing stuff in the constructor, or for classes loading a lot of dependencies. Thanks to the lazy ghosts only services which are actually used will be built.
This is only enabled for classes relying on the autobuild, so declarations like:
will not benefit from it.
They should be replaced by:
I’ve seen a measurable improvement of performances on some request with this, I would be interested in a more detailed analysis.
Checklist