From 8801edb7a2e4f3bf9bcd9c6444ddbead43b0fbeb Mon Sep 17 00:00:00 2001 From: Nicolas MURE Date: Thu, 10 Aug 2017 00:06:53 +0200 Subject: [PATCH] also yield URIs to save memory --- src/Index/AbstractAggregateIndex.php | 12 +++++------- src/Index/Index.php | 15 +++++++++++---- src/Index/ReadableIndex.php | 6 +++--- src/Server/TextDocument.php | 20 ++++++++++++++++---- 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/src/Index/AbstractAggregateIndex.php b/src/Index/AbstractAggregateIndex.php index a5826ab..7ba3870 100644 --- a/src/Index/AbstractAggregateIndex.php +++ b/src/Index/AbstractAggregateIndex.php @@ -160,19 +160,17 @@ abstract class AbstractAggregateIndex implements ReadableIndex } /** - * Returns all URIs in this index that reference a symbol + * Returns a Generator providing all URIs in this index that reference a symbol * * @param string $fqn The fully qualified name of the symbol - * @return string[] + * @return \Generator providing string[] */ - public function getReferenceUris(string $fqn): array + public function getReferenceUris(string $fqn): \Generator { - $refs = []; foreach ($this->getIndexes() as $index) { - foreach ($index->getReferenceUris($fqn) as $ref) { - $refs[] = $ref; + foreach ($index->getReferenceUris($fqn) as $uri) { + yield $uri; } } - return $refs; } } diff --git a/src/Index/Index.php b/src/Index/Index.php index 9130246..8d8675d 100644 --- a/src/Index/Index.php +++ b/src/Index/Index.php @@ -197,14 +197,21 @@ class Index implements ReadableIndex, \Serializable } /** - * Returns all URIs in this index that reference a symbol + * Returns a Generator providing all URIs in this index that reference a symbol * * @param string $fqn The fully qualified name of the symbol - * @return string[] + * @return \Generator providing string[] */ - public function getReferenceUris(string $fqn): array + public function getReferenceUris(string $fqn): \Generator { - return $this->references[$fqn] ?? []; + $uris = isset($this->references[$fqn]) + ? $this->references[$fqn] + : [] + ; + + foreach ($uris as $uri) { + yield $uri; + } } /** diff --git a/src/Index/ReadableIndex.php b/src/Index/ReadableIndex.php index 8d4e1ca..6e2b8c6 100644 --- a/src/Index/ReadableIndex.php +++ b/src/Index/ReadableIndex.php @@ -63,10 +63,10 @@ interface ReadableIndex extends EmitterInterface public function getDefinition(string $fqn, bool $globalFallback = false); /** - * Returns all URIs in this index that reference a symbol + * Returns a Generator providing all URIs in this index that reference a symbol * * @param string $fqn The fully qualified name of the symbol - * @return string[] + * @return \Generator providing string[] */ - public function getReferenceUris(string $fqn): array; + public function getReferenceUris(string $fqn): \Generator; } diff --git a/src/Server/TextDocument.php b/src/Server/TextDocument.php index 58e7074..46e822a 100644 --- a/src/Server/TextDocument.php +++ b/src/Server/TextDocument.php @@ -219,10 +219,9 @@ class TextDocument return []; } } - $refDocuments = yield Promise\all(array_map( - [$this->documentLoader, 'getOrLoad'], - $this->index->getReferenceUris($fqn) - )); + $refDocuments = yield Promise\all(iterator_to_array( + $this->getOrLoadReferences($fqn)) + ); foreach ($refDocuments as $document) { $refs = $document->getReferenceNodesByFqn($fqn); if ($refs !== null) { @@ -397,4 +396,17 @@ class TextDocument return [new SymbolLocationInformation($descriptor, $def->symbolInformation->location)]; }); } + + /** + * Gets or loads the documents referencing the given FQN. + * + * @param string $fqn + * @return \Generator providing Promise + */ + private function getOrLoadReferences(string $fqn): \Generator + { + foreach ($this->index->getReferenceUris($fqn) as $ref) { + yield $this->documentLoader->getOrLoad($ref); + } + } }