Choosing a Templating System
by Perrin Harkins
|
Pages: 1, 2, 3, 4
Performance
People always seem to worry about the performance of templating systems. If you've ever built a large-scale application, you should have enough perspective on the relative costs of different actions to know that your templating system is not the first place to look for performance gains. All of the systems mentioned here have excellent performance characteristics in persistent execution environments like mod_perl. Compared to such glacially slow operations as fetching data from a database or file, the time added by the templating system is almost negligible.
If you think your templating system is slowing you down, get the facts: pull out Devel::DProf and see. If one of the tools mentioned here is at the top of the list for wall clock time used, you should pat yourself on the back -- you've done a great job tuning your system and removing bottlenecks! Personally, I have only seen this happen when I had managed to successfully cache nearly every part of the work to handle a request except running a template.
However, if you really are in a situation where you need to squeeze a
few extra microseconds out of your page generation time, there are
performance differences between systems. They're pretty much what you
would expect: systems that do the least run the fastest. Using
in-line print() statements is faster than using templates. Using
simple substitution is faster than using in-line Perl code. Using
in-line Perl code is faster than using a mini-language.
The only templating benchmark available at this time is one developed by Joshua Chamas, author of Apache::ASP. It includes a ``hello world'' test, which simply checks how fast each system can spit back those famous words, and a ``hello 2000'' test, which exercises the basic functions used in most dynamic pages. It is available from the following URL:
http://www.chamas.com/bench/hello.tar.gz
Results from this benchmark currently show SSI, Apache::ASP and HTML::Embperl having the best performance. Not all of the systems mentioned here are currently included in the test. If your favorite was missed, you might want to download the benchmark code and add it. As you can imagine, benchmarking people's pet projects is largely a thankless task and Joshua deserves some recognition and support for this contribution to the community.
CGI Performance Concerns
If you're running under CGI, you have bigger fish to fry than worrying about the performance of your templating system. Nevertheless, some people are stuck with CGI but still want to use a templating system with reasonable performance. CGI is a tricky situation, since you have to worry about how much time it will take for Perl to compile the code for a large templating system on each request. CGI also breaks the in-memory caching of templates used by most of these systems, although the slower disk-based caching provided by Mason, HTML::Template and Template Toolkit will still work. (HTML::Template does provide a shared memory cache for templates, which may improve performance, although shared memory on my Linux system is usually slower than using the filesystem. Benchmarks and additional information are welcome.)
Your best performance bet with CGI is to use one of the simpler tools, like CGI::FastTemplate or Text::Template. They are small and compile quickly, and CGI::FastTemplate gets an extra boost since it relies on simple regex parsing and doesn't need to eval any in-line Perl code. Almost everything else mentioned here will add tenths of seconds to each page in compilation time alone.
Matrix
To help you choose a system, I'll summarize the basic characteristics of the major systems along the decision points I've explained in the beginning of the article. Keep in mind that in many cases a system can be used in more than one way, and I've simply shown the dominant method as seen in the documentation and real world use. You should not eliminate options based on this chart without reading the more detailed explanations above.
| Application Framework | Pipeline or Callback | Parsing Method | Language | |
|---|---|---|---|---|
| HTML::Mason | Framework | Callback | Compiled | Perl |
| Template Toolkit | Just Templates | Pipeline | Compiled | Mini-Language |
| Apache::ASP | Framework | Callback | Compiled | Perl and XSL |
| HTML::Embperl | Framework | Callback | Compiled | Perl |
| SSI | Just Templates | Callback | Repeated Parse | Mini-Language |
| AxKit | Framework | Pipeline | Compiled or Cached Parse Tree | Perl and XSL and Mini-Language(s) |
| HTML::Template | Just Templates | Pipeline | Cached Parse Tree | Mini-Language |
| Text::Template | Just Templates | Pipeline | Compiled | Perl |
Updates
These modules are moving targets, and a document like this is bound to contain some mistakes. Send your corrections to perrin@elem.com. Future versions of this document will be announced on the mod_perl mailing list, and possibly other popular Perl locations.

