• Could not connect to Twitter

Optimising Genshi Imports

We’ve just moved from Kid templates to Genshi (v0.4). Kyran’s first approach was to put all the helper div-functions into one of two separate (big) Genshi files to simplify the translation process.

We had several of these files, each about 10k in size, each imported into every main Genshi template.

This approach made it easy for Kyran to move us to Genshi but made for slower-than-desirable render times. Whilst speed-testing the pages I noted that the several 10k Genshi imports per page was the real burden.

The solution was to extract the necessary functions (div-blocks) from the helper files, put each of them into their own short Genshi file, and import only the necessary helper functions.

Now our main Genshi files need only import 3-10 helper functions (each file is about 10-30 lines long) rather than importing the several 10k files.

The speed-testing shows that each page’s imports took around 2 seconds per page before this process and 0.34 seconds after. The imports often dominated our render-times so this 6* speed-up is a welcome improvement.

For those who are new to optimisation we’re following the classical approach:

  1. Make measurements to identify the real bottleneck (very important!)
  2. Make changes one at a time to test their effect
  3. Keep the beneficial changes
Please share:
  • DZone
  • del.icio.us
  • Reddit
  • Furl
  • Ma.gnolia
  • email
  • StumbleUpon
  • Technorati
  • TwitThis
  • Slashdot
If you enjoyed this post, make sure you subscribe to my RSS feed!

Related posts:

  1. Speed-testing Genshi pages for optimisation
  2. Productive in 10 minutes with TurboGears and Python 2.5
  3. Using non-Python files with py2exe
  4. Simple easygui File Dialogs
  5. A proposal for a “Python Tutorial Series” – what should be included?

2 comments to Optimising Genshi Imports

  • So are you noticing big speed-ups versus Kid or is it much the same? I found kid dog slow for a few things and switched to Cheetah, but it’s syntax can sometimes make things look a bit messy. If Genshi is faster than Kid and what TurboGears will be moving to in the future, I’d be tempted to switch to it.

  • Rendering times are a bit faster but it looks like our main slow-down is the number of database calls we make (that’s our next job).
    The big improvement is readability – the new templates are super-easy to follow (Kid was pretty hard, I’ve not looked at Cheetah).
    It was real easy to embed this timing code even though I hadn’t writen the templates (that was Kyran’s job).
    ‘Fraid I’ve no idea if Cheetah is faster/slower than Genshi…