• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
How to Create a High-Speed Template Engine in Python
 

How to Create a High-Speed Template Engine in Python

on

  • 13,712 views

I developed Tenjin which is the fastest template engine in Python world. In this presentation, I show you how to make template engine faster with a lot of benchmarks.

I developed Tenjin which is the fastest template engine in Python world. In this presentation, I show you how to make template engine faster with a lot of benchmarks.

Statistics

Views

Total Views
13,712
Views on SlideShare
9,977
Embed Views
3,735

Actions

Likes
9
Downloads
71
Comments
0

13 Embeds 3,735

http://kwatch.houkagoteatime.net 3265
http://aodag.posterous.com 232
https://jujo00obo2o234ungd3t8qjfcjrs3o6k-a-sites-opensocial.googleusercontent.com 137
http://jujo00obo2o234ungd3t8qjfcjrs3o6k-a-sites-opensocial.googleusercontent.com 45
http://paper.li 13
http://www.techgig.com 13
https://twitter.com 11
http://thinkery.me 5
http://webcache.googleusercontent.com 4
http://a0.twimg.com 3
https://twimg0-a.akamaihd.net 3
http://twitter.com 3
http://www.slashdocs.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    How to Create a High-Speed Template Engine in Python How to Create a High-Speed Template Engine in Python Presentation Transcript

    • PyconMini JP 2011How to Create a High-SpeedTemplate Engine in PythonPythonmakoto kuwatahttp://www.kuwata-lab.com/
    • Profile @makotokuwata http://www.kwuata-lab.com/ Ruby/PHP/Python programmer Creator of Erubis (*) Python4PHPer (*) default template engine on Rails 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
    • 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/
    • Benchmark Tenjin 2660.1 Mako 1426.4 Jinja2 1257.6 Templetor 903.0 Cheetah 562.3 Django 114.2 Genshi 55.7 Kid 34.6 0 600 1200 1800 2400 3000Python 2.5.5, MacOS X 10.6 (x86_64), 2GB pages/secTenjin 1.0.0, Mako 0.2.5, Jinja2 2.2.1, Templetor 0.32,Cheetah 2.2.2, Django 1.1.0, Genshi 0.5.1, Kid 0.9.6
    • Benchmarks forString Concatenation
    • append()
    • Benchmark pages/sec append() 0 200 400 600 800 1000
    • extend()
    • Benchmark pages/sec append() extend() 0 200 400 600 800 1000
    • StringIO
    • Benchmark pages/sec append() extend() StringIO 0 200 400 600 800 1000
    • mmap
    • Benchmark pages/sec append() extend() StringIO mmap 0 200 400 600 800 1000
    • Generator
    • Benchmark pages/sec append() extend() StringIO mmap generator 0 200 400 600 800 1000
    • Slice
    • Benchmark pages/sec append() extend() StringIO mmap generator slice[-1:] slice[99999:] 0 200 400 600 800 1000
    • Bound method
    • Benchmark pages/sec append() extend() StringIO mmap generator slice[-1:] slice[99999:]extend() (bound) 0 200 400 600 800 1000
    • SummaryFast bound method >= slice[] > extend()Slow Generator > append() > mmap > StringIO
    • Try Benchmark Script
    • Step by Step toTune-up Template Code
    • HTML Template
    • Python Code
    • Benchmark pages/sec append (singleline) 0 2000 4000 6000 8000 10000 12000
    • Multiple Line String Eliminates method call
    • Benchmark pages/sec append (singleline) append (multiline) 0 2000 4000 6000 8000 10000 12000
    • From append() to extend() Eliminates method call
    • Benchmark pages/sec append (singleline) append (multiline) extend (unbound) 0 2000 4000 6000 8000 10000 12000
    • Bound Method Eliminates fetch method
    • Benchmark pages/sec append (singleline) append (multiline) extend (unbound) extend (bound) 0 2000 4000 6000 8000 10000 12000
    • str() function Necessary in Python!
    • Benchmark pages/sec append (singleline) append (multiline) extend (unbound) extend (bound) extend + str 0 2000 4000 6000 8000 10000 12000
    • Local Variable Local var is faster than global/build-in var
    • Benchmark pages/sec append (singleline) append (multiline) extend (unbound) extend (bound) extend + str extend + _str=str 0 2000 4000 6000 8000 10000 12000
    • Format (% operator) Delete all str() call by % operator
    • Benchmark pages/sec append (singleline) append (multiline) extend (unbound) extend (bound) extend + str extend + _str=str append + format 0 2000 4000 6000 8000 10000 12000
    • None => Empty String Converts None to empty string
    • Benchmark pages/sec append (singleline) append (multiline) extend (unbound) extend (bound) extend + str extend + _str=str append + format extend + to_strextend + _to_str=to_str 0 2000 4000 6000 8000 10000 12000
    • Escape HTML
    • Benchmark pages/sec append (singleline) append (multiline) extend (unbound) extend (bound) extend + str extend + _str=str append + format extend + to_strextend + _to_str=to_str escape_html + str escape_html + to_str 0 2000 4000 6000 8000 10000 12000
    • C Extension Implemented in C webext: http://pypi.python.org/pypi/Webext/
    • Benchmark pages/sec append (singleline) append (multiline) extend (unbound) extend (bound) extend + str extend + _str=str append + format extend + to_str extend + _to_str=to_str escape_html + str escape_html + to_strwebext.escape_html, to_str webext.escape_html 0 2000 4000 6000 8000 10000 12000
    • Extreme join() Not escaped Be escaped if index % 2 == 0 if index % 2 == 1 (no need to call escape_html() !)
    • Benchmark Not implemeted yet...
    • SummaryString concatenation is not a bottleneck extend() & join() are enough fastBottleneck is str() and escape_html() join() should call str() internally C Extension (webext) is great
    • Other Topics
    • 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
    • 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
    • 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
    • Components of View Layer Just one of them TemplateImportant for Engine More Importantperformance! for performance! Helper Cache Functions Mechanism
    • Preprocessing in Tenjin Convert Execute Called everytime
    • Preprocessing in Tenjin Call function Convert in this stage Execute Func call removed
    • Python v.s. Others plTenjin (Perl) 12108.0pyTenjin+Webext 4179.7 he st ! phpTenjin (PHP) 2788.0 Pe rl i ion ha mppyTenjin (Python) 2682.9 C rbTenjin (Ruby) 2634.8 0 2500 5000 7500 10000 12500 pages/sec
    • Why Perl is so Fast? No need to call str(val) nor val.toString() Bytecode op for string concatenation
    • C Ext v.s. Pure Script plTenjin Pure Perl MobaSiF C ExtTemplate::Toolkit C ExtpyTenjin+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
    • SummaryView layer components Template engine, Helper functions, and Cache mechanismNo need to implement engine in C(except helper functions)Perl is greatDjango temlate engine sucks
    • 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/
    • Appendix C Ruby http://www.kuwata-lab.com/presen/rubykaigi2007.pdf http://jp.rubyist.net/magazine/?0022-FasterThanC Java LL http://www.kuwata-lab.com/presen/LL2007LT.pdf http://jp.rubyist.net/magazine/?0024-TemplateSystem http://jp.rubyist.net/magazine/?0024-TemplateSystem2
    • thank you