librelist archives

« back to archive

optimizing jinja2 render

optimizing jinja2 render

From:
Jay Baker
Date:
2012-11-09 @ 00:35
Hi all. I'm doing some work to optimize some template rendering. Through
other analysis I am positive that I don't have any extraneous db calls or
anything like that hidden in the rendering.

What are some best practices to isolate what might be going slower than I
would like?

As background, what I am seeing is this page, taking somewhere between
200-300ms rendering.
http://www.galtsgulchonline.com

And again, I am not counting any of the other processing to service the
request.

Thanks for any input.

-- Jay

Re: [flask] optimizing jinja2 render

From:
Xuanyi Chew
Date:
2012-11-09 @ 00:41
If you are concerned with speed, you can look into using Tenjin to render
instead.

cheers

Xuanyi Chew
+61403928398





On Fri, Nov 9, 2012 at 11:35 AM, Jay Baker <jbaker.work@gmail.com> wrote:

> Hi all. I'm doing some work to optimize some template rendering. Through
> other analysis I am positive that I don't have any extraneous db calls or
> anything like that hidden in the rendering.
>
> What are some best practices to isolate what might be going slower than I
> would like?
>
> As background, what I am seeing is this page, taking somewhere between
> 200-300ms rendering.
> http://www.galtsgulchonline.com
>
> And again, I am not counting any of the other processing to service the
> request.
>
> Thanks for any input.
>
> -- Jay
>

Re: [flask] optimizing jinja2 render

From:
Freedom Dumlao
Date:
2012-11-09 @ 02:48
Not having seen the code itself, my first instincts are:

1. Look at your loops, and especially your nested loops. Is there a way to
flatten out the data to avoid them? Should they be inverted?
2. How many includes are there? Includes are going to read files from disk
if they aren't loaded yet.
3. Caching goes a very long way. Cache what you can.

Besides that, you might want to consider profiling the template rendering
straight out. There's an example here that might help:

https://bitbucket.org/mitsuhiko/jinja2-main/src/db198f8eb440/examples/profile.py?at=trunk

Good luck, we've all been there,

Freedom


On Thu, Nov 8, 2012 at 7:35 PM, Jay Baker <jbaker.work@gmail.com> wrote:

> Hi all. I'm doing some work to optimize some template rendering. Through
> other analysis I am positive that I don't have any extraneous db calls or
> anything like that hidden in the rendering.
>
> What are some best practices to isolate what might be going slower than I
> would like?
>
> As background, what I am seeing is this page, taking somewhere between
> 200-300ms rendering.
> http://www.galtsgulchonline.com
>
> And again, I am not counting any of the other processing to service the
> request.
>
> Thanks for any input.
>
> -- Jay
>

Re: [flask] optimizing jinja2 render

From:
Gabriel Jordão
Date:
2012-11-09 @ 14:34
I have some problems with jinja templates too. Somethings I found:

Jinja's cache uses a LRU cache, sized by 50 by default. In my case, where I
have lots of templates, it slows things down sometimes.
Flask doesn't provide a "beautiful" way to change this size (in Jinja, if
-1 is passed in options, it changes the LRU to a dict, and cache
everything), but, as we don't have this option explicit in Flask, one way
is to change the cache directly, by setting app.jinja_env.cache = {}

Another problem I've noticed is about url_for. There are some pages that
take almost 50 ms just with url_for (total time). I don't know if there's
something "weird" with my application, but you should take a look at it too.

To keep track of it, a good thing you can do is to measure every request,
and display render time, in a debug toolbar, for instance, so that you can
keep track of how long things are taking.


On Fri, Nov 9, 2012 at 12:48 AM, Freedom Dumlao <freedomdumlao@gmail.com>wrote:

> Not having seen the code itself, my first instincts are:
>
> 1. Look at your loops, and especially your nested loops. Is there a way to
> flatten out the data to avoid them? Should they be inverted?
> 2. How many includes are there? Includes are going to read files from disk
> if they aren't loaded yet.
> 3. Caching goes a very long way. Cache what you can.
>
> Besides that, you might want to consider profiling the template rendering
> straight out. There's an example here that might help:
>
> 
https://bitbucket.org/mitsuhiko/jinja2-main/src/db198f8eb440/examples/profile.py?at=trunk
>
> Good luck, we've all been there,
>
> Freedom
>
>
> On Thu, Nov 8, 2012 at 7:35 PM, Jay Baker <jbaker.work@gmail.com> wrote:
>
>> Hi all. I'm doing some work to optimize some template rendering. Through
>> other analysis I am positive that I don't have any extraneous db calls or
>> anything like that hidden in the rendering.
>>
>> What are some best practices to isolate what might be going slower than I
>> would like?
>>
>> As background, what I am seeing is this page, taking somewhere between
>> 200-300ms rendering.
>> http://www.galtsgulchonline.com
>>
>> And again, I am not counting any of the other processing to service the
>> request.
>>
>> Thanks for any input.
>>
>> -- Jay
>>
>
>


-- 
*
*
*Gabriel Jordão*

T (71) 3494-3514
C (71) 8780-0519
gabrielpjordao@jusbrasil.com.br
www.jusbrasil.com.br