Guest User

Untitled

a guest
Jun 17th, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1.  
  2. $:.unshift("/Users/miloops/Workspace/github/miloops/rails/activerecord/lib")
  3. $:.unshift("/Users/miloops/Workspace/github/swistak/weakling/lib")
  4. $:.unshift("/Users/miloops/Workspace/github/railsarel/lib")
  5. require 'active_record'
  6.  
  7. ActiveRecord::Base.establish_connection(
  8. :adapter => "sqlite3",
  9. :database => ":memory:"
  10. )
  11.  
  12. ActiveRecord::Schema.define do
  13. create_table :accounts do |t|
  14. t.string :email
  15.  
  16. t.timestamps
  17. end
  18.  
  19. create_table :posts do |t|
  20. t.string :title
  21. t.text :body
  22. t.integer :account_id
  23.  
  24. t.timestamps
  25. end
  26.  
  27. create_table :comments do |t|
  28. t.string :body
  29. t.integer :post_id
  30.  
  31. t.timestamps
  32. end
  33. end
  34.  
  35. class Post < ActiveRecord::Base
  36. belongs_to :account
  37. has_many :comments
  38. end
  39.  
  40. class Account < ActiveRecord::Base
  41. has_many :posts
  42. end
  43.  
  44. class Comment < ActiveRecord::Base
  45. belongs_to :post
  46. end
  47.  
  48. def instances(&block)
  49. GC.enable_stats
  50. GC.clear_stats
  51. block.call
  52. warmup_objs = GC.num_allocations
  53. warmup_bytes = GC.allocated_size
  54. GC.clear_stats
  55. block.call
  56. puts "=" * 50
  57. puts "ActiveRecord::IdentityMap.enabled: #{ActiveRecord::IdentityMap.enabled}"
  58. puts "Objects:"
  59. puts "Warmup: #{warmup_objs} allocations | #{warmup_bytes} bytes"
  60. puts "Actual: #{GC.num_allocations} allocations | #{GC.allocated_size} bytes"
  61. end
  62.  
  63.  
  64. Account.destroy_all
  65.  
  66. a = Account.create(:email => 'miloops@example.com')
  67. 50.times do |i|
  68. c = a.posts.create!(:title => "test", :body => "Loldel")
  69. 10.times do
  70. c.comments.create!(:body => ("lol! " * 10))
  71. end
  72. end
  73.  
  74. Account.first.posts.each do |post|
  75. post.account.email
  76. post.comments.each do |comment|
  77. comment.post.account.email
  78. end
  79. end
  80.  
  81. instances do
  82. ActiveRecord::IdentityMap.enabled = false
  83. Account.first.posts.each do |post|
  84. post.account.email
  85. post.comments.each do |comment|
  86. comment.post.account.email
  87. end
  88. end
  89. end
  90.  
  91. instances do
  92. ActiveRecord::IdentityMap.enabled = true
  93. Account.first.posts.each do |post|
  94. post.account.email
  95. post.comments.each do |comment|
  96. comment.post.account.email
  97. end
  98. end
  99. end
  100.  
  101. module ActiveRecord::IdentityMap
  102. class << self
  103. def current
  104. repositories[current_repository_name] ||= WeakHash.new
  105. end
  106. end
  107. end
  108.  
  109. instances do
  110. ActiveRecord::IdentityMap.enabled = true
  111. Account.first.posts.each do |post|
  112. post.account.email
  113. post.comments.each do |comment|
  114. comment.post.account.email
  115. end
  116. end
  117. end
  118.  
  119. Post.belongs_to :account, :inverse_of => :posts
  120. Post.has_many :comments, :inverse_of => :post
  121. Comment.belongs_to :post, :inverse_of => :comments
  122. Account.has_many :posts, :inverse_of => :account
  123.  
  124. instances do
  125. ActiveRecord::IdentityMap.enabled = false
  126. Account.first.posts.each do |post|
  127. post.account.email
  128. post.comments.each do |comment|
  129. comment.post.account.email
  130. end
  131. end
  132. end
Add Comment
Please, Sign In to add comment