search
Homephp教程php手册如何高效优化PHP代码解析损耗

如何高效优化PHP代码解析损耗

Jun 13, 2016 am 11:09 AM
phpsuperiorcodeoptimizationDiscoverexisthowofprogrammerparsegrammarconductEfficient

程序员们在进行

发现用在PHP的语法解析上的损耗占了很大比重,如果用valgrind看他的C调用的话,就会发现大约50%的时间被用在lex&yacc上面。也就是由PHP代码转成opcode的部分。即PHP代码解析损耗。

这个方面PHP代码解析损耗的优化极限目标是: 一个访问只运行一个PHP文件,并且这个文件里不包含任何与这个流程无关的代码。

如何兼顾代码结构容易理解和性能是个挑战

我们的处理思路是,通过类似smarty的编译系统,将访问编译成一个个文件:因为shopex是mvc的结构,那么编译粒度就每个控制器的方法对应一个流程文件。

当控制器第一次调用时,通过一种方法监控流经的每个model-method,子过程等等,最后抽取剥离出来,加上公用的数据库连接函数,配置文件等等一起组合成一个单一的终极PHP文件。
至于缓存的更新基本就是版本的更新,每次升级的时候。touch一个cachestat文件的最后修改时间即可。

那么实现的挑战有两个:

* 一个叫model的函数化 (这样叫很酷,有点像虚的死神化) 。是弱化model层对象特性,让类退化为仅是函数的容器,减少继承,重载这些应用。
* 二是实现一个自己的编译引擎。

上面两条最新的shopex485已经走了很远了,商品和订单的函数都已经拆分了。第二个PHP代码解析损耗的解决办法是我们自己实现了一个叫tramsy的解析器( 翻转(smart)+y ),特点是把大量的插件改成了编译型。强化了编译插件的特性,增加了一种编译型modifier的插件类型。并且提出了变量预绑定的概念:

<ol class="dp-xml">
<li class="alt"><span><span>{if $</span><span class="attribute">var</span><span>=</span><span class="attribute-value">1</span><span>}  </span></span></li>
<li><span>yes  </span></li>
<li class="alt">
<span>{elseif $</span><span class="attribute">var</span><span>=</span><span class="attribute-value">2</span><span>}  </span>
</li>
<li><span>no  </span></li>
<li class="alt"><span>{else}  </span></li>
<li><span>what?  </span></li>
<li class="alt"><span>{/if} </span></li>
</ol>

如果是原生的smarty,生成的代码是:

<ol class="dp-xml">
<li class="alt"><span><span>vars['var']==1){ </span><span class="tag">?></span><span> </span></span></li>
<li><span>yes  </span></li>
<li class="alt">
<span>vars['var']==2){ </span><span class="tag">?></span><span> </span>
</li>
<li><span>no  </span></li>
<li class="alt"><span>what? </span></li>
</ol>

如果在tramsy里,程序员预测var一定是1,并且有把握在其值改变的时候系统自动清除模板缓存,就可以把他设置为”预绑定变量”
那么最终生成的代码就是:

no

这个设计大约减少了一倍多的编译结果。性能提升了大约20%,极大的优化了PHP代码解析损耗。


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software