Skip to content

Commit

Permalink
Merge pull request #582 from eregon/truffleruby-generator-pure
Browse files Browse the repository at this point in the history
Use the pure-Ruby generator on TruffleRuby as it is much faster
  • Loading branch information
hsbt authored Jun 3, 2024
2 parents 55a869b + 8256455 commit dce07d2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
6 changes: 5 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,11 @@ if defined?(RUBY_ENGINE) and RUBY_ENGINE == 'jruby'
task :release => :build
else
desc "Compiling extension"
task :compile => [ EXT_PARSER_DL, EXT_GENERATOR_DL ]
if RUBY_ENGINE == 'truffleruby'
task :compile => [ EXT_PARSER_DL ]
else
task :compile => [ EXT_PARSER_DL, EXT_GENERATOR_DL ]
end

file EXT_PARSER_DL => EXT_PARSER_SRC do
cd EXT_PARSER_DIR do
Expand Down
9 changes: 7 additions & 2 deletions ext/json/ext/generator/extconf.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
require 'mkmf'

$defs << "-DJSON_GENERATOR"
create_makefile 'json/ext/generator'
if RUBY_ENGINE == 'truffleruby'
# The pure-Ruby generator is faster on TruffleRuby, so skip compiling the generator extension
File.write('Makefile', dummy_makefile("").join)
else
$defs << "-DJSON_GENERATOR"
create_makefile 'json/ext/generator'
end
18 changes: 13 additions & 5 deletions lib/json/ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@ module JSON
# This module holds all the modules/classes that implement JSON's
# functionality as C extensions.
module Ext
require 'json/ext/parser'
require 'json/ext/generator'
$DEBUG and warn "Using Ext extension for JSON."
JSON.parser = Parser
JSON.generator = Generator
if RUBY_ENGINE == 'truffleruby'
require 'json/ext/parser'
require 'json/pure'
$DEBUG and warn "Using Ext extension for JSON parser and Pure library for JSON generator."
JSON.parser = Parser
JSON.generator = JSON::Pure::Generator
else
require 'json/ext/parser'
require 'json/ext/generator'
$DEBUG and warn "Using Ext extension for JSON."
JSON.parser = Parser
JSON.generator = Generator
end
end

JSON_LOADED = true unless defined?(::JSON::JSON_LOADED)
Expand Down

0 comments on commit dce07d2

Please sign in to comment.