ohler55 / oj

Optimized JSON
http://www.ohler.com/oj
MIT License
3.14k stars 251 forks source link

Optimize integer value dumps #879

Closed Watson1978 closed 1 year ago

Watson1978 commented 1 year ago

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

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
ohler55 commented 1 year ago

Can you merge in your fix for truffle into this branch?

Watson1978 commented 1 year ago

OK, rebased.

ohler55 commented 1 year ago

Thank you

Watson1978 commented 1 year ago

Can you merge this PR? :)

ohler55 commented 1 year ago

Thank you for the reminder.