Permalink
Browse files

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
  • Loading branch information...
1 parent aa7859c commit 334026151581caf90c1ecebec6eec3f466ebc0a7 @technoweenie technoweenie committed Feb 4, 2007
View
@@ -1,5 +1,7 @@
*SVN*
+* Optimize Class Inheritable Attributes so that unnecessary hashes are not created. Closes #7472 [Bruce Perens]
+
* Ruby 1.8-cvs and 1.9 define a private Time#to_date that overrides Active
Support. Make it public to preserve compatibility. [Jeremy Kemper]
@@ -89,10 +89,13 @@ def class_inheritable_hash(*syms)
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 @@ def read_inheritable_attribute(key)
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)

0 comments on commit 3340261

Please sign in to comment.