How to efficiently optimize PHP code parsing loss _ PHP Tutorial

Source: Internet
Author: User
How to efficiently optimize PHP code parsing loss. Programmers are discovering that the loss in PHP syntax parsing accounts for a large proportion. if valgrind is used to check his C call, about 50% of the time is spent on lexyacc. Programmers are doing

It is found that the loss in PHP syntax parsing accounts for a large proportion. if valgrind is used to view its C call, it will find that about 50% of the time is used on lex & yacc. That is, converting PHP code into opcode. That is, PHP code parsing loss.

In this regard, the ultimate goal of PHP code parsing loss is to run only one PHP file for one access, and this file does not contain any code unrelated to this process.

It is a challenge to understand the code structure and ensure the performance.

Our solution is to compile access files into files through a compilation system similar to smarty: because shopex is a mvc structure, the compilation granularity corresponds to a process file for each controller method.

When the controller is called for the first time, it monitors every model-method and sub-process that flows through one method, and extracts the data, and adds the common database connection function, configuration files are combined into a single ultimate php file.
Cache updates are basically version updates. Touch the last modification time of a cachestat file.

There are two challenges:

* A model is called a function (this is called a cool function, and it is a bit like a virtual neighborhood ). It is to weaken the object feature of the model layer, degrade the class to a function-only container, reduce inheritance, and reload these applications.
* Second, implement a compilation engine.

The above two latest shopex485 have gone a long way, and the functions of goods and orders have been split. The second solution to PHP code parsing loss is to implement a parser named tramsy (flip (smart) + y), which features a large number of plug-ins changed to the compilation type. The compilation plug-in feature is enhanced, and a compilation modifier plug-in type is added. The variable pre-binding concept is also proposed:

 
  1. {if $var=1}
  2. yes
  3. {elseif $var=2}
  4. no
  5. {else}
  6. what?
  7. {/if}

If it is a native smarty, the generated code is:

 
  1. vars['var']==1){ ?>
  2. yes
  3. vars['var']==2){ ?>
  4. no
  5. what?

If the programmer predicts that var must be 1 in tramsy and is sure that the system automatically clears the template cache when its value changes, he can set it to "pre-bind variable"
The final generated code is:

No

This design reduces compilation results by more than doubled. The performance is improved by about 20%, greatly optimizing the PHP code parsing loss.


Parse found that the loss in PHP syntax parsing accounts for a large proportion. if valgrind is used to check his C call, it will be found that about 50% of the time is used on lexyacc ....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.