diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2013-03-12 14:10:21 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2013-03-12 14:10:21 -0700 |
commit | ed9e3f699ab3fac01ad9ef038847d1cdd4c9f063 (patch) | |
tree | ba730726d6f97b1b4a1736ea392a8edf26e93fff /activesupport | |
parent | 9ca55c2bb5f6902a05cd2cc292c893575ed6cc36 (diff) | |
download | rails-ed9e3f699ab3fac01ad9ef038847d1cdd4c9f063.tar.gz rails-ed9e3f699ab3fac01ad9ef038847d1cdd4c9f063.tar.bz2 rails-ed9e3f699ab3fac01ad9ef038847d1cdd4c9f063.zip |
drop memory consumption and startup speed by switching to define_method
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/core_ext/class/attribute.rb | 51 |
1 files changed, 28 insertions, 23 deletions
diff --git a/activesupport/lib/active_support/core_ext/class/attribute.rb b/activesupport/lib/active_support/core_ext/class/attribute.rb index 5d8d09aa69..23c174a231 100644 --- a/activesupport/lib/active_support/core_ext/class/attribute.rb +++ b/activesupport/lib/active_support/core_ext/class/attribute.rb @@ -77,38 +77,43 @@ class Class # may be used in a performance sensitive context therefore the overhead that # define_method introduces may become significant. attrs.each do |name| - class_eval <<-RUBY, __FILE__, __LINE__ + 1 - def self.#{name}() nil end - def self.#{name}?() !!#{name} end + define_singleton_method(name) { nil } + define_singleton_method("#{name}?") { !!public_send(name) } - def self.#{name}=(val) - singleton_class.class_eval do - remove_possible_method(:#{name}) - define_method(:#{name}) { val } - end + ivar = "@#{name}" + + define_singleton_method("#{name}=") do |val| + singleton_class.class_eval do + remove_possible_method(name) + define_method(name) { val } + end - if singleton_class? - class_eval do - remove_possible_method(:#{name}) - def #{name} - defined?(@#{name}) ? @#{name} : singleton_class.#{name} + if singleton_class? + class_eval do + remove_possible_method(name) + define_method(name) do + if instance_variable_defined? ivar + instance_variable_get ivar + else + singleton_class.send name end end end - val end + val + end - if instance_reader - remove_possible_method :#{name} - def #{name} - defined?(@#{name}) ? @#{name} : self.class.#{name} - end - - def #{name}? - !!#{name} + if instance_reader + remove_possible_method name + define_method(name) do + if instance_variable_defined?(ivar) + instance_variable_get ivar + else + self.class.public_send name end end - RUBY + define_method("#{name}?") { !!public_send(name) } + end attr_writer name if instance_writer end |