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
def format_number(pad, number)
integer, decimal = number.to_s.split(".")
d_groups = integer.chars.reverse.each_slice(3)
with_commas = d_groups.map(&:join).join(',').reverse
formatted = [with_commas, decimal].compact.join(".")
formatted.rjust(pad, ' ')
s = number.to_s
i = s.index('.') || s.size
s.insert(i -= 3, ',') while i > 3
s.rjust(pad, ' ')
end
# Format a number along with a percentage over a total value