Skip to content
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

[DI] Top micro benchmarks #26161

Merged
merged 1 commit into from Feb 13, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 11 additions & 14 deletions src/Symfony/Component/DependencyInjection/Container.php
Expand Up @@ -217,21 +217,18 @@ public function has($id)
*/
public function get($id, $invalidBehavior = /* self::EXCEPTION_ON_INVALID_REFERENCE */ 1)
{
if (isset($this->aliases[$id])) {
$id = $this->aliases[$id];
}

// Re-use shared service instance if it exists.
if (isset($this->services[$id])) {
return $this->services[$id];
}
if ('service_container' === $id) {
return $this;
}
if (isset($this->factories[$id])) {
return $this->factories[$id]();
}
return $this->services[$id]
?? $this->services[$id = $this->aliases[$id] ?? $id]
Copy link
Contributor

@dmaicher dmaicher Feb 13, 2018

Choose a reason for hiding this comment

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

isn't $this->services[$id = $this->aliases[$id] ?? $id] enough? Do we need to check $this->services[$id] before?

Edit: ah is this an optimization because we assume in the usual case a service is not aliased?

Copy link
Member Author

Choose a reason for hiding this comment

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

this an optimization because we assume in the usual case a service is not aliased?

correct!

?? ('service_container' === $id ? $this : ($this->factories[$id] ?? array($this, 'make'))($id, $invalidBehavior));
}

/**
* Creates a service.
*
* As a separate method to allow "get()" to use the really fast `??` operator.
*/
private function make(string $id, int $invalidBehavior)
Copy link
Member

Choose a reason for hiding this comment

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

Usually, private method goes after public ones

{
if (isset($this->loading[$id])) {
throw new ServiceCircularReferenceException($id, array_keys($this->loading));
}
Expand Down