]> granicus.if.org Git - php/commit
Optimize Traversable unpacking in zend_vm_def.h
authorTyson Andre <tysonandre775@hotmail.com>
Sat, 13 Feb 2021 00:42:03 +0000 (19:42 -0500)
committerTyson Andre <tysonandre775@hotmail.com>
Sat, 13 Feb 2021 14:34:48 +0000 (09:34 -0500)
commit8ffc20ace6c8a59b30aea53e2100aa26e4f1f3ee
tree2d1a5e510b68167be1ab86dd2ea48ad2bc59ac92
parentf74a02d263aa332d995f78bd71878012952623b1
Optimize Traversable unpacking in zend_vm_def.h

The C compiler sees that a dynamic function is being called, so it cannot infer
that iter->funcs has not changed.

This results in more assembly instructions and slightly more time to execute that code
path.

Unpacking traversables to arrays(`ZEND_ADD_ARRAY_UNPACK`),
starting foreach loops (`ZEND_FE_FETCH*`), etc. are affected.

```
<?php
/*
 * Before: 1.576 seconds
 * After:  1.474 seconds
 */
function example() {
    $start = hrtime(true);
    $it = new SplFixedArray(1000);
    $total = 0;
    for ($i = 0; $i < 100000; $i++) {
        $total += count([...$it]);
    }
    $end = hrtime(true);
    printf("Elapsed: %.6f\n", ($end - $start) / 1_000_000_000);
}
example();
```
Zend/zend_vm_def.h
Zend/zend_vm_execute.h