Re: [RFC] [VOTE] Constant scalar expressions

From: Date: Fri, 22 Nov 2013 01:30:04 +0000
Subject: Re: [RFC] [VOTE] Constant scalar expressions
References: 1 2 3 4 5 6 7  Groups: php.internals 
Request: Send a blank email to internals+get-70273@lists.php.net to get a copy of this message
Hi all,

On Fri, Nov 22, 2013 at 10:19 AM, Yasuo Ohgaki <yohgaki@ohgaki.net> wrote:

> On Fri, Nov 22, 2013 at 10:09 AM, Yasuo Ohgaki <yohgaki@ohgaki.net> wrote:
>
>> Anyway, constant variables are useful for VM/op code optimization.
>>
>> include('foo.php'); // const a = 123;
>> const b = a;
>>
>> is allowed currently. We may restrict this behavior only to the same file,
>> so that VM/op code optimization can be done.
>>
>> Currently, no expression is allowed with 'const'. Defining 'const'
>> using
>> another constant with the same value is useless. It's a bad coding
>> practice
>> in many case, I suppose.
>>
>>  I understand use cases of scalar expressions with constants, but I'm
>> concerned
>> loosing door for further optimizations. (e.g. $x = $x + 123 is faster
>> than $x = $x + a,
>>  where a is 123 in constant var hash)
>>
>> We may be better to restrict expressions with constants only to the same
>> file
>> even if we loose some benefits/flexibility in user code.
>>
>> Is this discussed?
>>
>
> I voted 'yes', but I change my mind. Sorry.
> If the RFC leaves door for constant optimization, I'll vote 'yes'.
> Constant look up is not a simple hash look up.
>

One more additional note why I decided to vote 'no' for this.
We already have dynamic constant via define().

<?php
define('A', 123);
define('B', A*2);

echo B; // 246
?>

We can even use function return values with define().

<?php
function foo() {
  return 123;
}

define('A', foo());

echo A; // 123
?>

I prefer 'const' to be a static for better performance, since
we have dynamic constant by define() already.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net


Thread (16 messages)

« previous php.internals (#70273) next »