0

Is it possible speed up loops in template using Cython, PyPy or Numba?

{% for student in [p for p in people if p.student and p.age > 23] %}
  <li>{{ escape(student.name) }}</li>
{% end %}

My intent is try out if loops can made faster for template rendering purpose.

Something in Numba can be tried out like

def python_sum(y):
    N = len(y)
    x = y[0]
    for i in xrange(1,N):
        x += y[i]
    return x

numba_sum = autojit()(python_sum)
numba_sum.func_name = "numba_sum"

If tried to pass the template as a function, it fails.

File "C:\Python27\lib\site-packages\numba\dispatcher.py", line 123, in _compile_and_call assert not kws AssertionError ; error throws up when I call the template as function

  • File "C:\Python27\lib\site-packages\numba\dispatcher.py", line 123, in _compile_and_call assert not kws AssertionError ; error throws up when I call the template as function –  Jul 27 '14 at 07:35

1 Answers1

1

If you run the server in pypy, the templates will be sped up too. It's not feasible to use cython for templates because of the way template code is generated dynamically. I'm not as familiar with numba but from the error message it looks like it doesn't support the **kwargs construct which tornado templates use extensively. Cython has an option always_allow_keywords; if numba has something simpler you may be able to use it but if not it probably won't work.

Ben Darnell
  • 21,844
  • 3
  • 29
  • 50