8000 [DependencyInjection] Improve performance of the generated code. by realityking · Pull Request #9432 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DependencyInjection] Improve performance of the generated code. #9432

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
[DependencyInjection] Remove some calls to call_user_func in generate…
…d containers.
  • Loading branch information
realityking committed Dec 16, 2013
commit d8ddb1f978817ba13e84c0c51d54b4023a3f63b7
16 changes: 14 additions & 2 deletions src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,13 @@ private function addServiceConfigurator($id, $definition, $variableName = 'insta
}
}

return sprintf(" call_user_func(array(%s, '%s'), \$%s);\n", $this->dumpValue($callable[0]), $callable[1], $variableName);
$class = $this->dumpValue($callable[0]);
// If the class is a string we can optimize call_user_func away
if (strpos($class, "'") === 0) {
return sprintf(" %s::%s(\$%s);\n", substr($class, 1, -1), $callable[1], $variableName);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks wrong to me because dumping the value will escape \ in the string, which will produce broken code

}

return sprintf(" call_user_func(array(%s, '%s'), \$%s);\n", $class, $callable[1], $variableName);
}

return sprintf(" %s(\$%s);\n", $callable, $variableName);
Expand Down Expand Up @@ -698,7 +704,13 @@ private function addNewInstance($id, Definition $definition, $return, $instantia

if (null !== $definition->getFactoryMethod()) {
if (null !== $definition->getFactoryClass()) {
return sprintf(" $return{$instantiation}call_user_func(array(%s, '%s')%s);\n", $this->dumpValue($definition->getFactoryClass()), $definition->getFactoryMethod(), $arguments ? ', '.implode(', ', $arguments) : '');
$class = $this->dumpValue($definition->getFactoryClass());

// If the class is a string we can optimize call_user_func away
if (strpos($class, "'") === 0) {
return sprintf(" $return{$instantiation}%s::%s(%s);\n", substr($class, 1, -1), $definition->getFactoryMethod(), $arguments ? implode(', ', $arguments) : '');
}
return sprintf(" $return{$instantiation}call_user_func(array(%s, '%s')%s);\n", $class, $definition->getFactoryMethod(), $arguments ? ', '.implode(', ', $arguments) : '');
}

if (null !== $definition->getFactoryService()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ protected function getFooService()
{
$a = $this->get('foo.baz');

$this->services['foo'] = $instance = call_user_func(array('FooClass', 'getInstance'), 'foo', $a, array($this->getParameter('foo') => 'foo is '.$this->getParameter('foo').'', 'foobar' => $this->getParameter('foo')), true, $this);
$this->services['foo'] = $instance = FooClass::getInstance('foo', $a, array($this->getParameter('foo') => 'foo is '.$this->getParameter('foo').'', 'foobar' => $this->getParameter('foo')), true, $this);

$instance->setBar($this->get('bar'));
$instance->initialize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ protected function getFooService()
{
$a = $this->get('foo.baz');

$this->services['foo'] = $instance = call_user_func(array('FooClass', 'getInstance'), 'foo', $a, array('bar' => 'foo is bar', 'foobar' => 'bar'), true, $this);
$this->services['foo'] = $instance = FooClass::getInstance('foo', $a, array('bar' => 'foo is bar', 'foobar' => 'bar'), true, $this);

$instance->setBar($this->get('bar'));
$instance->initialize();
Expand All @@ -169,9 +169,9 @@ protected function getFooService()
*/
protected function getFoo_BazService()
{
$this->services['foo.baz'] = $instance = call_user_func(array('BazClass', 'getInstance'));
$this->services['foo.baz'] = $instance = BazClass::getInstance();

call_user_func(array('BazClass', 'configureStatic1'), $instance);
BazClass::configureStatic1($instance);

return $instance;
}
Expand Down
0