Commit 3d1316e
authored
Optimize integer value dumps (#879)
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
```ruby
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
```1 parent 41cf6b1 commit 3d1316e
2 files changed
Lines changed: 30 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
995 | 995 | | |
996 | 996 | | |
997 | 997 | | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
998 | 1010 | | |
999 | 1011 | | |
1000 | 1012 | | |
| |||
1017 | 1029 | | |
1018 | 1030 | | |
1019 | 1031 | | |
1020 | | - | |
1021 | | - | |
| 1032 | + | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
| 1037 | + | |
| 1038 | + | |
| 1039 | + | |
| 1040 | + | |
| 1041 | + | |
| 1042 | + | |
1022 | 1043 | | |
| 1044 | + | |
1023 | 1045 | | |
1024 | 1046 | | |
1025 | 1047 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
111 | 111 | | |
112 | 112 | | |
113 | 113 | | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
114 | 120 | | |
115 | 121 | | |
116 | 122 | | |
| |||
0 commit comments