Created
October 7, 2010 18:24
-
-
Save miloops/615602 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def instances(&block) | |
GC.enable_stats | |
GC.clear_stats | |
block.call | |
warmup_objs = GC.num_allocations | |
warmup_bytes = GC.allocated_size | |
GC.clear_stats | |
block.call | |
puts "=" * 50 | |
puts "ActiveRecord::IdentityMap.enabled: #{ActiveRecord::IdentityMap.enabled}" | |
puts "Objects:" | |
puts "Warmup: #{warmup_objs} allocations | #{warmup_bytes} bytes" | |
puts "Actual: #{GC.num_allocations} allocations | #{GC.allocated_size} bytes" | |
end | |
Account.destroy_all | |
a = Account.create(:email => 'miloops@example.com') | |
50.times do |i| | |
c = a.posts.create!(:title => "test", :body => "Loldel") | |
10.times do | |
c.comments.create!(:body => ("lol! " * 10)) | |
end | |
end | |
ActiveRecord::IdentityMap.enabled = true | |
instances do | |
Account.first.posts.includes(:account).each do |post| | |
post.account.email | |
end | |
end | |
ActiveRecord::IdentityMap.enabled = false | |
instances do | |
Account.first.posts.includes(:account).each do |post| | |
post.account.email | |
end | |
end | |
Post.belongs_to :account, :inverse_of => :posts | |
Account.has_many :posts, :inverse_of => :account | |
instances do | |
Account.first.posts.includes(:account).each do |post| | |
post.account.email | |
end | |
end | |
================================================== | |
ActiveRecord::IdentityMap.enabled: true | |
Objects: | |
Warmup: 9329 allocations | 312613 bytes | |
Actual: 6297 allocations | 194259 bytes | |
================================================== | |
ActiveRecord::IdentityMap.enabled: false | |
Objects: | |
Warmup: 7171 allocations | 223017 bytes | |
Actual: 7178 allocations | 223099 bytes | |
================================================== | |
ActiveRecord::IdentityMap.enabled: false | |
Objects: | |
Warmup: 7572 allocations | 258253 bytes | |
Actual: 7178 allocations | 223099 bytes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment