Problem/Motivation

There are situations when it is necessary to determine whether a given instance was retrieved from the container. #2190643: Serializing the container is a very very bad idea, let's prevent it? introduced a public _serviceId property which is assigned to a service whenever it was instantiated. Subsequently it was discovered that this comes with a considerable performance penalty in #2307869: Remove Drupal's Container::get().

In the meantime usage of the public _serviceId expanded (e.g. ConfigImporter::reinjectMe).

Note that ContainerAwareEventDispatcher::removeListener() (both the drupal as well as the symfony) resolves a similar issue by looping through the instantiated subscriber services declared in the container and comparing them to the $listener instance to be removed.

N.b. there have been discussions on pushing DependencySerializationTrait upstream to Symfony, but given its reliance on an undeclared public property and on Drupal::getContainer() this is currently impossible.

Proposed resolution

Stop adding an undeclared public _serviceId to track service names, but instead discover them by searching through the service container. This also makes it possible to remove the Container::get() overrides from the Drupal implementation of the container (performance++).

Implement the reverse service lookup as a service itself. That way it is easy to statically cache the results of previous lookups in a lookup table (e.g. SplObjectStorage) if this is necessary at all.

Remaining tasks

  1. Write the patch
  2. Profiling
  3. Review

User interface changes

None.

API changes

None.

Comments

znerol’s picture

Issue summary: View changes
znerol’s picture

Issue summary: View changes
znerol’s picture

Issue summary: View changes
znerol’s picture

Status: Active » Needs review
StatusFileSize
new11.5 KB

Status: Needs review » Needs work

The last submitted patch, 4: 2389193-add-reverse-service-resolver.diff, failed testing.

znerol’s picture

Status: Needs work » Needs review
StatusFileSize
new14.55 KB
new5.4 KB

Fix ConfigImporter::reInjectMe() and LocaleConfigTranslationTest.

Status: Needs review » Needs work

The last submitted patch, 6: 2389193-add-reverse-service-resolver-6.diff, failed testing.

znerol’s picture

Status: Needs work » Needs review
StatusFileSize
new12.72 KB

Kinda broken reroll, HEAD changed significantly in the meantime.

znerol’s picture

StatusFileSize
new16.64 KB
new3.92 KB

Remove remaining occurrences of the _serviceId property.

The last submitted patch, 8: provide_a_way_to-2389193-8.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 9: provide_a_way_to-2389193-9.patch, failed testing.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

mpdonadio’s picture

Can we use debug_zval_dump for some good, old-fashioned reference counting? Or would this be too expensive?

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

andypost’s picture

Version: 9.4.x-dev » 9.5.x-dev
Status: Needs work » Needs review
StatusFileSize
new18.7 KB

quick re-roll

- added phpunit hunk and core doing it now
- entity storages removed
- I bet tests will fail

btw it looks more like duplicate of #2531564: Fix leaky and brittle container serialization solution

andypost’s picture

Status: Needs review » Needs work

to fix tests and clean-up remaining _serviceId usage

andypost’s picture

StatusFileSize
new649 bytes
new18.7 KB

fix cs

geek-merlin’s picture

I much like the direction this is going. One thing though:

+++ b/core/lib/Drupal/Core/DependencyInjection/ReverseServiceResolver.php
@@ -0,0 +1,44 @@
+  public function lookup($service) {
+    foreach ($this->container->getServiceIds() as $service_id) {
+      if ($this->container->initialized($service_id)) {
+        if ($this->container->get($service_id) === $service) {
+          return $service_id;
+        }
+      }
+    }
+  }
+

Unlike the current hackish solution, this will not work for "shared=false" services, that get an instance every time the container is asked for them. We may decide to drop DependencySerializationTrait support for non-shared services, but for BC reasons we have to deprecate first and document the new limitaiton.

ravi.shankar’s picture

StatusFileSize
new18.68 KB
new3.55 KB

Added reroll of patch #26 on Drupal 9.5.x.

anchal_gupta’s picture

StatusFileSize
new18.68 KB

Rerolled patch against 9.5x

Ankit.Gupta’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new16.54 KB

Reroll the patch #28 with Drupal 9.5.x

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

znerol’s picture

Status: Needs review » Closed (duplicate)
Related issues: +#3327856: Performance regression introduced by container serialization solution

Closing this, since #3327856: Performance regression introduced by container serialization solution implements the exact same approach.