Skip to content

Commit 3d4c7c3

Browse files
Earlopainmatzbot
authored andcommitted
[ruby/prism] Use reverse_each in the parser translator
Avoids an array allocation which matters more and more the larger the file is. I have it at 14% of runtime. ruby/prism@f65b90f27d
1 parent 67e6ccb commit 3d4c7c3

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

lib/prism/translation/parser/lexer.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,11 @@ def to_a
261261

262262
case type
263263
when :kDO
264-
types = tokens.map(&:first)
265-
nearest_lambda_token_type = types.reverse.find { |type| LAMBDA_TOKEN_TYPES.include?(type) }
264+
nearest_lambda_token = tokens.reverse_each.find do |token|
265+
LAMBDA_TOKEN_TYPES.include?(token.first)
266+
end
266267

267-
if nearest_lambda_token_type == :tLAMBDA
268+
if nearest_lambda_token&.first == :tLAMBDA
268269
type = :kDO_LAMBDA
269270
end
270271
when :tCHARACTER

0 commit comments

Comments
 (0)