Optimize simple cases such as 'if True'

This commit is contained in:
Ryan Gonzalez 2015-09-22 12:17:47 -05:00
parent 21bbab4e09
commit c3190ca07d

View File

@ -1002,8 +1002,8 @@ class HyASTCompiler(object):
def compile_if(self, expression): def compile_if(self, expression):
expression.pop(0) expression.pop(0)
cond = self.compile(expression.pop(0)) cond = self.compile(expression.pop(0))
body = self.compile(expression.pop(0)) body = self.compile(expression.pop(0))
orel = Result() orel = Result()
nested = root = False nested = root = False
if expression: if expression:
@ -1016,6 +1016,28 @@ class HyASTCompiler(object):
self.temp_if = self.temp_if or self.get_anon_var() self.temp_if = self.temp_if or self.get_anon_var()
orel = self.compile(orel_expr) orel = self.compile(orel_expr)
if not cond.stmts and isinstance(cond.force_expr, ast.Name):
name = cond.force_expr.id
branch = None
if name == 'True':
branch = body
elif name in ('False', 'None'):
branch = orel
if branch is not None:
if self.temp_if and branch.stmts:
name = ast.Name(id=ast_str(self.temp_if),
arg=ast_str(self.temp_if),
ctx=ast.Store(),
lineno=expression.start_line,
col_offset=expression.start_column)
branch += ast.Assign(targets=[name],
value=body.force_expr,
lineno=expression.start_line,
col_offset=expression.start_column)
return branch
# We want to hoist the statements from the condition # We want to hoist the statements from the condition
ret = cond ret = cond