在 PyPy 這邊看到 JIT 的重大進展:「Better JIT Support for Auto-Generated Python Code」。
他們在 Tornado 上重製出來效能問題,後面也都是用這個例子在測試:
If you render a big HTML template (example) using the Tornado templating engine, the template rendering is really not any faster than CPython.
看起來上的 workaround 是在撞到 trace limit 時標記起來,之後再遇到時就可以跳進 special mode,接著處理下去避免浪費掉之前處理過的 trace:
After we have hit the trace limit and no inlining has happened so far, we mark the outermost function as a source of huge traces. The next time we trace such a function, we do so in a special mode. In that mode, hitting the trace limit behaves differently: Instead of stopping the tracer and throwing away the trace produced so far, we will use the unfinished trace to produce machine code.
效能可以看到改善很多:
看起來這個概念有打算在 3.8 的時候放進去:
The work described in this post tiny bit experimental still, but we will release it as part of the upcoming 3.8 beta release, to get some more experience with it. Please grab a 3.8 release candidate, try it out and let us know your observations, good and bad!
Django 的 template engine 不怎麼快,用 Jinja2 可能是一個方法,但既有的 project 如果有遇到 template engine 的效能問題,也許也可以翻看看 PyPy 解得如何...