Wednesday, March 12, 2008

Fastest pure python text-based templating?

Mako is a mature full-featured templating engine for python. It is also pure-python i.e. it has no C-compiled extensions, and amongst pure-python systems it is currently considered to be the fastest.

Evoque is a new lightweight templating engine for python, also pure-python and full-featured. Its simple implementation is based on the eval python builtin, and weighs in at less than 1K lines of code, compared to about 2.5K for Mako, or about 4K for Genshi. One of Evoque's distinguishing features is automatic input quoting that, to my knowledge, Mako does not do, at least not by default.

It is difficult to make sensical performance comparisons between systems -- never do such comparisons give the entire picture. I will try to highlight the specific performance results for Evoque in manual quoting mode (called evoque_mq when used this way) and those for Mako -- two systems that come close to being equivalent. The Evoque distribution contains the following three benchmarks:
  • subs - does only string substitutions
  • basic - does a variety of standard templating tasks such as looping, calling sub-templates, escaping of data
  • bigtable - a simple brute force generation of a 10 columns x 1000 rows table
These three benchmarks are run on two different platforms, as indicated below, under python 2.5.1 in each case. All times are in milliseconds (less is better).

OS X 10.3 on a 667 MHz G4 PowerBook
             subs    basic    bigtable
evoque_mq 0.27 2.26 409.71
mako 0.72 2.73 445.47
Ubuntu 7.04 on a 2.0 GHz Intel Celeron
             subs    basic    bigtable
evoque_mq 0.13 1.25 152.12
mako 0.38 1.28 142.53
Evoque is not always faster. It seems to be consistently faster for simpler templates, while Mako seems to be quicker on loop-intensive templates, such as the 1000 row table benchmark. This is probably because of additional work that Evoque has to do for the runtime evaluation of loop variables.

Labels: , , , ,

Evoque - managed eval-based templating

Released the first version 0.1 of the lightweight, capable and fast Evoque templating engine on Saturday 7 March -- see comp.lang.python.announce announcement.

Evoque's distinguishing factors are:
  • automatic input quoting, i.e. XSS protection. To my knowledge no other text-based templating system has this feature. This is achieved thanks to Qpy's quoted-string h8 class, although any other quoted-string class may be used instead.
  • a restricted execution mode, to be able to safely expose templates for editing to untrusted clients. To facilitate this, only python expressions are allowed, i.e. nothing is ever exec'ed.
  • small footprint -- under 1K lines of code -- yet full-featured, e.g. elaborate cache management, template inheritance, versatile template addressing.
  • a contender for the fastest pure python templating engine currently out there. See some comparative performance benchmarks -- in particular compared with Mako that is currently considered to be the fastest text-based templating system.
Home page for Evoque is: http://evoque.gizmojo.org/

Labels: , , , , ,