Skip to content
Myr

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 file

Take 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 back
1"""The optimizer tries to constant fold expressions and modify the AST
2in 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 back
10import typing as t
11

isolate bytecode cache testsDavid Lordbefore adoptiond312609

lines 12–14why, one step back
12from . import nodes
13from .visitor import NodeTransformer14

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 15–16why, one step back
15if t.TYPE_CHECKING:
16 from .environment import Environment

isolate bytecode cache testsDavid Lordbefore adoptiond312609

line 17why, one step back
17

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 18–19why, one step back
18
19def optimize(node: nodes.Node, environment: "Environment") -> nodes.Node:

isolate bytecode cache testsDavid Lordbefore adoptiond312609

lines 20–22why, one step back
20 """The context hint can be used to perform an static optimization
21 based on the context given."""22 optimizer = Optimizer(environment)

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 23why, one step back
23 return t.cast(nodes.Node, optimizer.visit(node))

isolate bytecode cache testsDavid Lordbefore adoptiond312609

lines 24–26why, one step back
24
2526class Optimizer(NodeTransformer):

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 27why, one step back
27 def __init__(self, environment: "t.Optional[Environment]") -> None:

isolate bytecode cache testsDavid Lordbefore adoptiond312609

lines 28–29why, one step back
28 self.environment = environment
29

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 30–32why, one step back
30 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 back
33 node = super().generic_visit(node, *args, **kwargs)

isolate bytecode cache testsDavid Lordbefore adoptiond312609

lines 34–47why, one step back
34
35 # 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