← Back to team overview

launchpad-dev team mailing list archive

Re: announcing pybars - handlebars for Python

 

On Mon, Feb 20, 2012 at 3:05 PM, Robert Collins
<robertc@xxxxxxxxxxxxxxxxx> wrote:

> This should be (better | have the potential to be better) than the
> moustache implementation we have today, though I haven't yet profiled
> it - I wanted to get it out there rather than obsessing about it :).

tl;dr - renders in about 2/3 the time of pystache (and can almost
certainly be optimised heavily - e.g. using shared state for helpers
and partials, slots, removing the use of partial(), tuning the use of
scoped frames, dropping the recursive strlist grow... needs to be data
driven though :)

So, I've tried it on our bugtasklisting.mustache, vs pystache, using
timeit, on a 75 row buglisting..

#!/bin/bash
echo "pybars"
bin/py -m timeit -s 'from pybars import Compiler' -s 'compiler =
Compiler()' -s 'source =
file("/home/robertc/source/launchpad/lp-branches/working/lib/lp/bugs/templates/buglisting.mustache",
"rb").read().decode("utf8")' -s 'template = compiler.compile(source)'
-s 'import json' -s 'data = json.loads(file("profdata",
"rb").read().decode("utf8"))' 'unicode(template(data))'
echo "pystache"
bin/py -m timeit -s 'import pystache' -s 'source =
file("/home/robertc/source/launchpad/lp-branches/working/lib/lp/bugs/templates/buglisting.mustache",
"rb").read().decode("utf8")' -s 'import json' -s 'data =
json.loads(file("profdata", "rb").read().decode("utf8"))' -s 'render =
pystache.render' -s 'import pdb' 'render(source, data)'

=>

pybars
10 loops, best of 3: 21.6 msec per loop
pystache
10 loops, best of 3: 31.4 msec per loop


Note that there is a small issue in the template: AFAICT it only works
in *stache not handlebars - its definitely not in the mustache spec
(per http://mustache.github.com/mustache.5.html under 'Non-false
values').

profdata was obtained by saving https://bugs.launchpad.net/launchpad
to disk, and deleting everything before and after the mustache_model
key in the LP cache json blob.

=== modified file 'lib/lp/bugs/templates/buglisting.mustache'
--- lib/lp/bugs/templates/buglisting.mustache   2012-01-12 11:28:03 +0000
+++ lib/lp/bugs/templates/buglisting.mustache   2012-02-20 03:40:12 +0000
@@ -45,7 +45,7 @@
             {{/show_date_last_updated}}
             {{#show_assignee}}
                 <span class="sprite person field">
-                    {{#assignee}}Assignee: {{assignee}}{{/assignee}}
+                    {{#assignee}}Assignee: {{.}}{{/assignee}}
                     {{^assignee}}Assignee: None{{/assignee}}
                 </span>
             {{/show_assignee}}


Follow ups

References