Skip to content

Commit

Permalink
Extend ActiveSupport::DescendantsTracker before defining keys
Browse files Browse the repository at this point in the history
Without this, calling #descendants on a class will cause it to walk the entire ObjectSpace, which was happening whenever a new model was defined. This change cut the tests from ~90 secs to ~25 secs on my machine.
  • Loading branch information
bkeepers committed Mar 21, 2011
1 parent 9f8af48 commit 10e4c14
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 5 deletions.
1 change: 0 additions & 1 deletion lib/mongo_mapper.rb
Expand Up @@ -32,7 +32,6 @@ module Plugins
autoload :Callbacks, 'mongo_mapper/plugins/callbacks'
autoload :Caching, 'mongo_mapper/plugins/caching'
autoload :Clone, 'mongo_mapper/plugins/clone'
autoload :Descendants, 'mongo_mapper/plugins/descendants'
autoload :Dirty, 'mongo_mapper/plugins/dirty'
autoload :Document, 'mongo_mapper/plugins/document'
autoload :DynamicQuerying, 'mongo_mapper/plugins/dynamic_querying'
Expand Down
1 change: 1 addition & 0 deletions lib/mongo_mapper/plugins/keys.rb
Expand Up @@ -7,6 +7,7 @@ module Keys
extend ActiveSupport::Concern

included do
extend ActiveSupport::DescendantsTracker
key :_id, ObjectId
end

Expand Down
5 changes: 1 addition & 4 deletions lib/mongo_mapper/railtie.rb
Expand Up @@ -40,10 +40,7 @@ class Railtie < Rails::Railtie
ActionDispatch::Callbacks.to_prepare do
unless app.config.cache_classes
# Rails reloading was making descendants fill up and leak memory, these make sure they get cleared
MongoMapper::Document.descendants.each {|m| m.descendants.clear if m.respond_to?(:descendants) }
MongoMapper::Document.descendants.clear
MongoMapper::EmbeddedDocument.descendants.each {|m| m.descendants.clear if m.respond_to?(:descendants) }
MongoMapper::EmbeddedDocument.descendants.clear
ActiveSupport::DescendantsTracker.clear
MongoMapper::Plugins::IdentityMap.models.clear
end
end
Expand Down
5 changes: 5 additions & 0 deletions test/functional/test_document.rb
Expand Up @@ -269,4 +269,9 @@ def setup
doc.skills.should == ['ruby', 'rails', 'javascript', 'xhtml', 'css']
end
end

should "not walk ObjectSpace when creating a model" do
ObjectSpace.expects(:each_object).never
Doc()
end
end

0 comments on commit 10e4c14

Please sign in to comment.