|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2016-05-16 14:18 UTC] dmri87 at hotmail dot com
Description: ------------ PHP should improve the creation of Reflection objects. Almost literally any library or framework using Reflection non trivially I've encountered implements its own Reflection objects caching mechanism. That because creating such objects is incredibly expensive and it's way more expensive than caching them all, even if thousands of them. I had to do the same to keep performance acceptable. PHP should do this internally if possible, or try to optimize something, because again, the amount of times I've seen this is incredible and it's a clear sign the creation of such object is just too slow. Just to be clear (sorry if I sound harsh but I actually just want to be clear) Reflection object's creation performance is a problem even for those that are using it trivially. I don't think it's acceptable if the creation of few dozens of these objects turns out to be an important bottleneck for applications that would otherwise be extremely fast. Thank you for reading and double thank you if you intend to work on this. PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Dec 12 11:00:01 2025 UTC |
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 secondsThe 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.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.