Skip to content

Instantly share code, notes, and snippets.

@miloops
Created October 7, 2010 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miloops/615635 to your computer and use it in GitHub Desktop.
Save miloops/615635 to your computer and use it in GitHub Desktop.
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.map(&:comments).flatten.each do |comment|
comment.post.account.email
end
end
ActiveRecord::IdentityMap.enabled = false
instances do
Account.first.posts.map(&:comments).flatten.each do |comment|
comment.post.account.email
end
end
Post.belongs_to :account, :inverse_of => :posts
Post.has_many :comments, :inverse_of => :post
Comment.belongs_to :post, :inverse_of => :comments
Account.has_many :posts, :inverse_of => :account
instances do
Account.first.posts.map(&:comments).flatten.each do |comment|
comment.post.account.email
end
end
==================================================
ActiveRecord::IdentityMap.enabled: true
Objects:
Warmup: 117070 allocations | 3643893 bytes
Actual: 15819 allocations | 220567 bytes
==================================================
ActiveRecord::IdentityMap.enabled: false
Objects:
Warmup: 572747 allocations | 20617244 bytes
Actual: 562228 allocations | 20265932 bytes
==================================================
ActiveRecord::IdentityMap.enabled: false
Objects:
Warmup: 96817 allocations | 3031917 bytes
Actual: 95978 allocations | 2956359 bytes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment