This document summarizes Makoto Kuwata's presentation on creating a high-speed template engine in Python. It discusses Tenjin, a template engine created by Kuwata that is very fast. It benchmarks Tenjin against other template engines like Mako, Jinja2, and Django and shows that Tenjin is significantly faster. The presentation provides optimization techniques for template engines like using bound methods, local variables, and format strings instead of string concatenation. It also recommends implementing performance critical parts in a low-level language like C to achieve better speeds.
3. Python Products
Tenjin : very fast temlate engine
Kook : task utility like Ant/Rake
Benchmarker : a good friend for performance
Oktest : new-style testing library
4. Tenjin
Very fast
One file, 2000 lines
Full-featured
Python 3 support
Google App Engine
Release 1.0 coming soon!
http://www.kuwta-lab.com/tenjin/
47. Summary
String concatenation is not a bottleneck
extend() & join() are enough fast
Bottleneck is str() and escape_html()
join() should call str() internally
C Extension (webext) is great
49. Google says...
... The major web applications we
have surveyed have indicated that
they bottleneck primarily on
template systems, ...
Django?
http://code.google.com/p/unladen-swallow/wiki/ProjectPlan
50. Case Study #1
http://www.myweightracker.com/
Switch from Django template to Tenjin
M, C, Network, etc... Django
ed
M, C, Network, etc... Spe !
pp Up
A
30%
https://groups.google.com/group/kuwata-lab-products/
browse_thread/thread/b50877a9c56d64c9/60f77b5c9b9f5238
51. Case Study #2
Ruby on Rails 1.2
Remove helper methods by preprocessing
M, C, Network, etc... Helper Methods
template engine
ed
M, C, Network, etc... pp Spe !
A Up
1 00%
http://jp.rubyist.net/magazine/?0021-Erubis
52. Components of View Layer
Just one of them
Template
Important for Engine More Important
performance!
for performance!
Helper Cache
Functions Mechanism
55. Python v.s. Others
plTenjin (Perl) 12108.0
pyTenjin+Webext 4179.7
he
st !
phpTenjin (PHP) 2788.0
Pe rl i ion
ha mp
pyTenjin (Python) 2682.9 C
rbTenjin (Ruby) 2634.8
0 2500 5000 7500 10000 12500
pages/sec
56. Why Perl is so Fast?
No need to call str(val) nor val.toString()
Bytecode op for string concatenation
57. C Ext v.s. Pure Script
plTenjin Pure Perl
MobaSiF C Ext
Template::Toolkit C Ext
pyTenjin+Webext Python + C Ext
pyTenjin Pure Python
Cheetah C Ext No need to impl
engine in C
rbTenjin Pure Ruby (except helpers)
eruby C Ext
0 2500 5000 7500 10000 12500
pages/sec
58. Summary
View layer components
Template engine, Helper functions, and
Cache mechanism
No need to implement engine in C
(except helper functions)
Perl is great
Django temlate engine sucks
59. Appendix
Tenjin: fast & full-featured template engine
http://www.kuwata-lab.com/tenjin/
Webext: C extension for escape_html()
http://pypi.python.org/pypi/Webext/
Benchmarker: a utility for benchmarking
http://pypi.python.org/pypi/Benchmarker/