code patches for speed
| From: | Paul Lockaby | Date: | Fri, 13 Jun 2003 02:40:32 +0000 |
| Subject: | code patches for speed | ||
| Groups: | php.smarty.dev | ||
| Request: | Send a blank email to php-smarty-dev+get-1569@lists.php.net to get a copy of this message | ||
Hi,
I was looking through Smarty over the past few weeks to see if there is some way that I could speed it up for my slow server and I noticed a few things that could help do that. So I've included them in this email.
First thing I noticed is that when comments are annihilated, they are actually left as {**}, which then has to be parsed and broken up into tags and read later on. Why not just remove them entirely from the source so that they don't have to be parsed through and seperated?
Second thing I noticed is that in the _compile_tag function, the regular expression pulls out the tag name, modifier, and argument and then passes the original tag unparsed to _parse_var_props and then on to _parse_var where it has the variable name, modifier and arguments pulled out again. Wouldn't it be faster to pass the tag name and modifier and arguments instead of pulling them out again? Parse all variables once using one preg_match_all instead of each variable using a preg_match?
When expanding quoted text, the function could probably be cut down a lot more with some implementation like this:
preg_match_all('/(?:[^\\\]' . $this->_dvar_regexp . ')/', substr($variable, 1, -1), $_expand);
$_expand = array_unique($_expand[0]);
foreach($_expand as $key => $value) {
$_expand[$key] = trim($value);
if (strpos($_expand[$key], '$') > 0) {
$_expand[$key] = substr($_expand[$key], strpos($_expand[$key], '$'));} } $_result = $variable; foreach($_expand as $value) { $value = trim($value); $_result = str_replace($value, '" . ' . $this->_parse_variable($value) . ' . "', $_result); } return $_result; Where $variable is the variable that you are parsing. Anyway, like I said, since I was interested in speed, I took a look through smarty and removed a whole ton of stuff that I never used and managed to make a script that works between 5 and 10 times faster than Smarty with force_compile = true and about twice as fast when using compiling. If you want to check it out, I put it up here http://www.paullockaby.com/templatey/ along with some brief documentation describing the changes I made. I hope that this is useful! -Paul -- Paul Lockaby paul@paullockaby.com http://www.paullockaby.com