Skip to content

Commit 9e93bd6

Browse files
committed
Freeze the parse result for the ripper translator
It's a small, somewhat hacky performance boost. Locations are lazy, by freezing the result they don't have to be pack/unpacked redundantly. This gives about a 4% speed boost. Other changes are to not modify the frozen AST
1 parent 3ad9db3 commit 9e93bd6

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

lib/prism/translation/ripper.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ def visit_begin_node(node)
10071007
on_stmts_add(on_stmts_new, on_void_stmt)
10081008
else
10091009
body = node.statements.body
1010-
body.unshift(nil) if void_stmt?(location, node.statements.body[0].location, allow_newline)
1010+
body = [nil, *body] if void_stmt?(location, node.statements.body[0].location, allow_newline)
10111011

10121012
bounds(node.statements.location)
10131013
visit_statements_node_body(body)
@@ -1024,7 +1024,7 @@ def visit_begin_node(node)
10241024
[nil]
10251025
else
10261026
body = else_clause_node.statements.body
1027-
body.unshift(nil) if void_stmt?(else_clause_node.else_keyword_loc, else_clause_node.statements.body[0].location, allow_newline)
1027+
body = [nil, *body] if void_stmt?(else_clause_node.else_keyword_loc, else_clause_node.statements.body[0].location, allow_newline)
10281028
body
10291029
end
10301030

@@ -1046,7 +1046,7 @@ def visit_begin_node(node)
10461046
on_bodystmt(visit_statements_node_body([nil]), nil, nil, nil)
10471047
when StatementsNode
10481048
body = [*node.body]
1049-
body.unshift(nil) if void_stmt?(location, body[0].location, allow_newline)
1049+
body = [nil, *body] if void_stmt?(location, body[0].location, allow_newline)
10501050
stmts = visit_statements_node_body(body)
10511051

10521052
bounds(node.body.first.location)
@@ -1095,7 +1095,7 @@ def visit_block_node(node)
10951095
braces ? stmts : on_bodystmt(stmts, nil, nil, nil)
10961096
when StatementsNode
10971097
stmts = node.body.body
1098-
stmts.unshift(nil) if void_stmt?(node.parameters&.location || node.opening_loc, node.body.location, false)
1098+
stmts = [nil, *stmts] if void_stmt?(node.parameters&.location || node.opening_loc, node.body.location, false)
10991099
stmts = visit_statements_node_body(stmts)
11001100

11011101
bounds(node.body.location)
@@ -2022,7 +2022,7 @@ def visit_else_node(node)
20222022
[nil]
20232023
else
20242024
body = node.statements.body
2025-
body.unshift(nil) if void_stmt?(node.else_keyword_loc, node.statements.body[0].location, false)
2025+
body = [nil, *body] if void_stmt?(node.else_keyword_loc, node.statements.body[0].location, false)
20262026
body
20272027
end
20282028

@@ -2077,7 +2077,7 @@ def visit_ensure_node(node)
20772077
[nil]
20782078
else
20792079
body = node.statements.body
2080-
body.unshift(nil) if void_stmt?(node.ensure_keyword_loc, body[0].location, false)
2080+
body = [nil, *body] if void_stmt?(node.ensure_keyword_loc, body[0].location, false)
20812081
body
20822082
end
20832083

@@ -2860,7 +2860,7 @@ def visit_lambda_node(node)
28602860
braces ? stmts : on_bodystmt(stmts, nil, nil, nil)
28612861
when StatementsNode
28622862
stmts = node.body.body
2863-
stmts.unshift(nil) if void_stmt?(node.parameters&.location || node.opening_loc, node.body.location, false)
2863+
stmts = [nil, *stmts] if void_stmt?(node.parameters&.location || node.opening_loc, node.body.location, false)
28642864
stmts = visit_statements_node_body(stmts)
28652865

28662866
bounds(node.body.location)
@@ -3354,7 +3354,7 @@ def visit_pre_execution_node(node)
33543354
# The top-level program node.
33553355
def visit_program_node(node)
33563356
body = node.statements.body
3357-
body << nil if body.empty?
3357+
body = [nil] if body.empty?
33583358
statements = visit_statements_node_body(body)
33593359

33603360
bounds(node.location)
@@ -4070,7 +4070,7 @@ def visit_yield_node(node)
40704070

40714071
# Lazily initialize the parse result.
40724072
def result
4073-
@result ||= Prism.parse(source, partial_script: true, version: "current")
4073+
@result ||= Prism.parse(source, partial_script: true, version: "current", freeze: true)
40744074
end
40754075

40764076
def line_and_column_cache

0 commit comments

Comments
 (0)