YJIT: refactor format_number (#8869)

Replace enumerators with simpler and faster version that only inserts commas before '.' or end of integer string.
This commit is contained in:
Mau Magnaguagno 2023-11-08 12:37:19 -03:00 committed by GitHub
parent 50402db5a7
commit eb2abc3f16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -437,11 +437,10 @@ module RubyVM::YJIT
# Format large numbers with comma separators for readability # Format large numbers with comma separators for readability
def format_number(pad, number) def format_number(pad, number)
integer, decimal = number.to_s.split(".") s = number.to_s
d_groups = integer.chars.reverse.each_slice(3) i = s.index('.') || s.size
with_commas = d_groups.map(&:join).join(',').reverse s.insert(i -= 3, ',') while i > 3
formatted = [with_commas, decimal].compact.join(".") s.rjust(pad, ' ')
formatted.rjust(pad, ' ')
end end
# Format a number along with a percentage over a total value # Format a number along with a percentage over a total value