8000 Revert "minor #19689 [DI] Cleanup array_key_exists (ro0NL)" by nicolas-grekas · Pull Request #19848 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Revert "minor #19689 [DI] Cleanup array_key_exists (ro0NL)" #19848

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

Merged
merged 2 commits into from
Sep 6, 2016
Merged
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
Next Next commit
Revert "minor #19689 [DI] Cleanup array_key_exists (ro0NL)"
This reverts commit c89f80a, reversing
changes made to 386e5e7.
  • Loading branch information
nicolas-grekas committed Sep 4, 2016
commit ac742dfc48fd45fb3307abb8f3b3d4f97941591c
5 changes: 3 additions & 2 deletions src/Symfony/Component/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ public function has($id)
if ('service_container' === $id
|| isset($this->aliases[$id])
|| isset($this->services[$id])
Copy link
Member
@dunglas dunglas Sep 5, 2016

Choose a reason for hiding this comment

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

IIUC, $this->services always exist and is always an array, so this line can be removed (the array_key_exists call in the next will do the check).

Copy link
Member Author
@nicolas-grekas nicolas-grekas Sep 5, 2016

Choose a reason for hiding this comment

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

yes but: isset is faster, and isset is what we'll keep on 3.x - and this is a revert, let's stop "optimizing" things that worked fine and had already been reviewed hundred of times.

Copy link
Member
@dunglas dunglas Sep 5, 2016

Choose a reason for hiding this comment

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

Ok to ease the merge between branches.
Regarding the perf, does it really worth to make the code more complex for such micro-optimizations?

Copy link
Member Author
@nicolas-grekas nicolas-grekas Sep 5, 2016

Choose a reason for hiding this comment

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

container->has/get() is full of micro-optimizations because it can be called thousands of time.
Which immediately unqualifies this as a "micro"-optimization and makes it a "macro" one.

|| array_key_exists($id, $this->services)
) {
return true;
}
Expand Down Expand Up @@ -265,7 +266,7 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
$id = $this->aliases[$id];
}
// Re-use shared service instance if it exists.
if (isset($this->services[$id])) {
if (isset($this->services[$id]) || array_key_exists($id, $this->services)) {
Copy link
Member

Choose a reason for hiding this comment

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

Same here, can't the isset call be removed?

Copy link
Member Author
@nicolas-grekas nicolas-grekas Sep 5, 2016

Choose a reason for hiding this comment

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

fast path first, slow path last. isset first, array_key_exists last

Copy link
Member

Choose a reason for hiding this comment

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

But 2 calls instead of 1 in the worst case, and a code harder to read and maintain.

Copy link
Member Author

Choose a reason for hiding this comment

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

when worst case means less likely case, it's on purpose

return $this->services[$id];
}

Expand Down Expand Up @@ -347,7 +348,7 @@ public function initialized($id)
$id = $this->aliases[$id];
}

return isset($this->services[$id]);
return isset($this->services[$id]) || array_key_exists($id, $this->services);
Copy link
Member

Choose a reason for hiding this comment

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

And here.

}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/DependencyInjection/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV
return $service;
}

if (!isset($this->definitions[$id]) && isset($this->aliasDefinitions[$id])) {
if (!array_key_exists($id, $this->definitions) && isset($this->aliasDefinitions[$id])) {
return $this->get($this->aliasDefinitions[$id]);
}

Expand Down Expand Up @@ -784,7 +784,7 @@ public function setDefinition($id, Definition $definition)
*/
public function hasDefinition($id)
{
return isset($this->definitions[strtolower($id)]);
return array_key_exists(strtolower($id), $this->definitions);
}

/**
Expand All @@ -800,7 +800,7 @@ public function getDefinition($id)
{
$id = strtolower($id);

if (!isset($this->definitions[$id])) {
if (!array_key_exists($id, $this->definitions)) {
throw new ServiceNotFoundException($id);
}

Expand Down
0