optimizer.py
This file
Path
src/jinja2/optimizer.py
47 lines · 1.6 KiB · Python
Last changed by
Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
History of this fileTake it
The walk stopped after 200 commits: lines older than d312609 point at it.
isolate bytecode cache testsDavid Lordbefore adoptiond312609
lines 1–9why, one step back1"""The optimizer tries to constant fold expressions and modify the AST2in place so that it should be faster to evaluate.34Because the AST does not contain all the scoping information and the5compiler has to find that out, we cannot do all the optimizations we6want. For example, loop unrolling doesn't work because unrolled loops7would have a different scope. The solution would be a second syntax tree8that stored the scoping rules.9"""Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 10–11why, one step back10import typing as t11isolate bytecode cache testsDavid Lordbefore adoptiond312609
lines 12–14why, one step back12from . import nodes13from .visitor import NodeTransformer14Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 15–16why, one step back15if t.TYPE_CHECKING:16 from .environment import Environmentisolate bytecode cache testsDavid Lordbefore adoptiond312609
line 17why, one step back17Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 18–19why, one step back1819def optimize(node: nodes.Node, environment: "Environment") -> nodes.Node:isolate bytecode cache testsDavid Lordbefore adoptiond312609
lines 20–22why, one step back20 """The context hint can be used to perform an static optimization21 based on the context given."""22 optimizer = Optimizer(environment)Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 23why, one step back23 return t.cast(nodes.Node, optimizer.visit(node))isolate bytecode cache testsDavid Lordbefore adoptiond312609
lines 24–26why, one step back242526class Optimizer(NodeTransformer):Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 27why, one step back27 def __init__(self, environment: "t.Optional[Environment]") -> None:isolate bytecode cache testsDavid Lordbefore adoptiond312609
lines 28–29why, one step back28 self.environment = environment29Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 30–32why, one step back30 def generic_visit(31 self, node: nodes.Node, *args: t.Any, **kwargs: t.Any32 ) -> nodes.Node:Merge pull request #1136 from pallets/drop-python2David Lordbefore adoption3915eb5
line 33why, one step back33 node = super().generic_visit(node, *args, **kwargs)isolate bytecode cache testsDavid Lordbefore adoptiond312609
lines 34–47why, one step back3435 # Do constant folding. Some other nodes besides Expr have36 # as_const, but folding them causes errors later on.37 if isinstance(node, nodes.Expr):38 try:39 return nodes.Const.from_untrusted(40 node.as_const(args[0] if args else None),41 lineno=node.lineno,42 environment=self.environment,43 )44 except nodes.Impossible:45 pass4647 return node