On Wed, 26 Feb 2003, David Brown wrote:

> This may well be a stupid question, but I've spend enough time staring
> blankly at zend_compile.c/zend_execute.c that I figured it was time to
> ask. :)
> 
> Say I have a section of code like this:
> 
> <?php
>   $s1 = 'foo' . 'bar' . 'baz';
>   $s2 = 'foobarbaz';
> ?>
> 
> In the PHP bytecode (I hope I'm using the right terminology - I mean the
> stuff in the opline; the stuff that gets stored in the cache under APC
> or Zend cache), is there any functional difference between the
> assignment to $s1 and the assignment to $s2? Or, to put it more
> precisely, is Zend currently able to figure out that the strings on both
> sides of the concatenation operator are constants, and don't need to be
> concatenated at runtime?

No, the engine doesn't do this at compile time. This first one produces:

number of ops:  5
line     #  op                   fetch  ext operands
-------------------------------------------------------------------------------
   1     0  CONCAT                          ~1, 'foo', 'bar'
         1  CONCAT                          ~2, ~1, 'baz'
         2  FETCH_W              local      $0, 's1'
         3  ASSIGN                          $3, $0, ~2
   3     4  RETURN                          1

The second one:

line     #  op                   fetch  ext operands
-------------------------------------------------------------------------------
   1     0  FETCH_W              local      $0, 's2'
         1  ASSIGN                          $1, $0, 'foobarbaz'
   2     2  RETURN                          1

> If not, can anyone explain the barriers to doing something like this?

It's the job of an optimizer, not of a compiler. And because PHP doesn't 
have an internal optimizer, this is not optimized out. You can either 
check the ZendOptimiser (I can't show you the opcodes that that 
generates) or PEAR::Optimizer, which is in Pecl (which might not do this 
optimization yet btw).

Derick

-- 
                                                Stop mad cowboy disease!
-------------------------------------------------------------------------
 Derick Rethans                                 http://derickrethans.nl/ 
 PHP Magazine - PHP Magazine for Professionals       http://php-mag.net/
-------------------------------------------------------------------------

-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to