From 3b48bf58dcabe6dacd13124c04d9c2c10120b373 Mon Sep 17 00:00:00 2001 From: Shizuo Fujita Date: Tue, 27 May 2025 11:16:23 +0900 Subject: [PATCH] Use JSON.generate instead of Yajl.dump MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 ``` --- LICENSE-3rdparty.csv | 1 - fluent-plugin-datadog.gemspec | 1 - lib/fluent/plugin/out_datadog.rb | 6 +++--- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/LICENSE-3rdparty.csv b/LICENSE-3rdparty.csv index da9e709..ef9f891 100644 --- a/LICENSE-3rdparty.csv +++ b/LICENSE-3rdparty.csv @@ -1,5 +1,4 @@ Component,Origin,License -plugin,github.com/brianmario/yajl-ruby,MIT plugin,github.com/bundler/bundler,MIT plugin,github.com/ruby/openssl,BSD-2-Clause plugin,github.com/ruby/rake,MIT \ No newline at end of file diff --git a/fluent-plugin-datadog.gemspec b/fluent-plugin-datadog.gemspec index 9acb04d..1ab8110 100644 --- a/fluent-plugin-datadog.gemspec +++ b/fluent-plugin-datadog.gemspec @@ -29,7 +29,6 @@ Gem::Specification.new do |spec| spec.add_development_dependency "bundler", "~> 2.1" spec.add_development_dependency "test-unit", '~> 3.1' spec.add_development_dependency "rake", "~> 12.0" - spec.add_development_dependency "yajl-ruby", "~> 1.2" spec.add_development_dependency 'webmock', "~> 3.6.0" spec.metadata = { diff --git a/lib/fluent/plugin/out_datadog.rb b/lib/fluent/plugin/out_datadog.rb index 23c30f0..e316ccb 100644 --- a/lib/fluent/plugin/out_datadog.rb +++ b/lib/fluent/plugin/out_datadog.rb @@ -5,7 +5,7 @@ require "socket" require "openssl" -require "yajl" +require "json" require "zlib" require "fluent/plugin/output" @@ -118,10 +118,10 @@ def format(tag, time, record) # is compatible with Time.at below. record = enrich_record(tag, time.to_f, record) if @use_http - record = Yajl.dump(record) + record = JSON.generate(record) else if @use_json - record = "#{api_key} #{Yajl.dump(record)}" + record = "#{api_key} #{JSON.generate(record)}" else record = "#{api_key} #{record}" end