zendtech / ZendOptimizerPlus

Other
915 stars 142 forks source link

Constant which is fixed at compilation is not optimized. #156

Closed hidenori-wasa closed 10 years ago

hidenori-wasa commented 10 years ago

My environment is below.

The following is the issue sample code C:/xampp/htdocs/Pear-Package/OPcacheTestA.php.

<?php

define(CONST_BY_DEFINE, false);

// #8: op = DECLARE_CONST
// #8: operands = 'NOT_FIXED_AT_COMPILE', <unknown>
const NOT_FIXED_AT_COMPILE = CONST_BY_DEFINE; // "CONST_BY_DEFINE" is "<unknown>".

// #10: op = DECLARE_CONST
// #10: operands = 'FIXED_AT_COMPILE', false
const FIXED_AT_COMPILE = false; // "false" is not "<unknown>".

if (NOT_FIXED_AT_COMPILE) {
    echo 'Debug code which cannot delete.';
}

// #17: op = FETCH_CONSTANT
// #17: return = ~0
// #17: operands = 'FIXED_AT_COMPILE'

// #18: op = JMPZ
// #18: operands = ~0, ->21
if (FIXED_AT_COMPILE) { // We can delete from this line.
    echo 'Debug code which can delete.';
} // We can delete until this line.

/* Result of shortcut command "%SystemRoot%\system32\cmd.exe /k php -dopcache.enable_cli=1 -dvld.active=1 -dvld.execute=0 -dvld.verbosity=0 -dvld.dump_paths=0 C:/xampp/htdocs/Pear-Package/OPcacheTestA.php".
line     # *  op                           fetch          ext  return  operands
---------------------------------------------------------------------------------
3     0  >   EXT_STMT
        1      EXT_FCALL_BEGIN
        2      FETCH_CONSTANT                                   ~0      'CONST_BY_DEFINE'
        3      SEND_VAL                                                 ~0
        4      SEND_VAL                                                 false
        5      DO_FCALL                                      2          'define'
        6      EXT_FCALL_END
7     7      EXT_STMT
        8      DECLARE_CONST                                            'NOT_FIXED_AT_COMPILE', <unknown>
11     9      EXT_STMT
        10      DECLARE_CONST                                            'FIXED_AT_COMPILE', false
13    11      EXT_STMT
        12      FETCH_CONSTANT                                   ~0      'NOT_FIXED_AT_COMPILE'
        13    > JMPZ                                                     ~0, ->16
14    14  >   EXT_STMT
        15      ECHO                                                     'Debug+code+which+cannot+delete.'
23    16  >   EXT_STMT
        17      FETCH_CONSTANT                                   ~0      'FIXED_AT_COMPILE'
        18    > JMPZ                                                     ~0, ->21
24    19  >   EXT_STMT
        20      ECHO                                                     'Debug+code+which+can+delete.'
58    21  >   EXT_STMT
        22    > RETURN                                                   1
*/

// We can delete "#16 - #20".

?>
TerryE commented 10 years ago

@hidenori-wasa,

const FIXED_AT_COMPILE = \false;
if (FIXED_AT_COMPILE) {    
    echo 'Debug code which cannot delete.';
}

This generates:

   2     0  >   DECLARE_CONST         'FIXED_AT_COMPILE', false
   < if code commented out >

The issue here is that the optimizer only optimizes "built-in" extension constants and expressions thereof. All other constants can be overridden in a namespace, so can't be safely optimized.

However, true and false are special, in that (1) they are predefined in the compiler, (2) they can't be overridden in a namespace. Also constants declared and accessed in in the root namespace code can't be overridden, and these checks need not apply.

Even so, this isn't a bug. What you are suggesting is to add an extra special case to allow more optimizations. It is a feature enhancement, but a very sensible one. Thanks for highlighting this. :-)

hidenori-wasa commented 10 years ago

I understood. Thank you for your reply.

TerryE commented 10 years ago

@hidenori-wasa, you didn't edit your O/P ;-) and what you pointed out is worth acting on.

hidenori-wasa commented 10 years ago

Sorry, It might be difficult to understand. It corrected as follows.


My environment is below.

The following is the issue sample code C:/xampp/htdocs/Pear-Package/OPcacheTestA.php.

    <?php

    define(CONST_BY_DEFINE, false);

    // #8: op = DECLARE_CONST
    // #8: operands = 'NOT_FIXED_AT_COMPILE', <unknown>
    const NOT_FIXED_AT_COMPILE = CONST_BY_DEFINE; // "CONST_BY_DEFINE" is "<unknown>".

    // #10: op = DECLARE_CONST
    // #10: operands = 'FIXED_AT_COMPILE', false
    const FIXED_AT_COMPILE = false; // "false" is not "<unknown>".

    if (NOT_FIXED_AT_COMPILE) {
        echo 'Debug code which cannot delete.';
    }

    // #17: op = FETCH_CONSTANT
    // #17: return = ~0
    // #17: operands = 'FIXED_AT_COMPILE'

    // #18: op = JMPZ
    // #18: operands = ~0, ->21
    if (FIXED_AT_COMPILE) { // We can delete from this line.
        echo 'Debug code which can delete.';
    } // We can delete until this line.

    ?>

Windows shortcut command. "%SystemRoot%\system32\cmd.exe /k php -dopcache.enable_cli=1 -dvld.active=1 -dvld.execute=0 -dvld.verbosity=0 -dvld.dump_paths=0 C:/xampp/htdocs/Pear-Package/OPcacheTestA.php" Its result.

    line     # *  op                           fetch          ext  return  operands
    ---------------------------------------------------------------------------------
    3     0  >   EXT_STMT
            1      EXT_FCALL_BEGIN
            2      FETCH_CONSTANT                                   ~0      'CONST_BY_DEFINE'
            3      SEND_VAL                                                 ~0
            4      SEND_VAL                                                 false
            5      DO_FCALL                                      2          'define'
            6      EXT_FCALL_END
    7     7      EXT_STMT
            8      DECLARE_CONST                                            'NOT_FIXED_AT_COMPILE', <unknown>
    11     9      EXT_STMT
            10      DECLARE_CONST                                            'FIXED_AT_COMPILE', false
    13    11      EXT_STMT
            12      FETCH_CONSTANT                                   ~0      'NOT_FIXED_AT_COMPILE'
            13    > JMPZ                                                     ~0, ->16
    14    14  >   EXT_STMT
            15      ECHO                                                     'Debug+code+which+cannot+delete.'
    23    16  >   EXT_STMT
            17      FETCH_CONSTANT                                   ~0      'FIXED_AT_COMPILE'
            18    > JMPZ                                                     ~0, ->21
    24    19  >   EXT_STMT
            20      ECHO                                                     'Debug+code+which+can+delete.'
    28    21  >   EXT_STMT
            22    > RETURN                                                   1

We may be able to delete #16 - #20.


Thank you for pointing out.

dstogov commented 10 years ago

Actually, I think such constants may be optimized out and if we don't do it, it's just a limitation of our approach. I'll try to look into it.

On Sun, Dec 29, 2013 at 6:08 PM, hidenori-wasa notifications@github.comwrote:

Sorry, It might be difficult to understand.

It corrected as follows.

My environment is below.

  • PHP Version 5.3.1
  • Architecture=x86
  • Zend Extension Build=API220090626,TS,VC9

The following is the issue sample code C:/xampp/htdocs/Pear-Package/OPcacheTestA.php.

<?php

define(CONST_BY_DEFINE, false);

// #8: op = DECLARE_CONST
// #8: operands = 'NOT_FIXED_AT_COMPILE', <unknown>
const NOT_FIXED_AT_COMPILE = CONST_BY_DEFINE; // "CONST_BY_DEFINE" is "<unknown>".

// #10: op = DECLARE_CONST
// #10: operands = 'FIXED_AT_COMPILE', false
const FIXED_AT_COMPILE = false; // "false" is not "<unknown>".

if (NOT_FIXED_AT_COMPILE) {
    echo 'Debug code which cannot delete.';
}

// #17: op = FETCH_CONSTANT
// #17: return = ~0
// #17: operands = 'FIXED_AT_COMPILE'

// #18: op = JMPZ
// #18: operands = ~0, ->21
if (FIXED_AT_COMPILE) { // We can delete from this line.
    echo 'Debug code which can delete.';
} // We can delete until this line.

?>

Windows shortcut command.

"%SystemRoot%\system32\cmd.exe /k php -dopcache.enable_cli=1 -dvld.active=1 -dvld.execute=0 -dvld.verbosity=0 -dvld.dump_paths=0 C:/xampp/htdocs/Pear-Package/OPcacheTestA.php" Its result.

line     # *  op                           fetch          ext  return  operands
---------------------------------------------------------------------------------
3     0  >   EXT_STMT
        1      EXT_FCALL_BEGIN
        2      FETCH_CONSTANT                                   ~0      'CONST_BY_DEFINE'
        3      SEND_VAL                                                 ~0
        4      SEND_VAL                                                 false
        5      DO_FCALL                                      2          'define'
        6      EXT_FCALL_END
7     7      EXT_STMT
        8      DECLARE_CONST                                            'NOT_FIXED_AT_COMPILE', <unknown>
11     9      EXT_STMT
        10      DECLARE_CONST                                            'FIXED_AT_COMPILE', false
13    11      EXT_STMT
        12      FETCH_CONSTANT                                   ~0      'NOT_FIXED_AT_COMPILE'
        13    > JMPZ                                                     ~0, ->16
14    14  >   EXT_STMT
        15      ECHO                                                     'Debug+code+which+cannot+delete.'
23    16  >   EXT_STMT
        17      FETCH_CONSTANT                                   ~0      'FIXED_AT_COMPILE'
        18    > JMPZ                                                     ~0, ->21
24    19  >   EXT_STMT
        20      ECHO                                                     'Debug+code+which+can+delete.'
28    21  >   EXT_STMT
        22    > RETURN                                                   1

We may be able to delete #16 - #20.

Thank you for pointing out.

— Reply to this email directly or view it on GitHubhttps://github.com/zendtech/ZendOptimizerPlus/issues/156#issuecomment-31317573 .