php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #79337 Perform CSE of FETCH_CONSTANT
Submitted: 2020-03-03 11:16 UTC Modified: 2020-03-03 11:37 UTC
From: bugreports at gmail dot com Assigned:
Status: Open Package: opcache
PHP Version: 7.4Git-2020-03-03 (Git) OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: bugreports at gmail dot com
New email:
PHP Version: OS:

 

 [2020-03-03 11:16 UTC] bugreports at gmail dot com
Description:
------------
i would have expected that opcache optimizes the repeated FETCH_CONSTANT out

---------------------------------

define('IS_ROBOT', 1);

function a()
{
 return IS_ROBOT . IS_ROBOT . IS_ROBOT . IS_ROBOT;
}

function b()
{
 $IS_ROBOT = IS_ROBOT;
 return $IS_ROBOT . $IS_ROBOT . $IS_ROBOT . $IS_ROBOT;
}

---------------------------------

*************************************************************************************************************
*                                                VLD OPCACHE                                                *
*************************************************************************************************************

function name:  a
number of ops:  8
-------------------------------------------------------------------------------------
   7     0*       FETCH_CONSTANT                                   ~1      'IS_ROBOT'
         1*       FETCH_CONSTANT                                   ~2      'IS_ROBOT'
         2*       FAST_CONCAT                                      ~0      ~1, ~2
         3*       FETCH_CONSTANT                                   ~2      'IS_ROBOT'
         4*       FAST_CONCAT                                      ~1      ~0, ~2
         5*       FETCH_CONSTANT                                   ~2      'IS_ROBOT'
         6*       FAST_CONCAT                                      ~0      ~1, ~2
         7*       RETURN                                                   ~0

function name:  b
number of ops:  5
-------------------------------------------------------------------------------------
  12     0*       FETCH_CONSTANT                                   !0      'IS_ROBOT'
  13     1*       FAST_CONCAT                                      ~1      !0, !0
         2*       FAST_CONCAT                                      ~2      ~1, !0
         3*       FAST_CONCAT                                      ~1      ~2, !0
         4*       RETURN                                                   ~1

Expected result:
----------------
i would have expexted that repeatet FETCH_CONSTANT within a function is optimized out like the second implementation doing it in userland by copy it to a local variable

not that much of a performance overhead but on the other hand if it's a function which a loop using dozens of constants more than once.....


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-03-03 11:27 UTC] bugreports at gmail dot com
50 Mio calls without and with opcache, a full second runtime difference while native optimization could be faster than the handcrafted copy into a local variable is a lot of runtime given it's just 4 usages

in real world code this could sum up much more when it's a funtion lopping over some large array and using the same constant thousands of times at once

a(): 11.7516
b(): 10.9853

a(): 10.9835
b(): 9.9896
 [2020-03-03 11:37 UTC] nikic@php.net
-Summary: optimize FETCH_CONSTANT +Summary: Perform CSE of FETCH_CONSTANT
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Mon Oct 26 05:01:24 2020 UTC