Re: Compiler Optimizations
| From: | Benas IML | Date: | Tue, 15 Sep 2020 13:40:05 +0000 |
| Subject: | Re: Compiler Optimizations | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-111863@lists.php.net to get a copy of this message | ||
Hey,
During my free time, I'm implementing that specific
array_keys
optimization. I'm not planning on supporting cases like yours (i. e.
indirection through a variable) since there's no point in doing that. And
also, it's not feasible to support every use case. Should we also support
cases like this?
```php
$a = 'array_keys';
$b = $a(...);
$c = 'b';
foreach ($$c as $key) {
...
}
```
Obviously not. \array_keys optimization will work the same way as an
optimized strlen function works.
That means that the optimization is only going to be applied if the
array_keys function is used directly in the foreach loop and only if a)
either the namespace is global b) or \array_keys(...)/`use function
array_keys` is used.
Best regards,
Benas
On Tue, Sep 15, 2020, 4:23 PM Chase Peeler <chasepeeler@gmail.com> wrote:
> I brought this up on another thread, but it wasn't addressed (which is
> fine, since it was somewhat off-topic). I thought it might be
> worth bringing up in its own thread, though.
>
> In the other thread, someone had mentioned the following compiler
> optimization
>
> foreach(\array_keys($arr) as $key) {
>
> and quietly transform that into:
>
> foreach ($arr as $key => $_unusedVariableNameThatIsntEvenSpilledToTheScope)
> {
>
> I would be more likely to write:
> $keys = array_keys($arr);
> foreach($keys as $key){
> Which would prevent me from being able to take advantage of the
> optimization.
>
> So, what I was wondering, is if there are other optimizations I might be
> missing out on, and if so, are they documented anywhere?
>
>
> --
> Chase Peeler
> chasepeeler@gmail.com
>