-
Notifications
You must be signed in to change notification settings - Fork 89
Fix alias resolution #221 #230
Changes from 3 commits
f0f2cd3
2413112
7bf7b8a
a359426
886b6f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -868,6 +868,12 @@ private function mapAliasToTarget($alias, $target) | |
* | ||
* This function maps $this->aliases in place. | ||
* | ||
* This algorithm is an adaptated version of Tarjans Strongly | ||
* Connected Components. Instead of returning the strongly | ||
* connected components (i.e. cycles in our case), we throw. | ||
* If nodes are not strongly connected (i.e. resolvable in | ||
* our case), they get resolved. | ||
* | ||
* This algorithm is fast for mass updates through configure(). | ||
* It is not appropriate if just a single alias is added. | ||
* | ||
|
@@ -881,17 +887,40 @@ private function mapAliasesToTargets() | |
if (isset($tagged[$alias])) { | ||
continue; | ||
} | ||
|
||
$tCursor = $this->aliases[$alias]; | ||
$aCursor = $alias; | ||
if ($aCursor === $tCursor) { | ||
throw CyclicAliasException::fromCyclicAlias($alias, $this->aliases); | ||
} | ||
if (! isset($this->aliases[$tCursor])) { | ||
continue; | ||
} | ||
|
||
while (isset($this->aliases[$tCursor])) { | ||
$tagged[$aCursor] = true; | ||
$this->aliases[$aCursor] = $this->aliases[$tCursor]; | ||
$stack[] = $aCursor; | ||
if ($aCursor === $this->aliases[$tCursor]) { | ||
throw CyclicAliasException::fromCyclicAlias($alias, $this->aliases); | ||
} | ||
$aCursor = $tCursor; | ||
$tCursor = $this->aliases[$tCursor]; | ||
if ($aCursor === $tCursor) { | ||
} | ||
|
||
$tagged[$aCursor] = true; | ||
|
||
if (! isset($stack)) { | ||
continue; | ||
} | ||
|
||
foreach ($stack as $_ => $alias) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we are not using indexes from the array we can skip them in the foreach ($stack as $alias) { There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. '$_ => $alias' is faster. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @weierophinney and @webimpress There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At least as of PHP 5.x foreach with explicit $key => $value is always faster. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As of 7.x it doesn't seem to make sifnificant difference. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not addressed by the coding standards, it seems. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are humans and we want / must read the code. This type of variable name says nothing and there is also no comment to explain the current usage. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
with new zend-coding-standard we will get an error:
Glad we got rid of this useless variable 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And what about The current usage of $_ is to indicate that this variable will not get accessed in the loop. Unused, yes. Useless, no. |
||
if ($alias === $tCursor) { | ||
throw CyclicAliasException::fromCyclicAlias($alias, $this->aliases); | ||
} | ||
$this->aliases[$alias] = $tCursor; | ||
$tagged[$alias] = true; | ||
} | ||
|
||
unset($stack); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would set empty array to
$stack
beforewhile
loop:$stack = [];
and then we can remove this condition and also
unset
in line 923.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unset is faster.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fhein so you'd like to say that
isset
+unset
is faster than setting empty array and foreach loop on empty array?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
empty($var)
is an equivalent for!isset($var) || $var == false
.isset
is slightly faster thanempty
.$stack = []
would be done on every loop, while unset is done only if $stack was used.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fhein not sure if you understand me correctly. I know what is
empty
and what is the difference betweenempty
andisset
. My suggestion was to have:instead of:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand you correctly. See me annotations to your sample.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fhein
no, can be removed at all, because then we will have
foreach
on an empty array, andunset
will be no needed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No performance impact following your suggestion on PHP 7.x. I'll do.