]> Devi Nivas Git - chevron.git/commitdiff
cache tokens for lambdas for speed
authorKay-Uwe (Kiwi) Lorenz <kiwi@franka.dyndns.org>
Sun, 29 Jul 2018 06:02:57 +0000 (08:02 +0200)
committerKay-Uwe (Kiwi) Lorenz <kiwi@franka.dyndns.org>
Sun, 29 Jul 2018 06:02:57 +0000 (08:02 +0200)
chevron/renderer.py

index bcd4f3c6a60c03e252e4105c9026a772e3326e4c..15f4460a6fac3886a407a380ccd7e36ccbbbe019 100644 (file)
@@ -95,6 +95,7 @@ def _get_partial(name, partials_dict, partials_path, partials_ext):
 #
 # The main rendering function
 #
+g_token_cache = {}
 
 def render(template='', data={}, partials_path='.', partials_ext='mustache',
            partials_dict={}, padding=0, def_ldel='{{', def_rdel='}}',
@@ -156,8 +157,11 @@ def render(template='', data={}, partials_path='.', partials_ext='mustache',
         # But it does need to be a generator
         tokens = (token for token in template)
     else:
-        # Otherwise make a generator
-        tokens = tokenize(template, def_ldel, def_rdel)
+        if template in g_token_cache:
+            tokens = (token for token in g_token_cache[template])
+        else:
+            # Otherwise make a generator
+            tokens = tokenize(template, def_ldel, def_rdel)
 
     output = unicode('', 'utf-8')
 
@@ -220,10 +224,12 @@ def render(template='', data={}, partials_path='.', partials_ext='mustache',
 
                 # Generate template text from tags
                 text = unicode('', 'utf-8')
+                tags = []
                 for tag in tokens:
                     if tag == ('end', key):
                         break
 
+                    tags.append(tag)
                     tag_type, tag_key = tag
                     if tag_type == 'literal':
                         text += tag_key
@@ -240,6 +246,8 @@ def render(template='', data={}, partials_path='.', partials_ext='mustache',
                                 'no escape': '&'
                             }[tag_type], tag_key, def_rdel)
 
+                g_token_cache[text] = tags
+
                 rend = scope(text, lambda template, data=None: render(template,
                              data={},
                              partials_path=partials_path,