To convert an Integer to a String, it needs to do division and remainder calculations many times. These calculations can consume more CPU cycles than other instructions.
This patch will prepare a lookup table which has the remainder (0~99) of a number divided by 100 and use the table to omit the calculation.
−
before
after
result
Oj.dump
3.816M
4.255M
1.115x
Environment
Linux
Manjaro Linux x86_64
Kernel: 6.3.0-1-MANJARO
AMD Ryzen 7 5800H
gcc version 12.2.1
Ruby 3.2.2
Before
Warming up --------------------------------------
Oj.dump 386.110k i/100ms
Calculating -------------------------------------
Oj.dump 3.816M (± 1.4%) i/s - 19.306M in 5.060327s
After
Warming up --------------------------------------
Oj.dump 425.525k i/100ms
Calculating -------------------------------------
Oj.dump 4.255M (± 0.2%) i/s - 21.276M in 5.000424s
Test code
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'benchmark-ips'
gem 'oj'
end
data = [646086033, 414841692, 706653378, 1069473884, 181209966, 515120040, 892957102, 689595306, 719771732, 651396679, 549722480]
Benchmark.ips do |x|
x.report('Oj.dump') { Oj.dump(data) }
end
To convert an Integer to a String, it needs to do division and remainder calculations many times. These calculations can consume more CPU cycles than other instructions.
This patch will prepare a lookup table which has the remainder (0~99) of a number divided by 100 and use the table to omit the calculation.
Environment
Before
After
Test code