Merge pull request #1686 from bf4/perf/only_calc_associations_once

Memoize resource relationships
This commit is contained in:
Benjamin Fleischer 2016-04-18 12:26:22 -05:00
commit dd41e1be42
2 changed files with 12 additions and 16 deletions

View File

@ -34,7 +34,7 @@ module ActiveModelSerializers
def resource_relationships(options) def resource_relationships(options)
relationships = {} relationships = {}
serializer.associations(@include_tree).each do |association| serializer.associations(@include_tree).each do |association|
relationships[association.key] = relationship_value_for(association, options) relationships[association.key] ||= relationship_value_for(association, options)
end end
relationships relationships

View File

@ -35,6 +35,14 @@ class ApiAssertion
get("/non_caching/#{on_off}") get("/non_caching/#{on_off}")
end end
def debug(msg = '')
if block_given? && ENV['DEBUG'] =~ /\Atrue|on|0\z/i
STDERR.puts yield
else
STDERR.puts msg
end
end
private private
def assert_responses(caching, non_caching) def assert_responses(caching, non_caching)
@ -85,33 +93,21 @@ class ApiAssertion
STDERR.puts message unless ENV['SUMMARIZE'] STDERR.puts message unless ENV['SUMMARIZE']
end end
end end
def debug(msg = '')
if block_given? && ENV['DEBUG'] =~ /\Atrue|on|0\z/i
STDERR.puts yield
else
STDERR.puts msg
end
end
end end
assertion = ApiAssertion.new assertion = ApiAssertion.new
assertion.valid? assertion.valid?
# STDERR.puts assertion.get_status assertion.debug { assertion.get_status }
time = 10 time = 10
{ {
'caching on: caching serializers: gc off' => { disable_gc: true, send: [:get_caching, 'on'] }, 'caching on: caching serializers: gc off' => { disable_gc: true, send: [:get_caching, 'on'] },
# 'caching on: caching serializers: gc on' => { disable_gc: false, send: [:get_caching, 'on'] },
'caching off: caching serializers: gc off' => { disable_gc: true, send: [:get_caching, 'off'] },
# 'caching off: caching serializers: gc on' => { disable_gc: false, send: [:get_caching, 'off'] },
'caching on: non-caching serializers: gc off' => { disable_gc: true, send: [:get_non_caching, 'on'] }, 'caching on: non-caching serializers: gc off' => { disable_gc: true, send: [:get_non_caching, 'on'] },
# 'caching on: non-caching serializers: gc on' => { disable_gc: false, send: [:get_non_caching, 'on'] }, 'caching off: caching serializers: gc off' => { disable_gc: true, send: [:get_caching, 'off'] },
'caching off: non-caching serializers: gc off' => { disable_gc: true, send: [:get_non_caching, 'off'] } 'caching off: non-caching serializers: gc off' => { disable_gc: true, send: [:get_non_caching, 'off'] }
# 'caching off: non-caching serializers: gc on' => { disable_gc: false, send: [:get_non_caching, 'off'] }
}.each do |label, options| }.each do |label, options|
assertion.clear assertion.clear
Benchmark.ams(label, time: time, disable_gc: options[:disable_gc]) do Benchmark.ams(label, time: time, disable_gc: options[:disable_gc]) do
assertion.send(*options[:send]) assertion.send(*options[:send])
end end
# STDERR.puts assertion.get_status(options[:send][-1]) assertion.debug { assertion.get_status(options[:send][-1]) }
end end