Skip to content

Commit bdc1d32

Browse files
committed
Revert "Reduce allocations when running AR callbacks."
This reverts commit 796cab4.
1 parent 2271f7d commit bdc1d32

File tree

8 files changed

+24
-33
lines changed

8 files changed

+24
-33
lines changed

activemodel/lib/active_model/validations.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ def validate!(context = nil)
401401
protected
402402

403403
def run_validations! #:nodoc:
404-
_run_validate_callbacks
404+
run_callbacks :validate
405405
errors.empty?
406406
end
407407

activemodel/lib/active_model/validations/callbacks.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def after_validation(*args, &block)
109109

110110
# Overwrite run validations to include callbacks.
111111
def run_validations! #:nodoc:
112-
_run_validation_callbacks { super }
112+
run_callbacks(:validation) { super }
113113
end
114114
end
115115
end

activerecord/lib/active_record/associations/has_many_through_association.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def delete_records(records, method)
135135
if scope.klass.primary_key
136136
count = scope.destroy_all.length
137137
else
138-
scope.each(&:_run_destroy_callbacks)
138+
scope.each { |record| record.run_callbacks :destroy }
139139

140140
arel = scope.arel
141141

activerecord/lib/active_record/callbacks.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -289,25 +289,24 @@ module ClassMethods
289289
end
290290

291291
def destroy #:nodoc:
292-
_run_destroy_callbacks { super }
292+
run_callbacks(:destroy) { super }
293293
end
294294

295295
def touch(*) #:nodoc:
296-
_run_touch_callbacks { super }
296+
run_callbacks(:touch) { super }
297297
end
298298

299299
private
300-
301300
def create_or_update(*) #:nodoc:
302-
_run_save_callbacks { super }
301+
run_callbacks(:save) { super }
303302
end
304303

305304
def _create_record #:nodoc:
306-
_run_create_callbacks { super }
305+
run_callbacks(:create) { super }
307306
end
308307

309308
def _update_record(*) #:nodoc:
310-
_run_update_callbacks { super }
309+
run_callbacks(:update) { super }
311310
end
312311
end
313312
end

activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ def checkin(conn)
358358
synchronize do
359359
owner = conn.owner
360360

361-
conn._run_checkin_callbacks do
361+
conn.run_callbacks :checkin do
362362
conn.expire
363363
end
364364

@@ -449,7 +449,7 @@ def checkout_new_connection
449449
end
450450

451451
def checkout_and_verify(c)
452-
c._run_checkout_callbacks do
452+
c.run_callbacks :checkout do
453453
c.verify!
454454
end
455455
c

activerecord/lib/active_record/core.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ def initialize(attributes = nil)
293293
assign_attributes(attributes) if attributes
294294

295295
yield self if block_given?
296-
_run_initialize_callbacks
296+
run_callbacks :initialize
297297
end
298298

299299
# Initialize an empty model object from +coder+. +coder+ must contain
@@ -316,8 +316,8 @@ def init_with(coder)
316316

317317
self.class.define_attribute_methods
318318

319-
_run_find_callbacks
320-
_run_initialize_callbacks
319+
run_callbacks :find
320+
run_callbacks :initialize
321321

322322
self
323323
end
@@ -353,7 +353,7 @@ def initialize_dup(other) # :nodoc:
353353
@attributes = @attributes.dup
354354
@attributes.reset(self.class.primary_key)
355355

356-
_run_initialize_callbacks
356+
run_callbacks(:initialize)
357357

358358
@new_record = true
359359
@destroyed = false

activerecord/lib/active_record/transactions.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,8 @@ def rollback_active_record_state!
319319
end
320320

321321
def before_committed! # :nodoc:
322-
_run_before_commit_without_transaction_enrollment_callbacks
323-
_run_before_commit_callbacks
322+
run_callbacks :before_commit_without_transaction_enrollment
323+
run_callbacks :before_commit
324324
end
325325

326326
# Call the +after_commit+ callbacks.
@@ -329,8 +329,8 @@ def before_committed! # :nodoc:
329329
# but call it after the commit of a destroyed object.
330330
def committed!(should_run_callbacks: true) #:nodoc:
331331
if should_run_callbacks && destroyed? || persisted?
332-
_run_commit_without_transaction_enrollment_callbacks
333-
_run_commit_callbacks
332+
run_callbacks :commit_without_transaction_enrollment
333+
run_callbacks :commit
334334
end
335335
ensure
336336
force_clear_transaction_record_state
@@ -340,8 +340,8 @@ def committed!(should_run_callbacks: true) #:nodoc:
340340
# state should be rolled back to the beginning or just to the last savepoint.
341341
def rolledback!(force_restore_state: false, should_run_callbacks: true) #:nodoc:
342342
if should_run_callbacks
343-
_run_rollback_without_transaction_enrollment_callbacks
344-
_run_rollback_callbacks
343+
run_callbacks :rollback
344+
run_callbacks :rollback_without_transaction_enrollment
345345
end
346346
ensure
347347
restore_transaction_record_state(force_restore_state)

activesupport/lib/active_support/callbacks.rb

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,19 @@ module Callbacks
7979
# save
8080
# end
8181
def run_callbacks(kind, &block)
82-
send "_run_#{kind}_callbacks", &block
83-
end
84-
85-
private
82+
callbacks = send("_#{kind}_callbacks")
8683

87-
def _run_callbacks(callbacks, &block)
8884
if callbacks.empty?
89-
block.call if block
85+
yield if block_given?
9086
else
9187
runner = callbacks.compile
9288
e = Filters::Environment.new(self, false, nil, block)
9389
runner.call(e).value
9490
end
9591
end
9692

93+
private
94+
9795
# A hook invoked every time a before callback is halted.
9896
# This can be overridden in AS::Callback implementors in order
9997
# to provide better debugging/logging.
@@ -797,12 +795,6 @@ def define_callbacks(*names)
797795
names.each do |name|
798796
class_attribute "_#{name}_callbacks"
799797
set_callbacks name, CallbackChain.new(name, options)
800-
801-
module_eval <<-RUBY, __FILE__, __LINE__ + 1
802-
def _run_#{name}_callbacks(&block)
803-
_run_callbacks(_#{name}_callbacks, &block)
804-
end
805-
RUBY
806798
end
807799
end
808800

0 commit comments

Comments
 (0)