Skip to content
Myr

removed loop unrolling. for such optimizations we would need a separate syntax tree as far as i can see

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent 4f7d2d5 · Browse files · 1 file+9 added−87 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+9 added−87 removed

  • other:+9 added−87 removed

1 file+9 added−87 removed

modified:jinja2/optimizer.py+9 added-87 removed
1 unmodified line
2"""3 jinja2.optimizer4 ~~~~~~~~~~~~~~~~56 This module tries to optimize template trees by:6 The jinja optimizer is currently trying to constant fold a few expressions7 and modify the AST in place so that it should be easier to evaluate it.88 * eliminating constant nodes9 * evaluating filters and macros on constant nodes10 * unroll loops on constant values11 * replace variables which are already known (because they doesn't12 change often and you want to prerender a template) with constants9 Because the AST does not contain all the scoping information and the10 compiler has to find that out, we cannot do all the optimizations we11 want. For example loop unrolling doesn't work because unrolled loops would12 have a different scoping.1314 After the optimation you will get a new, simplier template which can15 be saved again for later rendering. But even if you don't want to16 prerender a template, this module might speed up your templates a bit17 if you are using a lot of constants.14 The solution would be a second syntax tree that has the scoping rules stored.1516 :copyright: Copyright 2008 by Christoph Hack, Armin Ronacher.17 :license: GNU GPL.18"""
1 unmodified line
20from jinja2.visitor import NodeVisitor, NodeTransformer21from jinja2.runtime import LoopContext222327# TODO28# - function calls to contant objects are not properly evaluated if the29# function is not representable at constant type. eg:30# {% for item in range(10) %} doesn't become31# for l_item in xrange(10: even though it would be possible32# - multiple Output() nodes should be concatenated into one node.33# for example the i18n system could output such nodes:34# "foo{% trans %}bar{% endtrans %}blah"35# - when unrolling loops local sets become global sets :-/36# see also failing test case `test_localset` in test_various373824def optimize(node, environment, context_hint=None):25 """The context hint can be used to perform an static optimization26 based on the context given."""27 optimizer = Optimizer(environment)
56 unmodified lines
8485 def visit_Block(self, node, context):86 return self.generic_visit(node, context.blank())87103 def visit_Macro(self, node, context):88 def scoped_section(self, node, context):89 context.push()90 try:91 return self.generic_visit(node, context)92 finally:93 context.pop()94 visit_For = visit_Macro = scoped_section9596 def visit_FilterBlock(self, node, context):97 """Try to filter a block at compile time."""98 node = self.generic_visit(node, context)
19 unmodified lines
118 context.pop()119 const = nodes.Const(data, lineno=node.lineno)120 return nodes.Output([const], lineno=node.lineno)121136 def visit_For(self, node, context):137 """Loop unrolling for iterable constant values."""138 fallback = self.generic_visit(node.copy(), context)139 try:140 iterable = self.visit(node.iter, context).as_const()141 # we only unroll them if they have a length and are iterable142 iter(iterable)143 len(iterable)144 # we also don't want unrolling if macros are defined in it145 if node.find(nodes.Macro) is not None:146 raise TypeError()147 except (nodes.Impossible, TypeError):148 return fallback149150 context.push()151 result = []152 iterated = False153154 def assign(target, value):155 if isinstance(target, nodes.Name):156 context[target.name] = value157 elif isinstance(target, nodes.Tuple):158 try:159 value = tuple(value)160 except TypeError:161 raise nodes.Impossible()162 if len(target.items) != len(value):163 raise nodes.Impossible()164 for name, val in zip(target.items, value):165 assign(name, val)166 else:167 raise AssertionError('unexpected assignable node')168169 if node.test is not None:170 filtered_sequence = []171 for item in iterable:172 context.push()173 assign(node.target, item)174 try:175 rv = self.visit(node.test.copy(), context).as_const()176 except:177 return fallback178 context.pop()179 if rv:180 filtered_sequence.append(item)181 iterable = filtered_sequence182183 try:184 try:185 for item, loop in LoopContext(iterable, True):186 context['loop'] = loop.make_static()187 assign(node.target, item)188 for n in node.body:189 result.extend(self.visit_list(n.copy(), context))190 iterated = True191 if not iterated and node.else_:192 for n in node.else_:193 result.extend(self.visit_list(n.copy(), context))194 except nodes.Impossible:195 return node196 finally:197 context.pop()198 return result199122 def visit_If(self, node, context):123 try:124 val = self.visit(node.test, context).as_const()125 except nodes.Impossible:
65 unmodified lines