From 9f8f4c28718d5688f18140111eac517a5ef8142e Mon Sep 17 00:00:00 2001 From: Bogdan Gusiev Date: Sun, 25 Dec 2011 16:44:19 +0200 Subject: AS::Callbacks: improve __define_runner perfomance --- activesupport/lib/active_support/callbacks.rb | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 11069301f1..ba9fb4ecce 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -378,18 +378,15 @@ module ActiveSupport module ClassMethods # Generate the internal runner method called by +run_callbacks+. def __define_runner(symbol) #:nodoc: - body = send("_#{symbol}_callbacks").compile - runner_method = "_run_#{symbol}_callbacks" + name = __callback_runner_name(nil, symbol) + undef_method(name) if method_defined?(name) silence_warnings do + runner_method = "_run_#{symbol}_callbacks" undef_method runner_method if method_defined?(runner_method) class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 def #{runner_method}(key = nil, &blk) - if key - self.class.__run_keyed_callback(key, :#{symbol}, self, &blk) - else - #{body} - end + self.class.__run_callback(key, :#{symbol}, self, &blk) end private :#{runner_method} RUBY_EVAL @@ -400,10 +397,10 @@ module ActiveSupport # If this called first time it creates a new callback method for the key, # calculating which callbacks can be omitted because of per_key conditions. # - def __run_keyed_callback(key, kind, object, &blk) #:nodoc: - name = "_run__#{self.name.hash.abs}__#{kind}__#{key.hash.abs}__callbacks" + def __run_callback(key, kind, object, &blk) #:nodoc: + name = __callback_runner_name(key, kind) unless object.respond_to?(name) - str = send("_#{kind}_callbacks").compile(name, object) + str = send("_#{kind}_callbacks").compile(key, object) class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 def #{name}() #{str} end protected :#{name} @@ -412,6 +409,10 @@ module ActiveSupport object.send(name, &blk) end + def __callback_runner_name(key, kind) + "_run__#{self.name.hash.abs}__#{kind}__#{key.hash.abs}__callbacks" + end + # This is used internally to append, prepend and skip callbacks to the # CallbackChain. # -- cgit v1.2.3 From 4e8286fe124dd5a54960fce191b86fa00010cd3b Mon Sep 17 00:00:00 2001 From: Bogdan Gusiev Date: Sun, 25 Dec 2011 22:23:16 +0200 Subject: AS::Callbacks: improved __define_runner performance --- activesupport/lib/active_support/callbacks.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index ba9fb4ecce..2ebafa28dd 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -381,9 +381,8 @@ module ActiveSupport name = __callback_runner_name(nil, symbol) undef_method(name) if method_defined?(name) - silence_warnings do - runner_method = "_run_#{symbol}_callbacks" - undef_method runner_method if method_defined?(runner_method) + runner_method = "_run_#{symbol}_callbacks" + unless private_method_defined?(runner_method) class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 def #{runner_method}(key = nil, &blk) self.class.__run_callback(key, :#{symbol}, self, &blk) -- cgit v1.2.3 From 5b8d27000b23e7561ef09d3ca3e50ac50bb5885b Mon Sep 17 00:00:00 2001 From: Bogdan Gusiev Date: Wed, 28 Dec 2011 18:03:18 +0200 Subject: Refactor AS::Callbacks Extracted `__reset_runner` from `__define_runner` And call it in proper places --- activesupport/lib/active_support/callbacks.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 2ebafa28dd..0495741c15 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -378,9 +378,6 @@ module ActiveSupport module ClassMethods # Generate the internal runner method called by +run_callbacks+. def __define_runner(symbol) #:nodoc: - name = __callback_runner_name(nil, symbol) - undef_method(name) if method_defined?(name) - runner_method = "_run_#{symbol}_callbacks" unless private_method_defined?(runner_method) class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 @@ -408,6 +405,11 @@ module ActiveSupport object.send(name, &blk) end + def __reset_runner(symbol) + name = __callback_runner_name(nil, symbol) + undef_method(name) if method_defined?(name) + end + def __callback_runner_name(key, kind) "_run__#{self.name.hash.abs}__#{kind}__#{key.hash.abs}__callbacks" end @@ -423,7 +425,7 @@ module ActiveSupport ([self] + ActiveSupport::DescendantsTracker.descendants(self)).reverse.each do |target| chain = target.send("_#{name}_callbacks") yield target, chain.dup, type, filters, options - target.__define_runner(name) + target.__reset_runner(name) end end @@ -537,12 +539,12 @@ module ActiveSupport chain = target.send("_#{symbol}_callbacks").dup callbacks.each { |c| chain.delete(c) } target.send("_#{symbol}_callbacks=", chain) - target.__define_runner(symbol) + target.__reset_runner(symbol) end self.send("_#{symbol}_callbacks=", callbacks.dup.clear) - __define_runner(symbol) + __reset_runner(symbol) end # Define sets of events in the object lifecycle that support callbacks. -- cgit v1.2.3 From 40c8aa706adbb60ff9d289016fb9020d48969fea Mon Sep 17 00:00:00 2001 From: Bogdan Gusiev Date: Fri, 30 Dec 2011 10:53:46 +0200 Subject: AS::Callbacks: remove __define_runner --- activesupport/lib/active_support/callbacks.rb | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 0495741c15..df3aeb6b8a 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -77,8 +77,8 @@ module ActiveSupport # save # end # - def run_callbacks(kind, *args, &block) - send("_run_#{kind}_callbacks", *args, &block) + def run_callbacks(kind, key = nil, &block) + self.class.__run_callbacks(key, kind, self, &block) end private @@ -376,24 +376,12 @@ module ActiveSupport end module ClassMethods - # Generate the internal runner method called by +run_callbacks+. - def __define_runner(symbol) #:nodoc: - runner_method = "_run_#{symbol}_callbacks" - unless private_method_defined?(runner_method) - class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 - def #{runner_method}(key = nil, &blk) - self.class.__run_callback(key, :#{symbol}, self, &blk) - end - private :#{runner_method} - RUBY_EVAL - end - end # This method calls the callback method for the given key. # If this called first time it creates a new callback method for the key, # calculating which callbacks can be omitted because of per_key conditions. # - def __run_callback(key, kind, object, &blk) #:nodoc: + def __run_callbacks(key, kind, object, &blk) #:nodoc: name = __callback_runner_name(key, kind) unless object.respond_to?(name) str = send("_#{kind}_callbacks").compile(key, object) @@ -618,7 +606,6 @@ module ActiveSupport callbacks.each do |callback| class_attribute "_#{callback}_callbacks" send("_#{callback}_callbacks=", CallbackChain.new(callback, config)) - __define_runner(callback) end end end -- cgit v1.2.3