mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Added more efficient implementation of the development mode reset of classes #1638 [Chris McGrath]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1761 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
b640a64760
commit
703ac4c5ac
4 changed files with 40 additions and 12 deletions
|
@ -1,6 +1,6 @@
|
|||
*SVN*
|
||||
|
||||
* Remove fix against memory leaks in development mode as it killed development performance on large applications (2-3s stall on Basecamp). Octopod is working on a better fix.
|
||||
* Added more efficient implementation of the development mode reset of classes #1638 [Chris McGrath]
|
||||
|
||||
|
||||
*1.1.0*
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
class Object #:nodoc:
|
||||
def remove_subclasses_of(superclass)
|
||||
subclasses_of(superclass).each do |subclass|
|
||||
# remove_instance_variables_of(klass)
|
||||
Object.send(:remove_const, subclass) rescue nil
|
||||
end
|
||||
def remove_subclasses_of(*superclasses)
|
||||
subclasses_of(*superclasses).each do |subclass|
|
||||
subclass.instance_variables.each { |v| subclass.send(:remove_instance_variable, v) }
|
||||
Object.send(:remove_const, subclass.to_s) rescue nil
|
||||
end
|
||||
end
|
||||
|
||||
def remove_instance_variables_of(klass)
|
||||
|
@ -14,11 +14,11 @@ class Object #:nodoc:
|
|||
end
|
||||
end
|
||||
|
||||
def subclasses_of(superclass)
|
||||
def subclasses_of(*superclasses)
|
||||
subclasses = []
|
||||
ObjectSpace.each_object(Class) do |k|
|
||||
next if !k.ancestors.include?(superclass) || superclass == k || k.to_s.include?("::") || subclasses.include?(k.to_s)
|
||||
subclasses << k.to_s
|
||||
next if (k.ancestors & superclasses).empty? || superclasses.include?(k) || k.to_s.include?("::") || subclasses.include?(k)
|
||||
subclasses << k
|
||||
end
|
||||
subclasses
|
||||
end
|
||||
|
@ -50,6 +50,6 @@ class Class #:nodoc:
|
|||
end
|
||||
|
||||
def subclasses
|
||||
Object.subclasses_of(self)
|
||||
Object.subclasses_of(self).map { |o| o.to_s }
|
||||
end
|
||||
end
|
||||
|
|
|
@ -39,7 +39,7 @@ module Dependencies #:nodoc:
|
|||
end
|
||||
|
||||
def remove_subclasses_for(*classes)
|
||||
classes.each { |klass| klass.remove_subclasses }
|
||||
Object.remove_subclasses_of(*classes)
|
||||
end
|
||||
|
||||
# LoadingModules implement namespace-safe dynamic loading.
|
||||
|
|
|
@ -12,6 +12,20 @@ class RemoveSubsBaseClass
|
|||
end
|
||||
end
|
||||
class RemoveSubsSubClass < RemoveSubsBaseClass; end
|
||||
class RemoveSubsTestClass2; end
|
||||
class RemoveSubsBaseClass2
|
||||
def self.add_ivar
|
||||
@ivar = RemoveSubsTestClass2.new
|
||||
end
|
||||
end
|
||||
class RemoveSubsSubClass2 < RemoveSubsBaseClass2; end
|
||||
class RemoveSubsTestClass3; end
|
||||
class RemoveSubsBaseClass3
|
||||
def self.add_ivar
|
||||
@ivar = RemoveSubsTestClass3.new
|
||||
end
|
||||
end
|
||||
class RemoveSubsSubClass3 < RemoveSubsBaseClass3; end
|
||||
|
||||
class ClassExtTest < Test::Unit::TestCase
|
||||
def test_methods
|
||||
|
@ -48,4 +62,18 @@ class ObjectTests < Test::Unit::TestCase
|
|||
flunk("ObjectSpace still contains RemoveSubsTestClass") if o.class == RemoveSubsTestClass
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_remove_subclasses_of_multiple_classes_unsets_ivars
|
||||
r2 = RemoveSubsSubClass2.new
|
||||
RemoveSubsSubClass2.add_ivar
|
||||
r3 = RemoveSubsSubClass3.new
|
||||
RemoveSubsSubClass3.add_ivar
|
||||
|
||||
Object.remove_subclasses_of(RemoveSubsBaseClass2, RemoveSubsBaseClass3)
|
||||
|
||||
GC.start
|
||||
ObjectSpace.each_object do |o|
|
||||
flunk("ObjectSpace still contains RemoveSubsTestClass") if o.class == RemoveSubsTestClass
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue