Skip to content

Instantly share code, notes, and snippets.

@miloops
Created October 20, 2010 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save miloops/636470 to your computer and use it in GitHub Desktop.
Save miloops/636470 to your computer and use it in GitHub Desktop.
$:.unshift("/Users/miloops/Workspace/github/miloops/rails/activerecord/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')
150.times do |i|
c = a.posts.create!(:title => "test", :body => "Loldel")
10.times do
c.comments.create!(:body => ("lol! " * 10))
end
end
Account.first.posts.each do |post|
post.account.email
post.comments.each do |comment|
comment.post.account.email
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.0597s
-- create_table(:posts)
-> 0.0009s
-- create_table(:comments)
-> 0.0006s
==================================================
ActiveRecord::IdentityMap.enabled: false
Objects:
Warmup: 1662829 allocations | 60073516 bytes
Actual: 1662797 allocations | 60072341 bytes
==================================================
ActiveRecord::IdentityMap.enabled: true
Objects:
Warmup: 53212 allocations | 694650 bytes
Actual: 53217 allocations | 694731 bytes
==================================================
ActiveRecord::IdentityMap.enabled: false
Objects:
Warmup: 293610 allocations | 9184219 bytes
Actual: 292247 allocations | 9055118 bytes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment