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

#135 optimise multiple class load attempts in AnnotationRegistry #137

Merged
merged 13 commits into from Jul 22, 2017

Conversation

Ocramius
Copy link
Member

This PR includes #135 as well as a number of fixes around the AnnotationRegistry class (which is mostly a horror show from the pre-composer era).

@Ocramius Ocramius added this to the v1.5.0 milestone Jul 22, 2017
@Ocramius Ocramius self-assigned this Jul 22, 2017
@Ocramius Ocramius changed the title Fix/#135 optimise multiple class load attempts #135 optimise multiple class load attempts in AnnotationRegistry Jul 22, 2017
@Ocramius Ocramius requested a review from lcobucci July 22, 2017 10:25
@Ocramius Ocramius merged commit 06d8979 into master Jul 22, 2017
@Ocramius
Copy link
Member Author

Urgh, premature merge due to CLI mistake on my side. @lcobucci still, if you can, give it a review...

@Ocramius Ocramius deleted the fix/#135-optimise-multiple-class-load-attempts branch July 22, 2017 10:38
* @return void
* An array of classes which cannot be found
*
* @var null[] indexed by class name
Copy link
Member

Choose a reason for hiding this comment

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

null[]?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, that's intentional, as a boolean can misrepresent two states, while here we just want to represent whether a key exists or not

$file = str_replace("\\", DIRECTORY_SEPARATOR, $class) . ".php";
if (\strpos($class, $namespace) === 0) {
$file = \str_replace('\\', \DIRECTORY_SEPARATOR, $class) . '.php';

if ($dirs === null) {
if ($path = stream_resolve_include_path($file)) {
require $path;
return true;
}
} else {
foreach((array)$dirs AS $dir) {
Copy link
Member

Choose a reason for hiding this comment

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

Add a space after the casting ((array) $dirs)

return true;
}
}

self::$failedToAutoload[$class] = null;
Copy link
Member

Choose a reason for hiding this comment

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

Any reason for setting the index instead pushing a new item to the array? It would never be duplicated anyway (since the check is done at the top).

Of course this is a micro optimisation but it might be worth it (also an array with only null values is a bit weird IMO)

Copy link
Member Author

Choose a reason for hiding this comment

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

The lookup can happen a lot of times (thousands according to the original report), so I'd rather keep it indexed

AnnotationRegistry::loadAnnotationClass('unloadableClass');
AnnotationRegistry::loadAnnotationClass('unloadableClass');
AnnotationRegistry::loadAnnotationClass('unloadableClass');
$this->assertEquals(1, $i, 'Autoloader should only be called once');
Copy link
Member

Choose a reason for hiding this comment

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

assertSame()

$i = 0;
$autoLoader = function($annotation) use (&$i, $className) {
eval('class ' . $className . ' {}');
$i++;
Copy link
Member

Choose a reason for hiding this comment

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

++$i

AnnotationRegistry::loadAnnotationClass($className);
AnnotationRegistry::loadAnnotationClass($className);
AnnotationRegistry::loadAnnotationClass($className);
$this->assertEquals(1, $i, 'Autoloader should only be called once');
Copy link
Member

Choose a reason for hiding this comment

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

assertSame

{
$failures = 0;
$failingLoader = function (string $annotation) use (& $failures) : bool {
$failures += 1;
Copy link
Member

Choose a reason for hiding this comment

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

++$failures (to be consistent 😜)

{
$failures = 0;
$failingLoader = function (string $annotation) use (& $failures) : bool {
$failures += 1;
Copy link
Member

Choose a reason for hiding this comment

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

++$failures (to be consistent 😜)

AnnotationRegistry::loadAnnotationClass('unloadableClass');

self::assertSame(2, $failures);
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Missing line break

Ocramius added a commit that referenced this pull request Jul 22, 2017
Ocramius added a commit that referenced this pull request Jul 22, 2017
@Ocramius
Copy link
Member Author

@lcobucci changes applied on master, thanks!

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

Successfully merging this pull request may close these issues.

None yet

4 participants