Welcome to Geeklog, Anonymous Monday, September 09 2024 @ 05:05 pm EDT
Geeklog Forums
Faster template processing
Status: offline
remy
Forum User
Full Member
Registered: 06/09/03
Posts: 162
Location:Rotterdam & Bonn
In the template class, the variables are substituted with a preg_replace, while all values must be manipulated to avoid 'bogus stripping'.
Following example does the same with a preg_replace_callback and speeds up the processing AND is recursive too. There is no need to neither process nor copying of (huge) arrays of vars and values.
Well, that's tested in my case: having many, many vars.
$regex = '/\{([-\.\w\d_\[\]]+)\}/';
$callback = function($matches) { return $this->substitute($matches[1]); };
$text = $this->get_var($var);
$text = preg_replace_callback($regex, $callback, $text);
}
There is no step for PHP to translate a lot of regexes, just one callback.
One can avoid the recursion when 'substitute' is replaced by 'get_var'.
Following example does the same with a preg_replace_callback and speeds up the processing AND is recursive too. There is no need to neither process nor copying of (huge) arrays of vars and values.
Well, that's tested in my case: having many, many vars.
Text Formatted Code
protected function substitute($var) {$regex = '/\{([-\.\w\d_\[\]]+)\}/';
$callback = function($matches) { return $this->substitute($matches[1]); };
$text = $this->get_var($var);
$text = preg_replace_callback($regex, $callback, $text);
}
There is no step for PHP to translate a lot of regexes, just one callback.
One can avoid the recursion when 'substitute' is replaced by 'get_var'.
33
33
Quote
Status: offline
Laugh
Site Admin
Admin
Registered: 09/27/05
Posts: 1470
Location:Canada
Always interested in speeding up the template class. Are you able to update the template class with these changes? You could do a pull request on our repository: https://github.com/Geeklog-Core/geeklog
One of the Geeklog Core Developers.
One of the Geeklog Core Developers.
32
22
Quote
Status: offline
remy
Forum User
Full Member
Registered: 06/09/03
Posts: 162
Location:Rotterdam & Bonn
Don't know pull.
The described function is a direct replacement for the function slow_subst.
I'm focussed on recursion, and the caching in template.class is hindering recursion in the logic extension syntax; i.e. if, loop, while. Blocks processing is a headache too.
I'm still experimenting.
The described function is a direct replacement for the function slow_subst.
I'm focussed on recursion, and the caching in template.class is hindering recursion in the logic extension syntax; i.e. if, loop, while. Blocks processing is a headache too.
I'm still experimenting.
26
35
Quote
Status: offline
Laugh
Site Admin
Admin
Registered: 09/27/05
Posts: 1470
Location:Canada
Experiment away.
I use a lot of template variables and logic processing in my templates so any speed increase is excellent.
I will be working on Geeklog code again in September. If you have an updated template class by that point I can test and include it with the main code base, if it all checks out. Just remember we need to support PHP 5.6 (and higher).
One of the Geeklog Core Developers.
I use a lot of template variables and logic processing in my templates so any speed increase is excellent.
I will be working on Geeklog code again in September. If you have an updated template class by that point I can test and include it with the main code base, if it all checks out. Just remember we need to support PHP 5.6 (and higher).
One of the Geeklog Core Developers.
30
26
Quote
Status: offline
remy
Forum User
Full Member
Registered: 06/09/03
Posts: 162
Location:Rotterdam & Bonn
Finished parts of a new template system. Below a description.
It is definitely faster than the GL native template class, besides it handles some functionality better.
In order to do a benchmark it is discovered meanwhile that the GL template class is tight integrated with GL and cannot be tested stand alone. Unless I'm overlooking a trick?
Anyway, I've broken down the template processing into substituting and parsing (Snippet class), including files and nesting blocks (Templet class), adding logic (Template class), and caching (Stencil class). Current versions are exhibiting my needs, but it would be easy to add backward compatibility. The Stencil class is not yet ready, in the sense that it is not tested enough.
Extra's:
configurable recursion
added inserting global variables
added conditional orphans
added default variable {debugInfo}
added closures
I've posted the package SIREN (Snippet + Templet) in phpclasses.org. Have a look at
https://www.phpclasses.org/package/11637-PHP-Template-engine-featuring-recursion-and-nesting.html
It is definitely faster than the GL native template class, besides it handles some functionality better.
In order to do a benchmark it is discovered meanwhile that the GL template class is tight integrated with GL and cannot be tested stand alone. Unless I'm overlooking a trick?
Anyway, I've broken down the template processing into substituting and parsing (Snippet class), including files and nesting blocks (Templet class), adding logic (Template class), and caching (Stencil class). Current versions are exhibiting my needs, but it would be easy to add backward compatibility. The Stencil class is not yet ready, in the sense that it is not tested enough.
Extra's:
configurable recursion
added inserting global variables
added conditional orphans
added default variable {debugInfo}
added closures
I've posted the package SIREN (Snippet + Templet) in phpclasses.org. Have a look at
https://www.phpclasses.org/package/11637-PHP-Template-engine-featuring-recursion-and-nesting.html
34
23
Quote
Status: offline
remy
Forum User
Full Member
Registered: 06/09/03
Posts: 162
Location:Rotterdam & Bonn
I've done some benchmarking with the class Template on my site boxary.
The benchmark just gives an idea, it is not exact. Please note I'm using PHP 7 and GL 2.2.0.
Running boxary with the native Template class:
Simple page: remove cache, first hit: 0,11 secs, second hit 0,09 secs.
Complex page: remove cache, first hit 0,25 secs, second ht 0,24 secs.
Running boxary with the native Template class:
Simple page: no cache involved, first hit: 0,08 secs, second hit 0,07 secs.
Complex page: no-cache involved, first hit 0,31 secs, second ht 0,30 secs.
The timing of the simple pages is nearly constant.
The timing of the complex pages shows however not a constant behavior in both cases.
That could have to do with many SQL's and a lot of template pages with logic. I will investigate further.
I will publish my Template class at phpclasses.org next week.
The benchmark just gives an idea, it is not exact. Please note I'm using PHP 7 and GL 2.2.0.
Running boxary with the native Template class:
Simple page: remove cache, first hit: 0,11 secs, second hit 0,09 secs.
Complex page: remove cache, first hit 0,25 secs, second ht 0,24 secs.
Running boxary with the native Template class:
Simple page: no cache involved, first hit: 0,08 secs, second hit 0,07 secs.
Complex page: no-cache involved, first hit 0,31 secs, second ht 0,30 secs.
The timing of the simple pages is nearly constant.
The timing of the complex pages shows however not a constant behavior in both cases.
That could have to do with many SQL's and a lot of template pages with logic. I will investigate further.
I will publish my Template class at phpclasses.org next week.
23
25
Quote
Status: offline
remy
Forum User
Full Member
Registered: 06/09/03
Posts: 162
Location:Rotterdam & Bonn
The version GL 2.2.0 contains the feature to override the template class by adding a function called OVERRIDE_newTemplate(), which takes over the creation of the template objects when called thru COM_newTemaplte().
Of course, I have some extra methods I'm my class to support BC with the GL version of Template. class. But what about FC (Forward as opposed to Backward C.)?
It would be nice if the GL Template class could add a magic __CALL method to catch calls to nonexisting methods to forward to a utility function, say OVERRIDEN_methods_template.
Like:
function __call($method, $args) { return OVERRIDEN_methods_template($this, $method, $args); }
Doing so is pretty developer-friendly !!
Of course, I have some extra methods I'm my class to support BC with the GL version of Template. class. But what about FC (Forward as opposed to Backward C.)?
It would be nice if the GL Template class could add a magic __CALL method to catch calls to nonexisting methods to forward to a utility function, say OVERRIDEN_methods_template.
Like:
Text Formatted Code
function __call($method, $args) { return OVERRIDEN_methods_template($this, $method, $args); }
Doing so is pretty developer-friendly !!
32
19
Quote
Status: offline
remy
Forum User
Full Member
Registered: 06/09/03
Posts: 162
Location:Rotterdam & Bonn
Finally finished Stencil. class. Heavy testing and benchmarking point out the following:
Templet, which adds files and blocks, is about a factor 3 faster.
However, that's not an honest benchmark. glTemplate does much more. Though it becomes dramatically slower when the number of variables rises. The tipping point is about 80 variables.
Benchmarking with a few unit tests delivers:
noCache Caching
Stencil 0.01491... 0.00187...
Template 0.00251... 0.00249...
0.01240.... -0.00062... diff
glTemplate 0.00496... 0.00283...
0.00995... -0.00096... diff
-0.00740... -0.00317... average diff
The package is published on phpclasses.org: https://www.phpclasses.org/package/11965-PHP-Template-engine-compiling-to-native-PHP-and-caches.html
For the time being there are some (minor) differences with GL, but a plugin is in the making.
For comments, contributing or beta testing, please use the support forum of the package at phpclasses.org, since I get increasingly allergic to all the dislikes on the GL forums.
Text Formatted Code
Snippet, which does solely the parsing, is about a factor 4 fasterTemplet, which adds files and blocks, is about a factor 3 faster.
However, that's not an honest benchmark. glTemplate does much more. Though it becomes dramatically slower when the number of variables rises. The tipping point is about 80 variables.
Benchmarking with a few unit tests delivers:
Text Formatted Code
noCache Caching
Stencil 0.01491... 0.00187...
Template 0.00251... 0.00249...
0.01240.... -0.00062... diff
glTemplate 0.00496... 0.00283...
0.00995... -0.00096... diff
-0.00740... -0.00317... average diff
The package is published on phpclasses.org: https://www.phpclasses.org/package/11965-PHP-Template-engine-compiling-to-native-PHP-and-caches.html
For the time being there are some (minor) differences with GL, but a plugin is in the making.
For comments, contributing or beta testing, please use the support forum of the package at phpclasses.org, since I get increasingly allergic to all the dislikes on the GL forums.
21
25
Quote
Status: offline
Laugh
Site Admin
Admin
Registered: 09/27/05
Posts: 1470
Location:Canada
Interesting to see the benchmarking on your class.
Should add a feature request for:
One of the Geeklog Core Developers.
Should add a feature request for:
It would be nice if the GL Template class could add a magic __CALL method to catch calls to nonexisting methods to forward to a utility function, say OVERRIDEN_methods_template.
One of the Geeklog Core Developers.
26
17
Quote
All times are EDT. The time is now 05:05 pm.
- Normal Topic
- Sticky Topic
- Locked Topic
- New Post
- Sticky Topic W/ New Post
- Locked Topic W/ New Post
- View Anonymous Posts
- Able to post
- Filtered HTML Allowed
- Censored Content