Skip to content

Commit 8256455

Browse files
committed
Use the pure-Ruby generator on TruffleRuby as it is much faster
* Using the benchmark from #580 $ ruby benchmarks/bench.rb dump pure JSON::Pure::Generator truffleruby 24.0.0, like ruby 3.2.2, Oracle GraalVM Native [x86_64-linux] Warming up -------------------------------------- JSON.dump(obj) 116.000 i/100ms JSON.dump(obj) 235.000 i/100ms JSON.dump(obj) 317.000 i/100ms JSON.dump(obj) 372.000 i/100ms JSON.dump(obj) 374.000 i/100ms Calculating ------------------------------------- JSON.dump(obj) 3.735k (± 0.9%) i/s (267.76 μs/i) - 18.700k in 5.007526s JSON.dump(obj) 3.738k (± 0.7%) i/s (267.49 μs/i) - 18.700k in 5.002252s JSON.dump(obj) 3.743k (± 0.7%) i/s (267.18 μs/i) - 19.074k in 5.096375s JSON.dump(obj) 3.747k (± 0.5%) i/s (266.87 μs/i) - 19.074k in 5.090463s JSON.dump(obj) 3.746k (± 0.5%) i/s (266.96 μs/i) - 19.074k in 5.092069s $ ruby benchmarks/bench.rb dump ext JSON::Ext::Generator truffleruby 24.0.0, like ruby 3.2.2, Oracle GraalVM Native [x86_64-linux] Warming up -------------------------------------- JSON.dump(obj) 19.000 i/100ms JSON.dump(obj) 18.000 i/100ms JSON.dump(obj) 18.000 i/100ms JSON.dump(obj) 18.000 i/100ms JSON.dump(obj) 21.000 i/100ms Calculating ------------------------------------- JSON.dump(obj) 221.260 (±10.8%) i/s (4.52 ms/i) - 1.092k in 5.004381s JSON.dump(obj) 221.983 (± 8.1%) i/s (4.50 ms/i) - 1.113k in 5.055574s JSON.dump(obj) 221.446 (± 8.6%) i/s (4.52 ms/i) - 1.113k in 5.073167s JSON.dump(obj) 226.452 (± 7.9%) i/s (4.42 ms/i) - 1.134k in 5.048568s JSON.dump(obj) 227.795 (± 8.3%) i/s (4.39 ms/i) - 1.134k in 5.025187s
1 parent 4f876a8 commit 8256455

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

Rakefile

+5-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,11 @@ if defined?(RUBY_ENGINE) and RUBY_ENGINE == 'jruby'
238238
task :release => :build
239239
else
240240
desc "Compiling extension"
241-
task :compile => [ EXT_PARSER_DL, EXT_GENERATOR_DL ]
241+
if RUBY_ENGINE == 'truffleruby'
242+
task :compile => [ EXT_PARSER_DL ]
243+
else
244+
task :compile => [ EXT_PARSER_DL, EXT_GENERATOR_DL ]
245+
end
242246

243247
file EXT_PARSER_DL => EXT_PARSER_SRC do
244248
cd EXT_PARSER_DIR do

ext/json/ext/generator/extconf.rb

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
require 'mkmf'
22

3-
$defs << "-DJSON_GENERATOR"
4-
create_makefile 'json/ext/generator'
3+
if RUBY_ENGINE == 'truffleruby'
4+
# The pure-Ruby generator is faster on TruffleRuby, so skip compiling the generator extension
5+
File.write('Makefile', dummy_makefile("").join)
6+
else
7+
$defs << "-DJSON_GENERATOR"
8+
create_makefile 'json/ext/generator'
9+
end

lib/json/ext.rb

+13-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,19 @@ module JSON
44
# This module holds all the modules/classes that implement JSON's
55
# functionality as C extensions.
66
module Ext
7-
require 'json/ext/parser'
8-
require 'json/ext/generator'
9-
$DEBUG and warn "Using Ext extension for JSON."
10-
JSON.parser = Parser
11-
JSON.generator = Generator
7+
if RUBY_ENGINE == 'truffleruby'
8+
require 'json/ext/parser'
9+
require 'json/pure'
10+
$DEBUG and warn "Using Ext extension for JSON parser and Pure library for JSON generator."
11+
JSON.parser = Parser
12+
JSON.generator = JSON::Pure::Generator
13+
else
14+
require 'json/ext/parser'
15+
require 'json/ext/generator'
16+
$DEBUG and warn "Using Ext extension for JSON."
17+
JSON.parser = Parser
18+
JSON.generator = Generator
19+
end
1220
end
1321

1422
JSON_LOADED = true unless defined?(::JSON::JSON_LOADED)

0 commit comments

Comments
 (0)