Php native template engine performance optimization

Source: Internet
Author: User
I haven't written a blog for a long time. I'm coming soon today. Background: for a website developed based on YAF, the template engine uses native php and develops a widget function to meet the requirements. Every time a widget is called, a template rendering is triggered. The same widget is called dozens of times on the homepage. When you view the xhprof data, you can find the widget rendering template.

I haven't written a blog for a long time. I'm coming soon today. Background: for a website developed based on YAF, the template engine uses native php and develops a widget function to meet the requirements. Every time a widget is called, a template rendering is triggered. The same widget is called dozens of times on the homepage. When you view the xhprof data, you can find the widget rendering template.

I haven't written a blog for a long time. I'm coming soon today.

Background:

For a website developed based on YAF, the template engine uses native php and develops a widget function to meet the requirements. Every time a widget is called, a template rendering is triggered. On the home page of the website, the same widget is called dozens of times. When you view the xhprof data, it is found that the widget rendering Template takes a large amount of time and is mainly used to load the template file, since the template used by the same widget is the same, we hope to load the template only once to improve the execution efficiency. Yaf

The result of AB-n1000-c50 is 11.31qps (poor VM performance ).

Ideas:

The first thing that comes to mind is to read the file into the memory before the include file and put it in a static variable. Then, you can directly retrieve the file from the static variable when calling it again. But the problem is how to render the template file after it is put into the variable? The simplest and most crude method is to use eval, but it is too crude to use it.

Performance

Then I think php has a wrapper function. You can register a wrapper, such as mem, and read the template file into the memory. Then you can use include ('mem: // template engine path ') method to load, the rough code is as follows: http://leo108.com/pid-2015.asp

Class Ext_Wrapper {// static member variable that stores the template file content protected static $ _ fileArr = array (); protected $ _ pos; protected $ _ curFile; public function stream_open ($ path, $ mode, $ options, & $ opened_path) {$ path = substr ($ path, 5, strlen ($ path)-5 ); // determine whether the template file is already in the Variable. if it does not exist, read if (! Isset (self ::$ _ fileArr [$ path]) {self ::: _ fileArr [$ path] = file_get_contents ($ path );} $ this-> _ curFile = $ path; $ this-> _ pos = 0; return true;} public function stream_read ($ count) {// read data directly from static variables $ content = self: $ _ fileArr [$ this-> _ curFile]; $ ret = substr ($ content, $ this-> _ pos, $ count); $ this-> _ pos + = strlen ($ ret); return $ ret ;} // other method omitted} // register wrapperstream_register_wrapper ('mem ', 'ext _ Wrapper ');

The result of AB-n1000-c50 is 12.68qps.

Php native template engine performance optimization

Finally, I tried eval performance. The Code is as follows:

Yaf

Class Ext_View extends Yaf_View_Simple {private $ tmpPath; private $ tmpData = array (); private $ include; // The static variable protected static $ _ fileArr = array () used to save the template content (); public function display ($ tplFile, $ data = array () {$ this-> tmpPath = $ this-> getScriptPath (). '/'. $ tplFile; if (is_array ($ data) {$ this-> tmpData = array_merge ($ this-> tmpData, $ data);} unset ($ tplFile ); unset ($ data); extract ($ this-> tmpData , EXTR_OVERWRITE); // judge whether the template file is already in the Variable. if it does not exist, read if (! Isset (self ::$ _ fileArr [$ this-> tmpPath]) {self :: $ _ fileArr [$ this-> tmpPath] = file_get_contents ($ this-> tmpPath);} eval ('?> '. Self: $ _ fileArr [$ this-> tmpPath]);} // other Code omitted}

AB-n1000-c50 result is 15.07qps, scared urine, eval is really simple and effective http://leo108.com/pid-2015.asp

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.