Problem/Motivation

\Drupal\Component\DependencyInjection\Container::createService and \Drupal\Component\DependencyInjection\PhpArrayContainer::createService have a long block of code inthe getService method that was designed to improve performance by minimising the use of reflection when instantiating classes with varying amounts of arguments. This is now unnecessary as we're not having to cater to PHP 5.5

Proposed resolution

Use php's variable length argument lists to make this much simpler.

Remaining tasks

  1. Extend patch to cover both container classes
  2. Commit

User interface changes

None

API changes

None

Data model changes

None

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

msankhala created an issue. See original summary.

msankhala’s picture

Issue summary: View changes
Anonymous’s picture

Status: Active » Postponed

We can not do it until March 6, 2019 because #2842431: [policy] Remove PHP 5.5, 5.6 support in Drupal 8.7.

Anonymous’s picture

I'm not an advocate for PHP 5.5 supporting. And I have 0 friends who agree with this prolonged support.

But I must admit that I personally like these Christmas trees, which you propose to replace with soulless if/else construction :p

Although progress makes sense, of course. Thanks!

msankhala’s picture

It's a half Christmas tree though :) Trees are good as data structure but not as code branching. :P

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

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now 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.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.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.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

jonathanshaw’s picture

Status: Postponed » Active
Issue tags: +Novice

5.5. support is now dropped.

if ($length <= 10) {
  $service = new $class(...$arguments);
}
else {
  $r = new \ReflectionClass($class);
  $service = $r->newInstanceArgs($arguments);
}

If we can now use $arguments, then we can use it for more than 10 arguments and drop the whole reflection class thing?

So the code becomes

    else {
      $class = $this->frozen ? $definition['class'] : current($this->resolveServicesAndParameters([$definition['class']]));
      $service = new $class(...$arguments);
    }
govind.maloo’s picture

govind.maloo’s picture

Status: Active » Needs review
jonathanshaw’s picture

Issue summary: View changes
Status: Needs review » Needs work

Thank @govind.maloo!

The test fail is a random (#3103492: Random fail in WidgetUploadTest, so we can ignore that.

However, we need to patch the same problem in Container.php as well as PhpArrayContainer.php. Resetting to Needs Work for that.

jonathanshaw’s picture

Title: Code smells in Container::createService and PhpArrayContainer::createService » Remove PHP5.5 performance optimisations in Container::createService and PhpArrayContainer::createService
Issue summary: View changes
govind.maloo’s picture

Status: Needs work » Needs review
FileSize
5.25 KB
2.47 KB
pandaski’s picture

Status: Needs review » Reviewed & tested by the community

This is an awesome and clean solution to use "Variable-length argument lists"

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Crediting @msankhala for filing the issue. @jonathanshaw for coming up with the solution.

Committed and pushed 79938c6da5 to 9.0.x and b040e57c1b to 8.9.x. Thanks!

  • alexpott committed 79938c6 on 9.0.x
    Issue #2962978 by govind.maloo, msankhala, jonathanshaw: Remove PHP5.5...

  • alexpott committed b040e57 on 8.9.x
    Issue #2962978 by govind.maloo, msankhala, jonathanshaw: Remove PHP5.5...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.