Skip to content

Instantly share code, notes, and snippets.

@miloops
Created October 8, 2010 14:55
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/616919 to your computer and use it in GitHub Desktop.
Save miloops/616919 to your computer and use it in GitHub Desktop.
$:.unshift("/Users/miloops/Workspace/github/miloops/rails/activerecord/lib")
$:.unshift("/Users/miloops/Workspace/github/swistak/weakling/lib")
$:.unshift("/Users/miloops/Workspace/github/railsarel/lib")
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:database => ":memory:"
)
ActiveRecord::Schema.define do
create_table :accounts do |t|
t.string :email
t.timestamps
end
create_table :posts do |t|
t.string :title
t.text :body
t.integer :account_id
t.timestamps
end
create_table :comments do |t|
t.string :body
t.integer :post_id
t.timestamps
end
end
class Post < ActiveRecord::Base
belongs_to :account
has_many :comments
end
class Account < ActiveRecord::Base
has_many :posts
end
class Comment < ActiveRecord::Base
belongs_to :post
end
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
instances do
ActiveRecord::IdentityMap.enabled = false
Account.first.posts.each do |post|
post.account.email
post.comments.each do |comment|
comment.post.account.email
end
end
end
instances do
ActiveRecord::IdentityMap.enabled = true
Account.first.posts.each do |post|
post.account.email
post.comments.each do |comment|
comment.post.account.email
end
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
ActiveRecord::IdentityMap.enabled = false
Account.first.posts.each do |post|
post.account.email
post.comments.each do |comment|
comment.post.account.email
end
end
end
-- create_table(:accounts)
-> 0.0247s
-- create_table(:posts)
-> 0.0008s
-- create_table(:comments)
-> 0.0006s
==================================================
ActiveRecord::IdentityMap.enabled: false
Objects:
Warmup: 555446 allocations | 20109069 bytes
Actual: 554746 allocations | 20045261 bytes
==================================================
ActiveRecord::IdentityMap.enabled: true
Objects:
Warmup: 118945 allocations | 3723192 bytes
Actual: 17924 allocations | 238183 bytes
==================================================
ActiveRecord::IdentityMap.enabled: false
Objects:
Warmup: 109178 allocations | 3464231 bytes
Actual: 97896 allocations | 3039749 bytes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment