From fa67f84b73fb8b554d9e45dbc3dd1afe7fba6ecf Mon Sep 17 00:00:00 2001 From: Felix Becker Date: Thu, 23 Nov 2017 02:22:26 -0800 Subject: [PATCH] perf: get direct children --- src/CompletionProvider.php | 18 ++++++++++-------- src/Index/AbstractAggregateIndex.php | 6 +++--- src/Index/Index.php | 20 ++++++++++++-------- src/Index/ReadableIndex.php | 4 ++-- 4 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/CompletionProvider.php b/src/CompletionProvider.php index 2f223334..18f00efe 100644 --- a/src/CompletionProvider.php +++ b/src/CompletionProvider.php @@ -220,11 +220,12 @@ public function provideCompletion(PhpDocument $doc, Position $pos, CompletionCon // The FQNs of the symbol and its parents (eg the implemented interfaces) foreach ($this->expandParentFqns($fqns) as $parentFqn) { + // Add the object access operator to only get members of all parents + $prefix = $parentFqn . '->'; + $prefixLen = strlen($prefix); // Collect fqn definitions - foreach ($this->index->getDescendantDefinitionsForFqn($parentFqn) as $fqn => $def) { - // Add the object access operator to only get members of all parents - $prefix = $parentFqn . '->'; - if (substr($fqn, 0, strlen($prefix)) === $prefix && $def->isMember) { + foreach ($this->index->getChildDefinitionsForFqn($parentFqn) as $fqn => $def) { + if (substr($fqn, 0, $prefixLen) === $prefix && $def->isMember) { $list->items[] = CompletionItem::fromDefinition($def); } } @@ -250,11 +251,12 @@ public function provideCompletion(PhpDocument $doc, Position $pos, CompletionCon // The FQNs of the symbol and its parents (eg the implemented interfaces) foreach ($this->expandParentFqns($fqns) as $parentFqn) { + // Append :: operator to only get static members of all parents + $prefix = strtolower($parentFqn . '::'); + $prefixLen = strlen($prefix); // Collect fqn definitions - foreach ($this->index->getDescendantDefinitionsForFqn($parentFqn) as $fqn => $def) { - // Append :: operator to only get static members of all parents - $prefix = strtolower($parentFqn . '::'); - if (substr(strtolower($fqn), 0, strlen($prefix)) === $prefix && $def->isMember) { + foreach ($this->index->getChildDefinitionsForFqn($parentFqn) as $fqn => $def) { + if (substr(strtolower($fqn), 0, $prefixLen) === $prefix && $def->isMember) { $list->items[] = CompletionItem::fromDefinition($def); } } diff --git a/src/Index/AbstractAggregateIndex.php b/src/Index/AbstractAggregateIndex.php index 90490ab2..8c8c95a1 100644 --- a/src/Index/AbstractAggregateIndex.php +++ b/src/Index/AbstractAggregateIndex.php @@ -112,15 +112,15 @@ public function getDefinitions(): \Generator } /** - * Returns a Generator that yields all the descendant Definitions of a given FQN + * Returns a Generator that yields all the direct child Definitions of a given FQN * * @param string $fqn * @return \Generator yields Definition */ - public function getDescendantDefinitionsForFqn(string $fqn): \Generator + public function getChildDefinitionsForFqn(string $fqn): \Generator { foreach ($this->getIndexes() as $index) { - yield from $index->getDescendantDefinitionsForFqn($fqn); + yield from $index->getChildDefinitionsForFqn($fqn); } } diff --git a/src/Index/Index.php b/src/Index/Index.php index dcb1b8f7..6f91e376 100644 --- a/src/Index/Index.php +++ b/src/Index/Index.php @@ -107,12 +107,12 @@ public function getDefinitions(): \Generator } /** - * Returns a Generator that yields all the descendant Definitions of a given FQN + * Returns a Generator that yields all the direct child Definitions of a given FQN * * @param string $fqn * @return \Generator yields Definition */ - public function getDescendantDefinitionsForFqn(string $fqn): \Generator + public function getChildDefinitionsForFqn(string $fqn): \Generator { $parts = $this->splitFqn($fqn); if ('' === end($parts)) { @@ -122,11 +122,15 @@ public function getDescendantDefinitionsForFqn(string $fqn): \Generator } $result = $this->getIndexValue($parts, $this->definitions); - - if ($result instanceof Definition) { - yield $fqn => $result; - } elseif (is_array($result)) { - yield from $this->yieldDefinitionsRecursively($result, $fqn); + if (!$result) { + return; + } + foreach ($result as $name => $item) { + // Don't yield the parent + if ($name === '') { + continue; + } + yield $fqn.$name => $item; } } @@ -374,7 +378,7 @@ private function getIndexValue(array $parts, array &$storage) $parts = array_slice($parts, 1); // we've reached the last provided part - if (0 === count($parts)) { + if (empty($parts)) { return $storage[$part]; } diff --git a/src/Index/ReadableIndex.php b/src/Index/ReadableIndex.php index 90ddcc45..505bb9a9 100644 --- a/src/Index/ReadableIndex.php +++ b/src/Index/ReadableIndex.php @@ -38,12 +38,12 @@ public function isStaticComplete(): bool; public function getDefinitions(): \Generator; /** - * Returns a Generator that yields all the descendant Definitions of a given FQN + * Returns a Generator that yields all the direct child Definitions of a given FQN * * @param string $fqn * @return \Generator yields Definition */ - public function getDescendantDefinitionsForFqn(string $fqn): \Generator; + public function getChildDefinitionsForFqn(string $fqn): \Generator; /** * Returns the Definition object by a specific FQN