Commit 3b48bf5
committed
Use JSON.generate instead of Yajl.dump
Recently, Ruby's JSON has huge improvement and it is much faster.
Ref. https://byroot.github.io/ruby/json/2024/12/15/optimizing-ruby-json-part-1.html
So, this PR will change yajl-ruby to Ruby's JSON.
### envronment
- Ruby: 3.4.4
- OS: Linux 6.14.6-2
- Compiler: gcc 15.1.1
### benchmark
```ruby
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'benchmark-ips'
gem 'json'
gem 'yajl-ruby'
end
require 'json'
require 'yajl'
require 'yajl/version'
puts "* JSON version: #{JSON::VERSION}"
puts "* Yajl version: #{Yajl::VERSION}"
json = '{"a":"Alpha","b":true,"c":12345,"d":[true,[false,[-123456789,null],3.9676,["Something else.",false],null]],"e":{"zero":null,"one":1,"two":2,"three":[3],"four":[0,1,2,3,4]},"f":null,"h":{"a":{"b":{"c":{"d":{"e":{"f":{"g":null}}}}}}},"i":[[[[[[[null]]]]]]]}'
data = JSON.parse(json)
Benchmark.ips do |x|
x.report('Yajl.dump') { Yajl.dump(data) }
x.report('JSON.generate') { JSON.generate(data) }
x.compare!
end
```
### result
```
$ ruby tmp.rb
* JSON version: 2.12.2
* Yajl version: 1.4.3
ruby 3.4.4 (2025-05-14 revision a38531fd3f) +PRISM [x86_64-linux]
Warming up --------------------------------------
Yajl.dump 34.964k i/100ms
JSON.generate 120.598k i/100ms
Calculating -------------------------------------
Yajl.dump 345.056k (± 2.8%) i/s (2.90 μs/i) - 1.748M in 5.070452s
JSON.generate 1.212M (± 1.6%) i/s (824.89 ns/i) - 6.150M in 5.074868s
Comparison:
JSON.generate: 1212286.6 i/s
Yajl.dump: 345056.0 i/s - 3.51x slower
```1 parent b50ef8b commit 3b48bf5
File tree
3 files changed
+3
-5
lines changed- lib/fluent/plugin
3 files changed
+3
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | 2 | | |
4 | 3 | | |
5 | 4 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | | - | |
33 | 32 | | |
34 | 33 | | |
35 | 34 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| |||
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
121 | | - | |
| 121 | + | |
122 | 122 | | |
123 | 123 | | |
124 | | - | |
| 124 | + | |
125 | 125 | | |
126 | 126 | | |
127 | 127 | | |
| |||
0 commit comments