Req #72224 [Opn->Csd]: Optimize reflection initialization
| From: | krakjoe@php.net | Date: | Sat, 22 May 2021 15:03:41 +0000 |
| Subject: | Req #72224 [Opn->Csd]: Optimize reflection initialization | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-233974@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: krakjoe@php.net
Reported by: dmri87 at hotmail dot com
Summary: Optimize reflection initialization
-Status: Open
+Status: Closed
Type: Feature/Change Request
Package: Reflection related
PHP Version: 7.0.7RC1
-Assigned To:
+Assigned To: krakjoe
Block user comment: N
Private report: N
New Comment:
Today, the reflection class constructor makes no duplicate strings. You still have to perform a
lookup if you pass the class name as a string, which as mentioned accounts for the majority of the
difference. Comparing new Class to new ReflectionClass doesn't make a lot of sense, new has a
cache slot for the class entry.
I think there's not much more that can be done here, so I'm closing this bug.
Previous Comments:
------------------------------------------------------------------------
[2020-02-28 15:09:12] nikic@php.net
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.
------------------------------------------------------------------------
[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
------------------------------------------------------------------------
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