From 334026151581caf90c1ecebec6eec3f466ebc0a7 Mon Sep 17 00:00:00 2001 From: Rick Olson Date: Sun, 4 Feb 2007 18:20:04 +0000 Subject: Optimize Class Inheritable Attributes so that unnecessary hashes are not created. Closes #7472 [Bruce Perens] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6110 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../core_ext/class/inheritable_attributes.rb | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'activesupport/lib/active_support/core_ext/class') diff --git a/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb b/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb index 3c5b2b8340..d8283760b9 100644 --- a/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb +++ b/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb @@ -89,10 +89,13 @@ class Class # :nodoc: end def inheritable_attributes - @inheritable_attributes ||= {} + @inheritable_attributes ||= EMPTY_INHERITABLE_ATTRIBUTES end def write_inheritable_attribute(key, value) + if inheritable_attributes.equal?(EMPTY_INHERITABLE_ATTRIBUTES) + inheritable_attributes = {} + end inheritable_attributes[key] = value end @@ -111,15 +114,21 @@ class Class # :nodoc: end def reset_inheritable_attributes - inheritable_attributes.clear + @inheritable_attributes = EMPTY_INHERITABLE_ATTRIBUTES end - private + private + EMPTY_INHERITABLE_ATTRIBUTES = {}.freeze + def inherited_with_inheritable_attributes(child) inherited_without_inheritable_attributes(child) if respond_to?(:inherited_without_inheritable_attributes) - new_inheritable_attributes = inheritable_attributes.inject({}) do |memo, (key, value)| - memo.update(key => (value.dup rescue value)) + if inheritable_attributes.equal?(EMPTY_INHERITABLE_ATTRIBUTES) + new_inheritable_attributes = EMPTY_INHERITABLE_ATTRIBUTES + else + new_inheritable_attributes = inheritable_attributes.inject({}) do |memo, (key, value)| + memo.update(key => (value.dup rescue value)) + end end child.instance_variable_set('@inheritable_attributes', new_inheritable_attributes) -- cgit v1.2.3