Browse Source

[ruby/roda] Use rapidjson for faster JSON serialization (#9164)

Petrik de Heus 1 year ago
parent
commit
93918507a4

+ 1 - 1
frameworks/Ruby/roda-sequel/Gemfile

@@ -7,7 +7,7 @@ gem "sequel", "~> 5.67"
 gem "roda", "~> 3.66"
 gem "tilt", "~> 2.1", require: "tilt/erb"
 gem "unicorn", "~> 6.1", platforms: %i[ruby mswin], require: false
-gem "oj", "~> 3.14"
+gem "rapidjson"
 
 group :mysql do
   gem "mysql2", "~> 0.5", platforms: %i[ruby mswin]

+ 1 - 4
frameworks/Ruby/roda-sequel/boot.rb

@@ -1,7 +1,7 @@
 # frozen_string_literal: true
 require "bundler/setup"
 require "time"
-require "oj"
+require "rapidjson"
 MAX_PK = 10_000
 QUERY_RANGE = (1..MAX_PK).freeze
 ALL_IDS = QUERY_RANGE.to_a
@@ -9,9 +9,6 @@ QUERIES_MIN = 1
 QUERIES_MAX = 500
 SEQUEL_NO_ASSOCIATIONS = true
 
-# Use the OJ gem instead of the JSON one
-Oj.mimic_JSON()
-
 SERVER_STRING =
   if defined?(PhusionPassenger)
     [

+ 4 - 4
frameworks/Ruby/roda-sequel/hello_world.rb

@@ -22,13 +22,13 @@ class HelloWorld < Roda
     # Test type 1: JSON serialization
     r.is "json" do
       response[CONTENT_TYPE] = JSON_TYPE
-      { message: "Hello, World!" }.to_json
+      RapidJSON.encode({ message: "Hello, World!" })
     end
 
     # Test type 2: Single database query
     r.is "db" do
       response[CONTENT_TYPE] = JSON_TYPE
-      World.with_pk(rand1).values.to_json
+      RapidJSON.encode(World.with_pk(rand1).values)
     end
 
     # Test type 3: Multiple database queries
@@ -40,7 +40,7 @@ class HelloWorld < Roda
             World.with_pk(id).values
           end
         end
-      worlds.to_json
+      RapidJSON.encode(worlds)
     end
 
     # Test type 4: Fortunes
@@ -70,7 +70,7 @@ class HelloWorld < Roda
           end
         World.batch_update(worlds)
       end
-      worlds.map(&:values).to_json
+      RapidJSON.encode(worlds.map!(&:values))
     end
 
     # Test type 6: Plaintext