mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Creating singleton class for every object whenever the instance-level accessor is used quite is expensive.
This commit is contained in:
@@ -72,11 +72,20 @@ class Class
|
||||
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}
|
||||
end
|
||||
end
|
||||
end
|
||||
val
|
||||
end
|
||||
|
||||
def #{name}
|
||||
defined?(@#{name}) ? @#{name} : singleton_class.#{name}
|
||||
defined?(@#{name}) ? @#{name} : self.class.#{name}
|
||||
end
|
||||
|
||||
def #{name}?
|
||||
@@ -87,4 +96,15 @@ class Class
|
||||
attr_writer name if instance_writer
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def singleton_class?
|
||||
# in case somebody is crazy enough to overwrite allocate
|
||||
allocate = Class.instance_method(:allocate)
|
||||
# object.class always points to a real (non-singleton) class
|
||||
allocate.bind(self).call.class != self
|
||||
rescue TypeError
|
||||
# MRI/YARV/JRuby all disallow creating new instances of a singleton class
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user