Skip to content

Commit

Permalink
Merge pull request #113 from intridea/performance
Browse files Browse the repository at this point in the history
Does MultiJson introduce slowness?
  • Loading branch information
rwz committed May 10, 2013
2 parents 802286d + 8a26ee9 commit 721688f
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 9 deletions.
8 changes: 8 additions & 0 deletions lib/multi_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ def default_options=(value)
self.load_options = self.dump_options = value
end

# cache busting
%w(load_options= dump_options=).each do |method|
define_method method do |*args|
use current_adapter
super *args
end
end

ALIASES = {
'jrjackson' => :jr_jackson
}
Expand Down
18 changes: 12 additions & 6 deletions lib/multi_json/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,33 @@ def defaults(action, value)
end
end

def activate!
@load_options_cache = {}
@dump_options_cache = {}
instance.activate if instance.respond_to?(:activate)
end

def load(string, options={})
instance.load(string, collect_load_options(string, options))
instance.load(string, collect_load_options(string, options).clone)
end

def dump(object, options={})
instance.dump(object, collect_dump_options(object, options))
instance.dump(object, collect_dump_options(object, options).clone)
end

protected

def collect_load_options(string, options)
collect_options :load_options, options, [ string, options ]
@load_options_cache[options] ||= collect_options(:load_options, options).merge(options)
end

def collect_dump_options(object, options)
collect_options :dump_options, options, [ object, options ]
@dump_options_cache[options] ||= collect_options(:dump_options, options).merge(options)
end

def collect_options(method, overrides, args)
def collect_options(method, *args)
global, local = *[MultiJson, self].map{ |r| r.send(method, *args) }
local.merge(global).merge(overrides)
local.merge(global)
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/multi_json/adapters/json_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class JsonCommon < Adapter

GEM_VERSION = '1.7.7'

def self.activate!
def activate
if JSON::VERSION < GEM_VERSION
Kernel.warn "You are using an old or stdlib version of #{gem_name} gem\n" +
"Please upgrade to the recent version by adding this to your Gemfile:\n\n" +
Expand Down
2 changes: 1 addition & 1 deletion lib/multi_json/adapters/json_gem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Adapters
class JsonGem < JsonCommon
ParseError = ::JSON::ParserError

def self.gem_name
def gem_name
'json'
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/multi_json/adapters/json_pure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Adapters
class JsonPure < JsonCommon
ParseError = ::JSON::ParserError

def self.gem_name
def gem_name
'json_pure'
end
end
Expand Down
12 changes: 12 additions & 0 deletions spec/multi_json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@
end
end

context 'caching' do
before{ MultiJson.use :json_gem }
let(:json_string){ '{"abc":"def"}' }

it 'busts options caches on change' do
MultiJson.load_options = { :symbolize_keys => true }
expect(MultiJson.load(json_string)).to eq(:abc => 'def')
MultiJson.load_options = nil
expect(MultiJson.load(json_string)).to eq('abc' => 'def')
end
end

context 'with stdlib version' do
around do |example|
version = JSON::VERSION
Expand Down

0 comments on commit 721688f

Please sign in to comment.