Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $:.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
- 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
- module ActiveRecord::IdentityMap
- class << self
- def current
- repositories[current_repository_name] ||= WeakHash.new
- 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
Add Comment
Please, Sign In to add comment