Skip to content

Commit 13a60b8

Browse files
committed
Improve performance by avoiding named block arguments. Closes #11109 [adymo]
Reapplies [8865] with some fixes git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8957 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
1 parent 7dcd0d7 commit 13a60b8

File tree

5 files changed

+65
-18
lines changed

5 files changed

+65
-18
lines changed

activerecord/CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
*SVN*
22

3+
* Perf fix: Avoid the use of named block arguments. Closes #11109 [adymo]
4+
35
* PostgreSQL: support server versions 7.4 through 8.0 and the ruby-pg driver. #11127 [jdavis]
46

57
* Ensure association preloading doesn't break when an association returns nil. ##11145 [GMFlash]

activerecord/lib/active_record/associations/association_collection.rb

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,12 @@ def delete_all
4343
end
4444

4545
# Calculate sum using SQL, not Enumerable
46-
def sum(*args, &block)
47-
calculate(:sum, *args, &block)
46+
def sum(*args)
47+
if block_given?
48+
calculate(:sum, *args) { |*block_args| yield(*block_args) }
49+
else
50+
calculate(:sum, *args)
51+
end
4852
end
4953

5054
# Remove +records+ from this association. Does not destroy +records+.
@@ -121,9 +125,9 @@ def empty?
121125
size.zero?
122126
end
123127

124-
def any?(&block)
128+
def any?
125129
if block_given?
126-
method_missing(:any?, &block)
130+
method_missing(:any?) { |*block_args| yield(*block_args) }
127131
else
128132
!empty?
129133
end
@@ -157,11 +161,21 @@ def replace(other_array)
157161

158162

159163
protected
160-
def method_missing(method, *args, &block)
164+
def method_missing(method, *args)
161165
if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method))
162-
super
166+
if block_given?
167+
super { |*block_args| yield(*block_args) }
168+
else
169+
super
170+
end
163171
else
164-
@reflection.klass.send(:with_scope, construct_scope) { @reflection.klass.send(method, *args, &block) }
172+
@reflection.klass.send(:with_scope, construct_scope) do
173+
if block_given?
174+
@reflection.klass.send(method, *args) { |*block_args| yield(*block_args) }
175+
else
176+
@reflection.klass.send(method, *args)
177+
end
178+
end
165179
end
166180
end
167181

@@ -187,15 +201,23 @@ def find_target
187201

188202
private
189203

190-
def create_record(attrs, &block)
204+
def create_record(attrs)
191205
ensure_owner_is_not_new
192206
record = @reflection.klass.send(:with_scope, :create => construct_scope[:create]) { @reflection.klass.new(attrs) }
193-
add_record_to_target_with_callbacks(record, &block)
207+
if block_given?
208+
add_record_to_target_with_callbacks(record) { |*block_args| yield(*block_args) }
209+
else
210+
add_record_to_target_with_callbacks(record)
211+
end
194212
end
195213

196-
def build_record(attrs, &block)
214+
def build_record(attrs)
197215
record = @reflection.klass.new(attrs)
198-
add_record_to_target_with_callbacks(record, &block)
216+
if block_given?
217+
add_record_to_target_with_callbacks(record) { |*block_args| yield(*block_args) }
218+
else
219+
add_record_to_target_with_callbacks(record)
220+
end
199221
end
200222

201223
def add_record_to_target_with_callbacks(record)

activerecord/lib/active_record/associations/association_proxy.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,13 @@ def merge_options_from_reflection!(options)
120120
end
121121

122122
private
123-
def method_missing(method, *args, &block)
123+
def method_missing(method, *args)
124124
if load_target
125-
@target.send(method, *args, &block)
125+
if block_given?
126+
@target.send(method, *args) { |*block_args| yield(*block_args) }
127+
else
128+
@target.send(method, *args)
129+
end
126130
end
127131
end
128132

activerecord/lib/active_record/associations/has_many_through_association.rb

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,12 @@ def size
113113
end
114114

115115
# Calculate sum using SQL, not Enumerable
116-
def sum(*args, &block)
117-
calculate(:sum, *args, &block)
116+
def sum(*args)
117+
if block_given?
118+
calculate(:sum, *args) { |*block_args| yield(*block_args) }
119+
else
120+
calculate(:sum, *args)
121+
end
118122
end
119123

120124
def count(*args)
@@ -128,11 +132,21 @@ def count(*args)
128132
end
129133

130134
protected
131-
def method_missing(method, *args, &block)
135+
def method_missing(method, *args)
132136
if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method))
133-
super
137+
if block_given?
138+
super { |*block_args| yield(*block_args) }
139+
else
140+
super
141+
end
134142
else
135-
@reflection.klass.send(:with_scope, construct_scope) { @reflection.klass.send(method, *args, &block) }
143+
@reflection.klass.send(:with_scope, construct_scope) do
144+
if block_given?
145+
@reflection.klass.send(method, *args) { |*block_args| yield(*block_args) }
146+
else
147+
@reflection.klass.send(method, *args)
148+
end
149+
end
136150
end
137151
end
138152

activerecord/test/cases/associations/join_model_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,11 @@ def test_has_many_class_methods_called_by_method_missing
290290
assert_equal nil, authors(:david).categories.find_by_name('Technology')
291291
end
292292

293+
def test_has_many_array_methods_called_by_method_missing
294+
assert true, authors(:david).categories.any? { |category| category.name == 'General' }
295+
assert_nothing_raised { authors(:david).categories.sort }
296+
end
297+
293298
def test_has_many_going_through_join_model_with_custom_foreign_key
294299
assert_equal [], posts(:thinking).authors
295300
assert_equal [authors(:mary)], posts(:authorless).authors

0 commit comments

Comments
 (0)