Skip to content

Commit 3b48bf5

Browse files
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

3 files changed

+3
-5
lines changed

LICENSE-3rdparty.csv

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
Component,Origin,License
2-
plugin,github.com/brianmario/yajl-ruby,MIT
32
plugin,github.com/bundler/bundler,MIT
43
plugin,github.com/ruby/openssl,BSD-2-Clause
54
plugin,github.com/ruby/rake,MIT

fluent-plugin-datadog.gemspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ Gem::Specification.new do |spec|
2929
spec.add_development_dependency "bundler", "~> 2.1"
3030
spec.add_development_dependency "test-unit", '~> 3.1'
3131
spec.add_development_dependency "rake", "~> 12.0"
32-
spec.add_development_dependency "yajl-ruby", "~> 1.2"
3332
spec.add_development_dependency 'webmock', "~> 3.6.0"
3433

3534
spec.metadata = {

lib/fluent/plugin/out_datadog.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
require "socket"
77
require "openssl"
8-
require "yajl"
8+
require "json"
99
require "zlib"
1010
require "fluent/plugin/output"
1111

@@ -118,10 +118,10 @@ def format(tag, time, record)
118118
# is compatible with Time.at below.
119119
record = enrich_record(tag, time.to_f, record)
120120
if @use_http
121-
record = Yajl.dump(record)
121+
record = JSON.generate(record)
122122
else
123123
if @use_json
124-
record = "#{api_key} #{Yajl.dump(record)}"
124+
record = "#{api_key} #{JSON.generate(record)}"
125125
else
126126
record = "#{api_key} #{record}"
127127
end

0 commit comments

Comments
 (0)