ranlt / smarty-php

Automatically exported from code.google.com/p/smarty-php
0 stars 0 forks source link

templatelexer.php and templateparser.php have to be micro-optimized. patch is attached. #221

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
-Use complex templates with many includes and variables. (86 compiled templates 
in my case)
-The compile time is about 18-19 sec
This is is not suitable even for the compile time.

What is the expected output? What do you see instead?
-The compile time is less then 10 sec
-5 sec is the very good time for 86 templates

What version of the product are you using? On what operating system?
Smarty 3.1.21
Linux 3.17.6-1-ARCH 

Please provide any additional information below.

The cachegrind.out file produced by x-debug shows that some methods are called 
over 5000 times
This is the good point for the php micro-optimization like
http://www.phpbench.com/
http://stackoverflow.com/questions/3470990/is-micro-optimization-worth-the-time
http://alexatnet.com/articles/php-micro-optimization-tips

This is the short list of these methods.
smarty3/sysplugins/smarty_internal_templateparser.php:    public function 
doParse was called 18582 times
smarty3/sysplugins/smarty_internal_templatelexer.php:    public function yylex 
was called 18582 times
smarty3/sysplugins/smarty_internal_templateparser.php:    public function 
yy_reduce was called 32637 times 
smarty3/sysplugins/smarty_internal_templatelexer.php:    public function yylex2 
was called 12634 times
and so on.

I have attached a simple patch which changes the compile time from 19 sec to 10 
sec (~50%  improvement) 
And it does not limit.

I suppose these micro-optimization can be done for the templatelexer.php and 
templateparser.php scripts.
double (") vs. single (') quotes
switch/case/default vs. if/elseif/else
foreach() vs. for vs. while(list() = each())
array_filter($yymatches, 'strlen') vs array_filter($yymatches)
!count($yymatches) vs empty($yymatches)
is_array($array) vs $array === (array) $array
cache function result in a static variable

may be something else.

Original issue reported on code.google.com by a...@x-cart.com on 15 Jan 2015 at 3:36

Attachments:

GoogleCodeExporter commented 9 years ago
My statistics for the just created templates_c directory
aim-server[../templates_c]$ find . -type f|wc
     91      91    9239
aim-server[../templates_c]$ grep -r  tpl_vars|wc
   1679   12063  400836
aim-server[../templates_c]$ grep -r echo|wc
   1097    8450  282738
aim-server[../templates_c]$ grep -r is_callable|wc
    129    1182   39121
aim-server[../templates_c]$ grep -rw section|wc
    123     664   34069
aim-server[../templates_c]$ grep -rw foreach|wc
     62     336   14058
aim-server[../templates_c]$ grep -r smarty_modifier|wc
    136    2204   44611
aim-server[../templates_c]$ grep -r smarty_function|wc
    168    1036   48323
aim-server[../templates_c]$ grep -r getSubTemplate|wc
    259    2825   80745

Top 10 files by size
 25742 Jan 15 18:10 64f660e1d9498b0761469715501a4f370ff9329b.file.buy_now.tpl.php
 21884 Jan 15 18:10 7c5c7be9dabdaddd6c1ea526ea2defc15f09ad29.file.service_js.tpl.php
 19341 Jan 15 18:10 3fb81a89a96a0403c9ff11ee6919a5d253740cd3.file.products_t.tpl.php
 18461 Jan 15 18:10 2b3f4b3016cbcfe90b3e2fe5ed1875447bcf306e.file.home_main.tpl.php
 16810 Jan 15 18:10 0a61e81c47b2a9edab979ac46161395ce54228ef.file.on_sale_icon.tpl.php
 15801 Jan 15 18:10 82c8ce22612b67ea145e185938944dfbe5530f81.file.button.tpl.php
 11662 Jan 15 18:10 92bf62f2aa597b80463853b3b94735e01c34f913.file.vote_bar.tpl.php
 10006 Jan 15 18:10 aaabbc8566e55a33a7847b6d1301478c7ee6e049.file.language_selector.tpl.php
  9961 Jan 15 18:10 17869a18dd63d947fe1f720d08b9a493598a7401.file.content.tpl.php
  9574 Jan 15 18:10 5b55a39758161e8cf39d7b2fdfcad25ab6eed114.file.customer_reviews_menu.tpl.php
  9197 Jan 15 18:10 36ff345031cb4f63fc4d060ffc6415593995bf56.file.service_head.tpl.php
  9193 Jan 15 18:10 a0f00b8af06fc38e99ec01d1e0c6acad3d92fc97.file.survey.tpl.php
  7756 Jan 15 18:10 e7aefa970327d4ba71b5b397d3b3a89f6560f200.file.spambot_arrest.tpl.php
  7589 Jan 15 18:10 3ab0a6bb2fc56817bc15a00f7a9c0970a971ab48.file.left_bar.tpl.php
  7462 Jan 15 18:10 5b8c6e03eff0559a83423a17fc05f570d62028b0.file.welcome.tpl.php

Original comment by a...@x-cart.com on 15 Jan 2015 at 3:37

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
These variables are not used

include/lib/smarty3/sysplugins/smarty_internal_configfileparser.php:        
$yymsp = $this->yystack[$this->yyidx];
include/lib/smarty3/sysplugins/smarty_internal_templateparser.php:        
$yymsp = $this->yystack[$this->yyidx];

The affected method is yy_reduce() which is called 39342 times in my case.

The time cost for this bug is about 1-2% !

Original comment by a...@x-cart.com on 16 Jan 2015 at 7:33

GoogleCodeExporter commented 9 years ago
The same bug 

Index: include/lib/smarty3/sysplugins/smarty_internal_templateparser.php
--- include/lib/smarty3/sysplugins/smarty_internal_templateparser.php
+++ include/lib/smarty3/sysplugins/smarty_internal_templateparser.php
@@ -4421,7 +4421,6 @@ class Smarty_Internal_Templateparser#line 80 
"smarty_internal_templateparser.php
         $this->_retvalue = $yy_lefthand_side = null;
         if (isset(self::$yyReduceMap[$yyruleno]) || array_key_exists($yyruleno, self::$yyReduceMap)) {
             // call the action
-            $this->_retvalue = null;
             $this->{'yy_r' . self::$yyReduceMap[$yyruleno]}();
             $yy_lefthand_side = $this->_retvalue;
         }

Original comment by a...@x-cart.com on 16 Jan 2015 at 8:09

GoogleCodeExporter commented 9 years ago
The attached patch 
cached_yy_is_expected_token_n_other_micro.diff

includes 
cached_yy_is_expected_token_n_isset_vs_array_key_exists.diff
and the changes from the last comments

Original comment by a...@x-cart.com on 16 Jan 2015 at 9:21

Attachments: