-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Now that the `json` gem added a `JSON.load_file` method, we can apply the same optimization than for YAML.
- Loading branch information
Showing
7 changed files
with
221 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# frozen_string_literal: true | ||
require('bootsnap/bootsnap') | ||
|
||
module Bootsnap | ||
module CompileCache | ||
module JSON | ||
class << self | ||
attr_accessor(:msgpack_factory, :cache_dir, :supported_options) | ||
|
||
def input_to_storage(payload, _) | ||
obj = ::JSON.parse(payload) | ||
msgpack_factory.dump(obj) | ||
end | ||
|
||
def storage_to_output(data, kwargs) | ||
if kwargs && kwargs.key?(:symbolize_names) | ||
kwargs[:symbolize_keys] = kwargs.delete(:symbolize_names) | ||
end | ||
msgpack_factory.load(data, kwargs) | ||
end | ||
|
||
def input_to_output(data, kwargs) | ||
::JSON.parse(data, **(kwargs || {})) | ||
end | ||
|
||
def precompile(path, cache_dir: self.cache_dir) | ||
Bootsnap::CompileCache::Native.precompile( | ||
cache_dir, | ||
path.to_s, | ||
self, | ||
) | ||
end | ||
|
||
def install!(cache_dir) | ||
self.cache_dir = cache_dir | ||
init! | ||
if ::JSON.respond_to?(:load_file) | ||
::JSON.singleton_class.prepend(Patch) | ||
end | ||
end | ||
|
||
def init! | ||
require('json') | ||
require('msgpack') | ||
|
||
self.msgpack_factory = MessagePack::Factory.new | ||
self.supported_options = [:symbolize_names] | ||
if ::JSON.parse('["foo"]', freeze: true).first.frozen? | ||
self.supported_options = [:freeze] | ||
end | ||
self.supported_options.freeze | ||
end | ||
end | ||
|
||
module Patch | ||
def load_file(path, *args) | ||
return super if args.size > 1 | ||
if kwargs = args.first | ||
return super unless kwargs.is_a?(Hash) | ||
return super unless (kwargs.keys - ::Bootsnap::CompileCache::JSON.supported_options).empty? | ||
end | ||
|
||
begin | ||
::Bootsnap::CompileCache::Native.fetch( | ||
Bootsnap::CompileCache::JSON.cache_dir, | ||
File.realpath(path), | ||
::Bootsnap::CompileCache::JSON, | ||
kwargs, | ||
) | ||
rescue Errno::EACCES | ||
::Bootsnap::CompileCache.permission_error(path) | ||
end | ||
end | ||
|
||
ruby2_keywords :load_file if respond_to?(:ruby2_keywords, true) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# frozen_string_literal: true | ||
require('test_helper') | ||
|
||
class CompileCacheJSONTest < Minitest::Test | ||
include(TmpdirHelper) | ||
|
||
module FakeJson | ||
Fallback = Class.new(StandardError) | ||
class << self | ||
def load_file(path, symbolize_names: false, freeze: false, fallback: nil) | ||
raise Fallback | ||
end | ||
end | ||
end | ||
|
||
def setup | ||
super | ||
Bootsnap::CompileCache::JSON.init! | ||
FakeJson.singleton_class.prepend(Bootsnap::CompileCache::JSON::Patch) | ||
end | ||
|
||
def test_json_input_to_output | ||
document = ::Bootsnap::CompileCache::JSON.input_to_output(<<~JSON, {}) | ||
{ | ||
"foo": 42, | ||
"bar": [1] | ||
} | ||
JSON | ||
expected = { | ||
'foo' => 42, | ||
'bar' => [1], | ||
} | ||
assert_equal expected, document | ||
end | ||
|
||
def test_load_file | ||
Help.set_file('a.json', '{"foo": "bar"}', 100) | ||
assert_equal({'foo' => 'bar'}, FakeJson.load_file('a.json')) | ||
end | ||
|
||
def test_load_file_symbolize_names | ||
Help.set_file('a.json', '{"foo": "bar"}', 100) | ||
FakeJson.load_file('a.json') | ||
|
||
if ::Bootsnap::CompileCache::JSON.supported_options.include?(:symbolize_names) | ||
2.times do | ||
assert_equal({foo: 'bar'}, FakeJson.load_file('a.json', symbolize_names: true)) | ||
end | ||
else | ||
assert_raises(FakeJson::Fallback) do # would call super | ||
FakeJson.load_file('a.json', symbolize_names: true) | ||
end | ||
end | ||
end | ||
|
||
def test_load_file_freeze | ||
Help.set_file('a.json', '["foo"]', 100) | ||
FakeJson.load_file('a.json') | ||
|
||
if ::Bootsnap::CompileCache::JSON.supported_options.include?(:freeze) | ||
2.times do | ||
string = FakeJson.load_file('a.json', freeze: true).first | ||
assert_equal("foo", string) | ||
assert_predicate(string, :frozen?) | ||
end | ||
else | ||
assert_raises(FakeJson::Fallback) do # would call super | ||
FakeJson.load_file('a.json', freeze: true) | ||
end | ||
end | ||
end | ||
|
||
def test_load_file_unknown_option | ||
Help.set_file('a.json', '["foo"]', 100) | ||
FakeJson.load_file('a.json') | ||
|
||
assert_raises(FakeJson::Fallback) do # would call super | ||
FakeJson.load_file('a.json', fallback: true) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters