Edgewall Software

Changes between Version 5 and Version 6 of TracDev/Proposals/Jinja


Ignore:
Timestamp:
Feb 5, 2016, 2:09:00 AM (8 years ago)
Author:
Christian Boos
Comment:

at last, started to experiment!

Legend:

Unmodified
Added
Removed
Modified
  • TracDev/Proposals/Jinja

    v5 v6  
    11= Add Support for the Jinja2 Template Engine
    22
    3 We've decided some time ago to remove support for the ClearSilver template engine, in Trac 0.12, but this didn't happen yet. Clearsilver had its share of inconveniences, enough that we decided to switch to the nicer [http://genshi.edgewall.org/ Genshi template engine] in 0.12, but it was **very** fast and memory lenient. While we managed to keep Genshi memory usage somewhat in control (remember #6614?), the speed was never really adequate, especially for big changesets and for displaying source files over a few thousand lines of code (see TracDev/Performance#Genshi for details).
     3We've decided some time ago to remove the legacy support for the ClearSilver template engine, for Trac 1.0 (r10570). Clearsilver had its share of inconveniences, enough that we decided to switch to the nicer [http://genshi.edgewall.org/ Genshi template engine] in 0.11, but to be honest ClearSilver was **very** fast and memory lenient. While we managed to keep Genshi memory usage somewhat in control (remember #6614?), the speed was never really adequate, especially for big changesets and for displaying source files over a few thousand lines of code (see TracDev/Performance#Genshi for details).
    44
    55So one solution would be to switch once again, to a template engine that would combine the advantages of Genshi (pure Python, nice templates, flexible) and ClearSilver (speed!). Such a beast seems to exist now: **[http://jinja.pocoo.org/2/documentation/ Jinja2]**.
    66
    7 The switch can happen progressively: instead of removing support for ClearSilver we could substitute that code by the support for Jinja2.
    87
    98Several points remain to be clarified:
     
    1615[gmessage:trac-users:PYqQ4UDRnl8/wg8lQzrGDAAJ Genshi question] on Trac-Users.
    1716
     17== Experimenting with Jinja2 (2.8)
     18
     19Nothing like a few numbers to make a point ;-)
     20
     21These are the timings for rendering !r3871, with the diff options set to side-by-side, in place modifications, served by tracd on my development laptop. This generates a page weighing 11.5MB (Genshi) to 10.3MB (Jinja2) in size.
     22
     23||       ||||||||= Genshi                      ||||||||||||||||||||||||= Jinja2                                                                            ||
     24||       ||||= stream       ||||= blob         ||||= generate  ||||= stream (5) ||||= stream (10) ||||= stream (100) ||||= stream (1000) ||||= blob        ||
     25||       ||= 1st ||= 2nd    ||= 1st ||=   2nd  ||= 1st ||= 2nd ||= 1st ||= 2nd  ||= 1st ||= 2nd   ||= 1st ||= 2nd    ||= 1st ||= 2nd     ||= 1st ||=   2nd ||
     26||= TTFB || 16600||**15670**|| 25530||    24460||  2020||  1160||  2030||   1160||  2070||    1170||  2150|| **1230**||  2280||      1230||  3370||    2450||
     27||= CD   || 16090||**16050**||   387||     1240||  2820||  2720||  2730||   2640||  2730||    2680||  2470|| **2390**||  2350||      2250||   488||    1060||
     28|------------------------------------------------------------------------------------------------------------------------------------------------------------
     29||= Total|| 32690||    31720|| 25917||    25700||  4840||  3880||  4760||   3800||  4800||    3850||  4620||     3620||  4630||      3480||  3850||    3510||
     30||= Rdr  ||    --||       --|| 23533||**23273**||    --||    --||    --||     --||    --||      --||    --||       --||    --||        --||  1477||**1263**||
     31
     32Some explanations:
     33 - Genshi (0.7 with speedups)
     34   - ''stream'' means we return content via `Stream.serialize` and send chunks as we have them
     35   - ''blob'' means we first generate all the content in memory with `Stream.render`, then send it at once
     36 - Jinja2 (2.8 with [http://www.pocoo.org/projects/markupsafe/ speedups])
     37   - ''generate'' means we use `Template.generate` and send chunks as we have them
     38   - ''stream'' means we use the `TemplateBuffer` wrapper on the above, which groups a few chunks (given by the number in parenthesis) together before we send them;
     39     for a chunk size of **100**, we get the best compromise: still a very low TTFB and a reduced Content download time; actually the sweet spot is probably between
     40     10 and 100, and will most certainly depend on the actual content (I just tested 75 which gives 1160/2430 for example)
     41   - ''blob'' means we first generate all the content in memory with `Template.render`
     42 - both:
     43   - ''1st'' is the time in ms for the first request, sent right after a server restart
     44   - ''2nd'' is the time in ms for the second request, sent just after the first (usually the 3rd and subsequent requests would show the same results as this 2nd request)
     45
     46We measure:
     47 - TTFB (Time to first byte), as given by Chrome network panel in the developer window
     48 - CD (Content download), idem
     49 - Rdr (template rendering time), mostly significant for the "blob" method otherwise it also takes the network latency into account
     50
     51Note that even if the total "blob" time seems better than the total "stream" one, the lower TTFB is nevertheless a major benefit for the streaming variant, as this means the secondary requests can start earlier (and in this case, finish before the main request).
     52
     53In addition, while I didn't measure precisely the memory usage, Genshi made the python.exe process jump from 109MB to 239MB while rendering the request (blob). The memory seems to be freed afterwards (there were no concurrent requests). By contrast, with Jinja2 the memory spike was 106MB to 126MB.
     54
     55In summary, this means that for the big problematic pages, we can easily have a 10x speedup and more, by migrating to Jinja2,and this with a much lighter memory footprint.
     56For smaller pages, the speed-up is between 5x to 10x as well.
    1857
    1958
    20