Skip to content

Commit a18b0ac

Browse files
committed
Optimize ripper translator token sorting
With the benchmark from 2ea8139 Prism is: * 1.33x slower before * 1.07x slower after
1 parent 61e1207 commit a18b0ac

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

lib/prism/lex_compat.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -802,8 +802,12 @@ def result
802802
# Drop the EOF token from the list
803803
tokens = tokens[0...-1]
804804

805-
# We sort by location to compare against Ripper's output
806-
tokens.sort_by!(&:location)
805+
# We sort by location because Ripper.lex sorts.
806+
# Manually implemented instead of `sort_by!(&:location)` for performance.
807+
tokens.sort_by! do |token|
808+
line, column = token.location
809+
source.line_to_byte_offset(line) + column
810+
end
807811

808812
# Add :on_sp tokens
809813
tokens = add_on_sp_tokens(tokens, source, result.data_loc, bom, eof_token)

0 commit comments

Comments
 (0)