-
Notifications
You must be signed in to change notification settings - Fork 234
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
#135 optimise multiple class load attempts in AnnotationRegistry
#137
Conversation
Registering an expensive loader like `class_exists` twice doubles the number of calls to the callable - this prevents that from happening.
…ust be using the internal autoloader
… check is replaced by hint
AnnotationRegistry
Urgh, premature merge due to CLI mistake on my side. @lcobucci still, if you can, give it a review... |
* @return void | ||
* An array of classes which cannot be found | ||
* | ||
* @var null[] indexed by class name |
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.
null[]
?
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.
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) { |
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.
Add a space after the casting ((array) $dirs
)
return true; | ||
} | ||
} | ||
|
||
self::$failedToAutoload[$class] = null; |
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.
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)
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.
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'); |
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.
assertSame()
$i = 0; | ||
$autoLoader = function($annotation) use (&$i, $className) { | ||
eval('class ' . $className . ' {}'); | ||
$i++; |
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
AnnotationRegistry::loadAnnotationClass($className); | ||
AnnotationRegistry::loadAnnotationClass($className); | ||
AnnotationRegistry::loadAnnotationClass($className); | ||
$this->assertEquals(1, $i, 'Autoloader should only be called once'); |
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.
assertSame
{ | ||
$failures = 0; | ||
$failingLoader = function (string $annotation) use (& $failures) : bool { | ||
$failures += 1; |
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.
++$failures
(to be consistent 😜)
{ | ||
$failures = 0; | ||
$failingLoader = function (string $annotation) use (& $failures) : bool { | ||
$failures += 1; |
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.
++$failures
(to be consistent 😜)
AnnotationRegistry::loadAnnotationClass('unloadableClass'); | ||
|
||
self::assertSame(2, $failures); | ||
} | ||
} |
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.
Missing line break
@lcobucci changes applied on |
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).