[ruby/prism] Optimize ripper translator token sorting

With the benchmark from 2ea81398cc
Prism is:
* 1.33x slower before
* 1.07x slower after

https://github.com/ruby/prism/commit/a18b0acd80
This commit is contained in:
Earlopain 2026-01-25 21:33:48 +01:00 committed by git
parent b75938e2c3
commit 9269069e90

View File

@ -802,8 +802,12 @@ module Prism
# Drop the EOF token from the list # Drop the EOF token from the list
tokens = tokens[0...-1] tokens = tokens[0...-1]
# We sort by location to compare against Ripper's output # We sort by location because Ripper.lex sorts.
tokens.sort_by!(&:location) # Manually implemented instead of `sort_by!(&:location)` for performance.
tokens.sort_by! do |token|
line, column = token.location
source.line_to_byte_offset(line) + column
end
# Add :on_sp tokens # Add :on_sp tokens
tokens = add_on_sp_tokens(tokens, source, result.data_loc, bom, eof_token) tokens = add_on_sp_tokens(tokens, source, result.data_loc, bom, eof_token)