Req #72224 [Opn]: Optimize reflection initialization
| From: | nikic@php.net | Date: | Fri, 28 Feb 2020 15:09:12 +0000 |
| Subject: | Req #72224 [Opn]: Optimize reflection initialization | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-225791@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=72224&edit=1
ID: 72224
Updated by: nikic@php.net
Reported by: dmri87 at hotmail dot com
Summary: Optimize reflection initialization
Status: Open
Type: Feature/Change Request
Package: Reflection related
PHP Version: 7.0.7RC1
Block user comment: N
Private report: N
New Comment:
Based on the script provided by OP:
<?php
class A{}
$t = microtime(true);
for($i = 0; $i < 50000000; $i++){
new A();
}
var_dump(microtime(true) - $t);
$t = microtime(true);
for($i = 0; $i < 50000000; $i++){
new ReflectionClass('A');
}
var_dump(microtime(true) - $t);
Numbers on master:
float(1.1946558952332)
float(3.4293010234833)
So ReflectionClass construction is now a bit less than 3x slower than the object construction.
Previous Comments:
------------------------------------------------------------------------
[2016-05-29 10:04:03] nikic@php.net
The only obvious inefficiency in the ReflectionClass constructor is the population of the
"name" property -- which will allocate and then destroy a temporary string. If we replace
this with a persistent string we might get a 10% improvement there.
The difference between "new A" and "new ReflectionClass('A')" is that
the latter has to perform a constructor call (expensive) and a runtime lookup of the class
'A' (expensive). This already accounts for most of the difference.
Some ctors like ReflectionProperty::__construct() could be optimized by using a union for different
reflection object structures, rather than a separate allocation.
------------------------------------------------------------------------
[2016-05-29 04:22:18] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
------------------------------------------------------------------------
[2016-05-23 20:20:58] dmri87 at hotmail dot com
The example would probably make more sense if I assigned the object to a variable.
Also notice how I left out the constructor. I'd expect ReflectionClass to compare in
performance to the creation of an object without calling its constructor.
Sorry for the multiple replies.
------------------------------------------------------------------------
[2016-05-23 20:03:46] dmri87 at hotmail dot com
Literally any reflection object, krakjoe.
It is comparable in performance to the creation of other objects, sure, but it is too slow
considered that reflection is basically just static information.
For instance, why is this 6 (!!!) times slower than creating the actual object?
class A{}
for($i = 0; $i < 1000000; $i++){
new A();
} // 0.3150179386 seconds
for($i = 0; $i < 1000000; $i++){
new ReflectionClass('A');
} // 1.9061090946 seconds
------------------------------------------------------------------------
[2016-05-20 19:29:35] krakjoe@php.net
I've flicked through the source code of the most common reflector constructors, and I see
nothing that is obviously causing such problems.
Can you provide some example code that behaves extra ordinarily, perhaps that will help me to
understand better ?
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=72224
--
Edit this bug report at https://bugs.php.net/bug.php?id=72224&edit=1