constant folding optimization

php.internals

Nuno Lopes

18 years ago
Hi, I made a patch to implement constant folding optimization in the Zend engine. The patch is available at: http://web.ist.utl.pt/nuno.lopes/zend_constant_folding.txt Please review & commit. Regards, Nuno P.S.: 'make test' shows 2 regressions with this patch (tests/run-test/test005.phpt & tests/run-test/test008a.phpt). they appear because of the "division by zero" warning. This problem still needs to be fixed before commiting the patch.

Stanislav Malyshev

18 years ago
> I made a patch to implement constant folding optimization in the Zend > engine. > The patch is available at: > http://web.ist.utl.pt/nuno.lopes/zend_constant_folding.txt
I like the idea a lot, however I think this patch has one significant downside - as far as I understand, it doesn't enable constant expressions in constant contexts. I.e. I can do $a = 2+2 and get it evaluated but I can't do function foo($a = 2+2) or const $a = 2+2. If we moved this to the parser level I think we could do both.
-- Stanislav Malyshev, Zend Software Architect stas@zend.com http://www.zend.com/ (408)253-8829 MSN: stas@zend.com

Nuno Lopes

18 years ago
>> I made a patch to implement constant folding optimization in the >> Zend engine. >> The patch is available at: >> http://web.ist.utl.pt/nuno.lopes/zend_constant_folding.txt > > I like the idea a lot, however I think this patch has one > significant downside - as far as I understand, it doesn't enable > constant expressions in constant contexts. I.e. I can do $a = 2+2 > and get it evaluated but I can't do function foo($a = 2+2) or const > $a = 2+2. If we moved this to the parser level I think we could do > both.
Well we don't support that syntax at this moment. If you are saying that we could add support for such syntax.. it does seem a good idea, but in that case we would need to modify a few rules of the grammar (like adding a const_expr production, and allow static_scalar derive it, etc..). If others agree with this syntax, I can take a look in implementing it. Nuno

Stanislav Malyshev

18 years ago
> Well we don't support that syntax at this moment. If you are saying that > we could add support for such syntax.. it does seem a good idea, but in > that case we would need to modify a few rules of the grammar (like > adding a const_expr production, and allow static_scalar derive it, etc..).
Right, that's what I had in mind. That doesn't seem to break anything and allows also a little more freedom. let's see if anyone objects.
-- Stanislav Malyshev, Zend Software Architect stas@zend.com http://www.zend.com/ (408)253-8829 MSN: stas@zend.com

Nuno Lopes

18 years ago
>> Well we don't support that syntax at this moment. If you are saying >> that we could add support for such syntax.. it does seem a good >> idea, but in that case we would need to modify a few rules of the >> grammar (like adding a const_expr production, and allow >> static_scalar derive it, etc..). > > Right, that's what I had in mind. That doesn't seem to break > anything and allows also a little more freedom. let's see if anyone > objects.
I tried implementing this and it seems much more complicated than what I though. I modified the static_scalar rule of the grammar and I got hundreds of reduce/reduce and shift/reduce ambiguities. Ignoring the warnings, well.. didn't work (the parser then couldn't process the run-tests.php script). Nuno P.S.: I already spoted a few more places where we can produce better byte-code (including more simple constant folding).. but one thing at a time :)

Dmitry Stogov

18 years ago
I see a problem with this idea. In case we support constant expressions, we should support constants in such expressions, but the values of these constants may be unknown in compile time. <?php class Foo { const A = 0; } ?> <?php require_once "foo.php"; class Bar extends Foo { const B = self::A + 1; } ?> Delaying constant initialization for expressions in general will require keeping of Abstaract Syntax Tree, that should be evaluated at run-time by zval_update_constant(). Thanks. Dmitry.

Stanislav Malyshev

18 years ago
> In case we support constant expressions, we should support constants in such > expressions, but the values of these constants may be unknown in compile > time.
I don't see why we should support constants in such expressions.
-- Stanislav Malyshev, Zend Software Architect stas@zend.com http://www.zend.com/ (408)253-8829 MSN: stas@zend.com

Dmitry Stogov

18 years ago
Now we support "const FOO = BAR;" where BAR is another constant (but BAR is also a simple expression). I don't see how can we support constant expressions and don't support constants in them. Thanks. Dmitry.

Marcus Börger

18 years ago
Hello Dmitry, This is a good catch. I gave it a bit of attention some time ago and it is not trivial to fix. However code often improves from this. Most of all ofeten enough you end up with some consts that would in other languages be enums like: class Week { const Monday = 0; const Tuesday = Monday + 1; // ... } or this: class Logging { const INFO = 0; const WARN = INFO + 1; const FAIL = WARN + 1; // ... } I'd especially like this because it would make my enum implementation in pecl/spl_types more handy. marcus Monday, September 10, 2007, 9:55:49 AM, you wrote:
> I see a problem with this idea. > In case we support constant expressions, we should support constants in such > expressions, but the values of these constants may be unknown in compile > time.
> <?php > class Foo { > const A = 0; > }
?>>
> <?php > require_once "foo.php"; > class Bar extends Foo { > const B = self::A + 1; > }
?>>
> Delaying constant initialization for expressions in general will require > keeping of Abstaract Syntax Tree, that should be evaluated at run-time by > zval_update_constant().
> Thanks. Dmitry.
>> -----Original Message----- >> From: Nuno Lopes [mailto:nlopess@php.net] >> Sent: Saturday, September 08, 2007 10:50 PM >> To: Stanislav Malyshev >> Cc: internals@lists.php.net; andi@php.net; dmitry@php.net >> Subject: Re: Re: [PHP-DEV] constant folding optimization >> >> >> >> I made a patch to implement constant folding optimization in the >> >> Zend engine. >> >> The patch is available at: >> >> http://web.ist.utl.pt/nuno.lopes/zend_constant_folding.txt >> > >> > I like the idea a lot, however I think this patch has one >> > significant downside - as far as I understand, it doesn't enable >> > constant expressions in constant contexts. I.e. I can do $a = 2+2 >> > and get it evaluated but I can't do function foo($a = 2+2) >> or const >> > $a = 2+2. If we moved this to the parser level I think we could do >> > both. >> >> Well we don't support that syntax at this moment. If you are saying >> that we could add support for such syntax.. it does seem a >> good idea, >> but in that case we would need to modify a few rules of the grammar >> (like adding a const_expr production, and allow static_scalar derive >> it, etc..). >> >> If others agree with this syntax, I can take a look in >> implementing it. >> >> Nuno >> >> -- >> PHP Internals - PHP Runtime Development Mailing List >> To unsubscribe, visit: http://www.php.net/unsub.php >>
Best regards, Marcus

Alexey Zakhlestin

18 years ago
Did this topic end nowhere? Isn't some fix here better than no fix? class-constants are currently compile-time constants and letting them use constants from classes defined in other files will move them to run-time constants (because php has ambiguity-feature of conditional-loading), so, probably, this part should be skipped anyway. On 9/10/07, Marcus Boerger <helly@php.net> wrote:
> Hello Dmitry, > > This is a good catch. I gave it a bit of attention some time ago and it is > not trivial to fix. However code often improves from this. Most of all > ofeten enough you end up with some consts that would in other languages be > enums like: > class Week { > const Monday = 0; > const Tuesday = Monday + 1; > // ... > } > or this: > class Logging { > const INFO = 0; > const WARN = INFO + 1; > const FAIL = WARN + 1; > // ... > } > > I'd especially like this because it would make my enum implementation in > pecl/spl_types more handy. > > marcus > > Monday, September 10, 2007, 9:55:49 AM, you wrote: > > > I see a problem with this idea. > > In case we support constant expressions, we should support constants in such > > expressions, but the values of these constants may be unknown in compile > > time. > > > <?php > > class Foo { > > const A = 0; > > } > ?>> > > > <?php > > require_once "foo.php"; > > class Bar extends Foo { > > const B = self::A + 1; > > } > ?>> > > > Delaying constant initialization for expressions in general will require > > keeping of Abstaract Syntax Tree, that should be evaluated at run-time by > > zval_update_constant(). > > > Thanks. Dmitry. > > >> -----Original Message----- > >> From: Nuno Lopes [mailto:nlopess@php.net] > >> Sent: Saturday, September 08, 2007 10:50 PM > >> To: Stanislav Malyshev > >> Cc: internals@lists.php.net; andi@php.net; dmitry@php.net > >> Subject: Re: Re: [PHP-DEV] constant folding optimization > >> > >> > >> >> I made a patch to implement constant folding optimization in the > >> >> Zend engine. > >> >> The patch is available at: > >> >> http://web.ist.utl.pt/nuno.lopes/zend_constant_folding.txt > >> > > >> > I like the idea a lot, however I think this patch has one > >> > significant downside - as far as I understand, it doesn't enable > >> > constant expressions in constant contexts. I.e. I can do $a = 2+2 > >> > and get it evaluated but I can't do function foo($a = 2+2) > >> or const > >> > $a = 2+2. If we moved this to the parser level I think we could do > >> > both. > >> > >> Well we don't support that syntax at this moment. If you are saying > >> that we could add support for such syntax.. it does seem a > >> good idea, > >> but in that case we would need to modify a few rules of the grammar > >> (like adding a const_expr production, and allow static_scalar derive > >> it, etc..). > >> > >> If others agree with this syntax, I can take a look in > >> implementing it. > >> > >> Nuno > >> > >> -- > >> PHP Internals - PHP Runtime Development Mailing List > >> To unsubscribe, visit: http://www.php.net/unsub.php > >> > > > > > Best regards, > Marcus > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > >
-- Alexey Zakhlestin http://blog.milkfarmsoft.com/

Nuno Lopes

18 years ago
There is no "fix" for the the class constants yet. I didn't have the time to look at it nor anyone else did. Anyway my patch for "regular" constant folding is still on-line, if someone wants to commit or use it. Nuno ----- Original Message -----