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
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?
$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
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
optimizationforeach(\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
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.
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?$a = 'array_keys'; $b = $a(...); $c = 'b'; foreach ($$c as $key) { ... }Obviously not.
\array_keysoptimization will work the same way as an
optimizedstrlenfunction works.That means that the optimization is only going to be applied if the
array_keysfunction is used directly in theforeachloop and only if a)
either the namespace is global b) or\array_keys(...)/use function array_keysis used.Best regards,
BenasI 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
optimizationforeach(\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
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:
-
Binary OP evaluation i. e.
2 * 2does not yieldZEND_ADDopcode but
is instead computed by the compiler directly. -
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
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?$a = 'array_keys'; $b = $a(...); $c = 'b'; foreach ($$c as $key) { ... }Obviously not.
\array_keysoptimization will work the same way as an
optimizedstrlenfunction works.That means that the optimization is only going to be applied if the
array_keysfunction is used directly in theforeachloop and only if a)
either the namespace is global b) or\array_keys(...)/use function array_keysis used.Best regards,
BenasI 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
optimizationforeach(\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
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:
Binary OP evaluation i. e.
2 * 2does not yieldZEND_ADDopcode but
is instead computed by the compiler directly.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,
BenasI 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?$a = 'array_keys'; $b = $a(...); $c = 'b'; foreach ($$c as $key) { ... }Obviously not.
\array_keysoptimization will work the same way as an
optimizedstrlenfunction works.That means that the optimization is only going to be applied if the
array_keysfunction is used directly in theforeachloop and only if a)
either the namespace is global b) or\array_keys(...)/use function array_keysis used.Best regards,
BenasOn 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
optimizationforeach(\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