Compiler Optimizations

php.internals

Chase Peeler

5 years ago
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

Benas IML

5 years ago
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:

Chase Peeler

5 years ago
I wasn't proposing that my example be supported. I'm totally okay with the fact that it doesn't. My question was about whether or not those kinds of optimizations are documented anywhere so that developers can make sure they don't miss out on them by not fitting the proper pattern. On Tue, Sep 15, 2020 at 9:40 AM Benas IML <benas.molis.iml@gmail.com> wrote:
> 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 >> >
-- Chase Peeler chasepeeler@gmail.com

Benas IML

5 years ago
Ah, sorry! Misread your post. Anyways, the compiler doesn't transform `\array_keys()` yet, so there's no optimization for that. As for other compiler optimizations, 2 that I know that the compiler does is: 1. Binary OP evaluation i. e. `2 * 2` does not yield `ZEND_ADD` opcode but is instead computed by the compiler directly. 2. There are special functions that have their own opcodes. You can learn more about those here: https://phpinternals.net/articles/optimising_internal_functions_via_new_opcode_instructions Best regards, Benas On Tue, Sep 15, 2020, 4:44 PM Chase Peeler <chasepeeler@gmail.com> wrote:

Chase Peeler

5 years ago
On Tue, Sep 15, 2020 at 9:51 AM Benas IML <benas.molis.iml@gmail.com> wrote:
> Ah, sorry! Misread your post. Anyways, the compiler doesn't transform > `\array_keys()` yet, so there's no optimization for that. > > As for other compiler optimizations, 2 that I know that the compiler does > is: > > 1. Binary OP evaluation i. e. `2 * 2` does not yield `ZEND_ADD` opcode but > is instead computed by the compiler directly. > > 2. There are special functions that have their own opcodes. You can learn > more about those here: > > https://phpinternals.net/articles/optimising_internal_functions_via_new_opcode_instructions >
That's a good starting point, thanks.
> > Best regards, > Benas > > On Tue, Sep 15, 2020, 4:44 PM Chase Peeler <chasepeeler@gmail.com> wrote: > >> I wasn't proposing that my example be supported. I'm totally okay with >> the fact that it doesn't. My question was about whether or not those kinds >> of optimizations are documented anywhere so that developers can make sure >> they don't miss out on them by not fitting the proper pattern. >> >> On Tue, Sep 15, 2020 at 9:40 AM Benas IML <benas.molis.iml@gmail.com> >> wrote: >> >>> 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 >>>> >>> >> >> -- >> Chase Peeler >> chasepeeler@gmail.com >> >
-- Chase Peeler chasepeeler@gmail.com