From 344a2d5adca154a4d13539421bdf5ea7865e0235 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 7 Jan 2011 13:53:34 -0800 Subject: use a hash for caching aggregations rather than ivars --- activerecord/lib/active_record/aggregations.rb | 18 ++++++------------ activerecord/lib/active_record/base.rb | 5 +++-- 2 files changed, 9 insertions(+), 14 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/aggregations.rb b/activerecord/lib/active_record/aggregations.rb index 0224187fed..4b1fa5c59c 100644 --- a/activerecord/lib/active_record/aggregations.rb +++ b/activerecord/lib/active_record/aggregations.rb @@ -4,9 +4,7 @@ module ActiveRecord extend ActiveSupport::Concern def clear_aggregation_cache #:nodoc: - self.class.reflect_on_all_aggregations.to_a.each do |assoc| - instance_variable_set "@#{assoc.name}", nil - end if self.persisted? + @aggregation_cache.clear if persisted? end # Active Record implements aggregation through a macro-like class method called +composed_of+ @@ -224,11 +222,7 @@ module ActiveRecord def reader_method(name, class_name, mapping, allow_nil, constructor) module_eval do define_method(name) do - unless instance_variable_defined?("@#{name}") - instance_variable_set("@#{name}", nil) - end - - if (instance_variable_get("@#{name}").nil?) && (!allow_nil || mapping.any? {|pair| !read_attribute(pair.first).nil? }) + if (@aggregation_cache[name].nil?) && (!allow_nil || mapping.any? {|pair| !read_attribute(pair.first).nil? }) attrs = mapping.collect {|pair| read_attribute(pair.first)} object = case constructor when Symbol @@ -238,9 +232,9 @@ module ActiveRecord else raise ArgumentError, 'Constructor must be a symbol denoting the constructor method to call or a Proc to be invoked.' end - instance_variable_set("@#{name}", object) + @aggregation_cache[name] = object end - instance_variable_get("@#{name}") + @aggregation_cache[name] end end @@ -251,7 +245,7 @@ module ActiveRecord define_method("#{name}=") do |part| if part.nil? && allow_nil mapping.each { |pair| self[pair.first] = nil } - instance_variable_set("@#{name}", nil) + @aggregation_cache[name] = nil else unless part.is_a?(class_name.constantize) || converter.nil? part = case converter @@ -265,7 +259,7 @@ module ActiveRecord end mapping.each { |pair| self[pair.first] = part.send(pair.last) } - instance_variable_set("@#{name}", part.freeze) + @aggregation_cache[name] = part.freeze end end end diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index f32132b18a..632c7aff4a 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1369,6 +1369,7 @@ MSG def initialize(attributes = nil) @attributes = attributes_from_column_definition @association_cache = {} + @aggregation_cache = {} @attributes_cache = {} @new_record = true @readonly = false @@ -1417,6 +1418,7 @@ MSG @attributes = coder['attributes'] @attributes_cache, @previously_changed, @changed_attributes = {}, {}, {} @association_cache = {} + @aggregation_cache = {} @readonly = @destroyed = @marked_for_destruction = false @new_record = false _run_find_callbacks @@ -1628,8 +1630,7 @@ MSG @changed_attributes[attr] = orig_value if field_changed?(attr, orig_value, @attributes[attr]) end - clear_aggregation_cache - + @aggregation_cache = {} @association_cache = {} @attributes_cache = {} @new_record = true -- cgit v1.2.3