Different methods of named value access
Wcc PHP objects in C++ have an example of a dynamic property oriented class - Wcc\Config , and also Wcc\Hmap class, which internally uses a PHP array to store properties.
Accessing properties and array values by string keys is the mainstay of PHP coding. To compare the efficiency of various kinds of PHP property/value access by key name, I created a script that repeated a numeric sum and division ( a + b ) / a, and averaged the time taken over a large number of iterations. The following table shows the kind of entity, and results ratioed two of chosen methods, being just local variables, and object-declared property. Compared are local array with string keys, dynamic property objects, and object properties implemented as array storage, and a call to a function implemented as a method of Wcc\Pair, just for this purpose.
The ratio of averaged times to complete are formatted to 2 decimal places below.
| Implementation | X / Local variable (e) | X / declared property (a) |
|---|---|---|
| Local variables (e) | 1 | 0.6 |
| Wcc\Pair (C++) declared properties (a) | 1.66 | 1 |
| EmptyTest (PHP) declared properties (i) | 1.66 | 1 |
| Wcc\Pair call test_calc() (c) | 1.77 | 1.07 |
| Wcc\Pair (C++) methods get (b) | 3.36 | 2.03 |
| Wcc\Config dynamic properties | 1.78 | 1.08 |
| Use local array (g) | 2.14 | 1.29 |
| Extend stdClass (h) | 3.81 | 2.3 |
| Hmap property handler (f) | 2.92 | 1.76 |
| Hmap array handler (m) | 3.78 | 2.28 |
| ArrayObject as [array] (k) | 4.97 | 3 |
| ArrayObject ->Property (j) | 5.14 | 3.11 |
The results suggest the unsurprising find that using script function local variables is optimal, and object access to declared properties take about 67% more time. For any none trivial calculation, a method call to compiled code (c) is good. Even for a trivial calculation, doing it within a declared method is faster than using get method (b) 3 times for doing calculation inline locally.
All of the short statements looping repeat processes are fast, presumed CPU cache coherancy works well, since each of the 12 test functions timed a loop of 1000 iterations, and the whole sequence repeated 1000 times, which makes for 12 million iterations. Onn a modern x86_64 processor, this takes few seconds.
Dynamic properties a little bit slower. Property handlers seem faster than array handlers. Except for the "ArrayObject" in its "ARRAY_AS_PROPS" setup, which lagged the others, for reasons I cannot explain.
Wcc\Config and Wcc\Hmap are into the Wcc extension using the ZPP / base_d C++ classes. They seem okay for simple uses they are reasonably effective at basic get/set compared to ArrayObject, but may have some issues for complex expression requirement.
The script that produced this table is "tests/pairs.php". This was done a while ago, and this developer is tired of maintaining and re-running it. Okay, it needs an updated version of the PHP stats extension, which is on my github account, git@github.com:betrixed/stats.gitThe script actually generates entire results table in markdown text, so not too hard regenerate this. The ZPP project has had many updates since them, and its not even the same OS and computer and PHP version (Now PHP 8.5.10, cachyos linux, Ryzen 5 lenovo laptop).
| Implementation | X / Local variable (e) | X / declared property (a) |
|---|---|---|
| Local variables (e) | 1 | 0.87 |
| Wcc\Pair (C++) declared properties (a) | 1.15 | 1 |
| EmptyTest (PHP) declared properties (i) | 1.17 | 1.01 |
| Wcc\Pair call test_calc() (c) | 3.4 | 2.95 |
| Wcc\Pair (C++) methods get (b) | 6.94 | 6.02 |
| Wcc\Config dynamic properties (d) | 1.18 | 1.02 |
| Use local array (g) | 1.32 | 1.14 |
| Extend stdClass (h) | 1.53 | 1.33 |
| Hmap property handler (f) | 1.57 | 1.36 |
| Hmap array handler (m) | 1.77 | 1.53 |
| ArrayObject [array] (k) | 2.11 | 1.83 |
| ArrayObject ->Property (j) | 2.11 | 1.83 |
| Target magic method ->Property (n) | 10.37 | 9 |
The relative execution times for the various loop methods remain consistent. They show that declared properties are optimal for efficient access to named zval, dynamic properties not much worse, local array slower again, stdClass properties a bit disappointing. ArrayObject is again the most disappointing. The results suggest that PHP evolution is on an informed track in emphasizing the efficiency of object declared properties, Arrays are still convenient but less efficient, and that extensions should try to design around this.
The recent addition of magic method used to set an array property on this Wcc\Target class "__get" and "__set" shows this being 5 times slower, literally this takes the long way round.
The situation inside the workings of an extension is different. Properties in a C++ object are directly accessible by address offset. Function calls are direct. If a number of objects are working together for a purpose of moderate computation complexity, their memory access isn't impeded by the workings of an interpreter or Xdebug. Also C++ objects are not bound to hold their data members as Zval properties which are publicly accessible.
The PHP reference counted handles of zend_string, zend_object, HashTable* can be maintained by C++ objects independently of existing within a zval, and still be added to or removed from zval for data exchange and storage as required, but used in code as independent interacting entities. That is the purpose of having their single pointer class wrappers of str_rc, obj_rc, and htab_rc respectively, which covers a majority of object construction requirements.