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

SingleFileSourceLocator is too ineffective about unknown identifiers #614

Open
ondrejmirtes opened this issue May 28, 2020 · 2 comments
Open

Comments

@ondrejmirtes
Copy link
Contributor

When asked about an unknown identifier, it always parses the source code again.

Instead, it should parse the file only a single time and remember all the parsed symbols.

I know there's MemoizingSourceLocator but it doesn't help really. It will only remember null when asked about the same identifiers for the second time.

@ondrejmirtes
Copy link
Contributor Author

@Ocramius
Copy link
Member

Possibly related to how the AbstractSourceLocator does not cache the created "located source":

$locatedSource = $this->createLocatedSource($identifier);
if (! $locatedSource) {
return null;
}
try {
return $this->astLocator->findReflection($reflector, $locatedSource, $identifier);
} catch (IdentifierNotFound) {
return null;
}

/**
* {@inheritDoc}
*
* @throws ParseToAstFailure
*/
final public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): array
{
$locatedSource = $this->createLocatedSource(new Identifier(Identifier::WILDCARD, $identifierType));

Note how the entire source is re-created each time:

/**
* {@inheritDoc}
*
* @throws InvalidArgumentException
* @throws InvalidFileLocation
*/
protected function createLocatedSource(Identifier $identifier): ?LocatedSource
{
return new LocatedSource(
file_get_contents($this->fileName),
$identifier->getName(),
$this->fileName,
);
}

We can most likely avoid the expensive I/O operations ( file_get_contents() ), at the price of more memory usage.

Still, the AstLocator will then repeat the traversal of the whole tree of symbols:

public function findReflectionsOfType(
Reflector $reflector,
LocatedSource $locatedSource,
IdentifierType $identifierType,
): array {
try {
return $this->findReflectionsInTree->__invoke(
$reflector,
$this->parser->parse($locatedSource->getSource()),
$identifierType,
$locatedSource,
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants