Skip to content

Commit dd41e1b

Browse files
committed
Merge pull request #1686 from bf4/perf/only_calc_associations_once
Memoize resource relationships
2 parents 4c0e2dc + e554ba2 commit dd41e1b

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

lib/active_model_serializers/adapter/attributes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def serializable_hash_for_single_resource(options)
3434
def resource_relationships(options)
3535
relationships = {}
3636
serializer.associations(@include_tree).each do |association|
37-
relationships[association.key] = relationship_value_for(association, options)
37+
relationships[association.key] ||= relationship_value_for(association, options)
3838
end
3939

4040
relationships

test/benchmark/bm_caching.rb

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ def get_non_caching(on_off = 'on'.freeze)
3535
get("/non_caching/#{on_off}")
3636
end
3737

38+
def debug(msg = '')
39+
if block_given? && ENV['DEBUG'] =~ /\Atrue|on|0\z/i
40+
STDERR.puts yield
41+
else
42+
STDERR.puts msg
43+
end
44+
end
45+
3846
private
3947

4048
def assert_responses(caching, non_caching)
@@ -85,33 +93,21 @@ def assert_equal(expected, actual, message)
8593
STDERR.puts message unless ENV['SUMMARIZE']
8694
end
8795
end
88-
89-
def debug(msg = '')
90-
if block_given? && ENV['DEBUG'] =~ /\Atrue|on|0\z/i
91-
STDERR.puts yield
92-
else
93-
STDERR.puts msg
94-
end
95-
end
9696
end
9797
assertion = ApiAssertion.new
9898
assertion.valid?
99-
# STDERR.puts assertion.get_status
99+
assertion.debug { assertion.get_status }
100100

101101
time = 10
102102
{
103103
'caching on: caching serializers: gc off' => { disable_gc: true, send: [:get_caching, 'on'] },
104-
# 'caching on: caching serializers: gc on' => { disable_gc: false, send: [:get_caching, 'on'] },
105-
'caching off: caching serializers: gc off' => { disable_gc: true, send: [:get_caching, 'off'] },
106-
# 'caching off: caching serializers: gc on' => { disable_gc: false, send: [:get_caching, 'off'] },
107104
'caching on: non-caching serializers: gc off' => { disable_gc: true, send: [:get_non_caching, 'on'] },
108-
# 'caching on: non-caching serializers: gc on' => { disable_gc: false, send: [:get_non_caching, 'on'] },
105+
'caching off: caching serializers: gc off' => { disable_gc: true, send: [:get_caching, 'off'] },
109106
'caching off: non-caching serializers: gc off' => { disable_gc: true, send: [:get_non_caching, 'off'] }
110-
# 'caching off: non-caching serializers: gc on' => { disable_gc: false, send: [:get_non_caching, 'off'] }
111107
}.each do |label, options|
112108
assertion.clear
113109
Benchmark.ams(label, time: time, disable_gc: options[:disable_gc]) do
114110
assertion.send(*options[:send])
115111
end
116-
# STDERR.puts assertion.get_status(options[:send][-1])
112+
assertion.debug { assertion.get_status(options[:send][-1]) }
117113
end

0 commit comments

Comments
 (0)