2017-07-09 13:41:28 -04:00
# frozen_string_literal: true
2004-11-23 20:04:44 -05:00
module ActiveRecord
2015-07-08 06:16:16 -04:00
# = Active Record \Callbacks
2010-08-14 01:13:00 -04:00
#
2015-07-08 06:16:16 -04:00
# \Callbacks are hooks into the life cycle of an Active Record object that allow you to trigger logic
2004-12-07 07:10:57 -05:00
# before or after an alteration of the object state. This can be used to make sure that associated and
2015-07-08 06:16:16 -04:00
# dependent objects are deleted when {ActiveRecord::Base#destroy}[rdoc-ref:Persistence#destroy] is called (by overwriting +before_destroy+) or
# to massage attributes before they're validated (by overwriting +before_validation+).
# As an example of the callbacks initiated, consider the {ActiveRecord::Base#save}[rdoc-ref:Persistence#save] call for a new record:
2007-08-28 19:18:57 -04:00
#
# * (-) <tt>save</tt>
# * (-) <tt>valid</tt>
# * (1) <tt>before_validation</tt>
# * (-) <tt>validate</tt>
2009-09-08 11:10:14 -04:00
# * (2) <tt>after_validation</tt>
# * (3) <tt>before_save</tt>
# * (4) <tt>before_create</tt>
2007-08-28 19:18:57 -04:00
# * (-) <tt>create</tt>
2009-09-08 11:10:14 -04:00
# * (5) <tt>after_create</tt>
# * (6) <tt>after_save</tt>
2010-05-01 18:47:09 -04:00
# * (7) <tt>after_commit</tt>
2004-12-07 07:10:57 -05:00
#
2010-05-01 18:47:09 -04:00
# Also, an <tt>after_rollback</tt> callback can be configured to be triggered whenever a rollback is issued.
2015-07-08 06:16:16 -04:00
# Check out ActiveRecord::Transactions for more details about <tt>after_commit</tt> and
2010-05-01 18:47:09 -04:00
# <tt>after_rollback</tt>.
#
2013-10-08 20:57:53 -04:00
# Additionally, an <tt>after_touch</tt> callback is triggered whenever an
# object is touched.
#
2011-12-28 10:38:16 -05:00
# Lastly an <tt>after_find</tt> and <tt>after_initialize</tt> callback is triggered for each object that
2011-01-28 23:23:39 -05:00
# is found and instantiated by a finder, with <tt>after_initialize</tt> being triggered after new objects
# are instantiated as well.
#
2013-10-08 20:57:53 -04:00
# There are nineteen callbacks in total, which give you immense power to react and prepare for each state in the
2015-07-08 06:16:16 -04:00
# Active Record life cycle. The sequence for calling {ActiveRecord::Base#save}[rdoc-ref:Persistence#save] for an existing record is similar,
2011-02-01 02:37:43 -05:00
# except that each <tt>_create</tt> callback is replaced by the corresponding <tt>_update</tt> callback.
2004-11-23 20:04:44 -05:00
#
# Examples:
# class CreditCard < ActiveRecord::Base
2004-12-07 07:10:57 -05:00
# # Strip everything but digits, so the user can specify "555 234 34" or
2012-06-19 14:41:36 -04:00
# # "5552-3434" and both will mean "55523434"
2012-11-08 16:16:54 -05:00
# before_validation(on: :create) do
2004-11-23 20:04:44 -05:00
# self.number = number.gsub(/[^0-9]/, "") if attribute_present?("number")
# end
# end
#
# class Subscription < ActiveRecord::Base
2005-01-01 11:14:15 -05:00
# before_create :record_signup
#
# private
# def record_signup
# self.signed_up_on = Date.today
# end
2004-11-23 20:04:44 -05:00
# end
#
# class Firm < ActiveRecord::Base
2016-05-05 16:30:21 -04:00
# # Disables access to the system, for associated clients and people when the firm is destroyed
# before_destroy { |record| Person.where(firm_id: record.id).update_all(access: 'disabled') }
# before_destroy { |record| Client.where(client_of: record.id).update_all(access: 'disabled') }
2005-01-01 11:14:15 -05:00
# end
2004-11-23 20:04:44 -05:00
#
# == Inheritable callback queues
#
2010-08-14 01:13:00 -04:00
# Besides the overwritable callback methods, it's also possible to register callbacks through the
# use of the callback macros. Their main advantage is that the macros add behavior into a callback
2010-08-02 12:25:26 -04:00
# queue that is kept intact down through an inheritance hierarchy.
2004-11-23 20:04:44 -05:00
#
# class Topic < ActiveRecord::Base
# before_destroy :destroy_author
# end
#
# class Reply < Topic
# before_destroy :destroy_readers
# end
#
2010-08-14 01:13:00 -04:00
# Now, when <tt>Topic#destroy</tt> is run only +destroy_author+ is called. When <tt>Reply#destroy</tt> is
2018-03-30 08:15:55 -04:00
# run, both +destroy_author+ and +destroy_readers+ are called.
2004-11-23 20:04:44 -05:00
#
2010-08-14 01:13:00 -04:00
# *IMPORTANT:* In order for inheritance to work for the callback queues, you must specify the
# callbacks before specifying the associations. Otherwise, you might trigger the loading of a
2010-08-02 12:25:26 -04:00
# child before the parent has registered the callbacks and they won't be inherited.
2005-03-19 10:36:41 -05:00
#
2004-11-23 20:04:44 -05:00
# == Types of callbacks
#
2004-12-07 07:10:57 -05:00
# There are four types of callbacks accepted by the callback macros: Method references (symbol), callback objects,
2017-10-19 16:17:48 -04:00
# inline methods (using a proc). Method references and callback objects
2010-08-14 01:13:00 -04:00
# are the recommended approaches, inline methods using a proc are sometimes appropriate (such as for
2017-10-19 16:17:48 -04:00
# creating mix-ins).
2004-11-23 20:04:44 -05:00
#
# The method reference callbacks work by specifying a protected or private method available in the object, like this:
#
# class Topic < ActiveRecord::Base
# before_destroy :delete_parents
#
# private
# def delete_parents
2019-02-19 10:40:53 -05:00
# self.class.delete_by(parent_id: id)
2004-11-23 20:04:44 -05:00
# end
# end
#
# The callback objects have methods named after the callback called with the record as the only parameter, such as:
#
# class BankAccount < ActiveRecord::Base
2009-02-24 07:29:25 -05:00
# before_save EncryptionWrapper.new
# after_save EncryptionWrapper.new
# after_initialize EncryptionWrapper.new
# end
#
# class EncryptionWrapper
# def before_save(record)
# record.credit_card_number = encrypt(record.credit_card_number)
# end
#
# def after_save(record)
# record.credit_card_number = decrypt(record.credit_card_number)
# end
#
2013-12-03 09:11:53 -05:00
# alias_method :after_initialize, :after_save
2009-02-24 07:29:25 -05:00
#
# private
# def encrypt(value)
# # Secrecy is committed
# end
#
# def decrypt(value)
# # Secrecy is unveiled
# end
# end
#
2018-05-28 07:20:59 -04:00
# So you specify the object you want to be messaged on a given callback. When that callback is triggered, the object has
2009-02-24 07:29:25 -05:00
# a method by the name of the callback messaged. You can make these callbacks more flexible by passing in other
# initialization data such as the name of the attribute to work with:
#
# class BankAccount < ActiveRecord::Base
2004-11-23 20:04:44 -05:00
# before_save EncryptionWrapper.new("credit_card_number")
# after_save EncryptionWrapper.new("credit_card_number")
# after_initialize EncryptionWrapper.new("credit_card_number")
# end
#
# class EncryptionWrapper
# def initialize(attribute)
# @attribute = attribute
# end
#
# def before_save(record)
2009-02-24 07:29:25 -05:00
# record.send("#{@attribute}=", encrypt(record.send("#{@attribute}")))
2004-11-23 20:04:44 -05:00
# end
#
# def after_save(record)
2009-02-24 07:29:25 -05:00
# record.send("#{@attribute}=", decrypt(record.send("#{@attribute}")))
2004-11-23 20:04:44 -05:00
# end
2004-12-07 07:10:57 -05:00
#
2013-12-03 09:11:53 -05:00
# alias_method :after_initialize, :after_save
2004-11-23 20:04:44 -05:00
#
# private
# def encrypt(value)
# # Secrecy is committed
# end
#
# def decrypt(value)
2005-02-07 09:15:53 -05:00
# # Secrecy is unveiled
2004-11-23 20:04:44 -05:00
# end
# end
#
2007-08-28 19:18:57 -04:00
# == <tt>before_validation*</tt> returning statements
2006-07-04 22:44:26 -04:00
#
2014-12-15 01:10:15 -05:00
# If the +before_validation+ callback throws +:abort+, the process will be
2015-07-08 06:16:16 -04:00
# aborted and {ActiveRecord::Base#save}[rdoc-ref:Persistence#save] will return +false+.
2016-02-13 06:36:16 -05:00
# If {ActiveRecord::Base#save!}[rdoc-ref:Persistence#save!] is called it will raise an ActiveRecord::RecordInvalid exception.
2015-07-08 06:16:16 -04:00
# Nothing will be appended to the errors object.
2006-07-04 22:44:26 -04:00
#
2007-12-05 13:54:41 -05:00
# == Canceling callbacks
2005-01-15 12:45:16 -05:00
#
2014-12-15 01:10:15 -05:00
# If a <tt>before_*</tt> callback throws +:abort+, all the later callbacks and
# the associated action are cancelled.
2010-08-14 01:13:00 -04:00
# Callbacks are generally run in the order they are defined, with the exception of callbacks defined as
2010-08-02 12:25:26 -04:00
# methods on the model, which are called last.
2008-08-23 20:51:45 -04:00
#
2012-10-19 15:25:45 -04:00
# == Ordering callbacks
#
# Sometimes the code needs that the callbacks execute in a specific order. For example, a +before_destroy+
2015-08-12 10:17:04 -04:00
# callback (+log_children+ in this case) should be executed before the children get destroyed by the
2015-12-07 08:06:00 -05:00
# <tt>dependent: :destroy</tt> option.
2012-10-19 15:25:45 -04:00
#
2012-10-21 08:53:29 -04:00
# Let's look at the code below:
2012-10-19 15:25:45 -04:00
#
# class Topic < ActiveRecord::Base
2015-12-07 08:06:00 -05:00
# has_many :children, dependent: :destroy
2012-10-19 15:25:45 -04:00
#
# before_destroy :log_children
#
2012-10-22 07:38:57 -04:00
# private
# def log_children
# # Child processing
2012-10-19 15:25:45 -04:00
# end
# end
#
2012-10-21 08:53:29 -04:00
# In this case, the problem is that when the +before_destroy+ callback is executed, the children are not available
2015-07-08 06:16:16 -04:00
# because the {ActiveRecord::Base#destroy}[rdoc-ref:Persistence#destroy] callback gets executed first.
# You can use the +prepend+ option on the +before_destroy+ callback to avoid this.
2012-10-19 15:25:45 -04:00
#
# class Topic < ActiveRecord::Base
2015-12-07 08:06:00 -05:00
# has_many :children, dependent: :destroy
2012-10-19 15:25:45 -04:00
#
# before_destroy :log_children, prepend: true
#
2012-10-22 07:38:57 -04:00
# private
# def log_children
# # Child processing
2012-10-19 15:25:45 -04:00
# end
# end
#
2015-12-07 08:06:00 -05:00
# This way, the +before_destroy+ gets executed before the <tt>dependent: :destroy</tt> is called, and the data is still available.
2012-10-19 15:25:45 -04:00
#
2016-12-07 05:36:32 -05:00
# Also, there are cases when you want several callbacks of the same type to
# be executed in order.
#
# For example:
#
2017-12-15 02:32:58 -05:00
# class Topic < ActiveRecord::Base
2016-12-07 05:36:32 -05:00
# has_many :children
#
# after_save :log_children
# after_save :do_something_else
#
# private
#
2017-10-10 23:08:03 -04:00
# def log_children
2016-12-07 05:36:32 -05:00
# # Child processing
# end
#
# def do_something_else
# # Something else
# end
# end
#
# In this case the +log_children+ gets executed before +do_something_else+.
# The same applies to all non-transactional callbacks.
#
# In case there are multiple transactional callbacks as seen below, the order
# is reversed.
#
# For example:
#
2017-12-15 02:32:58 -05:00
# class Topic < ActiveRecord::Base
2016-12-07 05:36:32 -05:00
# has_many :children
#
# after_commit :log_children
# after_commit :do_something_else
#
# private
#
2017-10-10 23:08:03 -04:00
# def log_children
2016-12-07 05:36:32 -05:00
# # Child processing
# end
#
# def do_something_else
# # Something else
# end
# end
#
# In this case the +do_something_else+ gets executed before +log_children+.
#
2015-07-08 06:16:16 -04:00
# == \Transactions
2008-08-23 20:51:45 -04:00
#
2015-07-08 06:16:16 -04:00
# The entire callback chain of a {#save}[rdoc-ref:Persistence#save], {#save!}[rdoc-ref:Persistence#save!],
# or {#destroy}[rdoc-ref:Persistence#destroy] call runs within a transaction. That includes <tt>after_*</tt> hooks.
# If everything goes fine a COMMIT is executed once the chain has been completed.
2008-08-23 20:51:45 -04:00
#
# If a <tt>before_*</tt> callback cancels the action a ROLLBACK is issued. You
# can also trigger a ROLLBACK raising an exception in any of the callbacks,
# including <tt>after_*</tt> hooks. Note, however, that in that case the client
2015-07-08 06:16:16 -04:00
# needs to be aware of it because an ordinary {#save}[rdoc-ref:Persistence#save] will raise such exception
2008-08-23 20:51:45 -04:00
# instead of quietly returning +false+.
2010-01-09 08:36:59 -05:00
#
2011-03-28 23:22:16 -04:00
# == Debugging callbacks
2011-12-28 10:38:16 -05:00
#
2015-07-08 06:16:16 -04:00
# The callback chain is accessible via the <tt>_*_callbacks</tt> method on an object. Active Model \Callbacks support
2011-03-28 23:22:16 -04:00
# <tt>:before</tt>, <tt>:after</tt> and <tt>:around</tt> as values for the <tt>kind</tt> property. The <tt>kind</tt> property
# defines what part of the chain the callback runs in.
2011-12-28 10:38:16 -05:00
#
# To find all callbacks in the before_save callback chain:
#
2011-03-28 23:22:16 -04:00
# Topic._save_callbacks.select { |cb| cb.kind.eql?(:before) }
2011-12-28 10:38:16 -05:00
#
2011-03-28 23:22:16 -04:00
# Returns an array of callback objects that form the before_save chain.
2011-12-28 10:38:16 -05:00
#
2011-03-28 23:22:16 -04:00
# To further check if the before_save chain contains a proc defined as <tt>rest_when_dead</tt> use the <tt>filter</tt> property of the callback object:
2011-12-28 10:38:16 -05:00
#
2011-03-28 23:22:16 -04:00
# Topic._save_callbacks.select { |cb| cb.kind.eql?(:before) }.collect(&:filter).include?(:rest_when_dead)
2011-12-28 10:38:16 -05:00
#
2011-03-28 23:22:16 -04:00
# Returns true or false depending on whether the proc is contained in the before_save callback chain on a Topic model.
2011-12-28 10:38:16 -05:00
#
2004-11-23 20:04:44 -05:00
module Callbacks
2009-05-28 12:35:36 -04:00
extend ActiveSupport :: Concern
2009-05-11 22:23:47 -04:00
2009-09-08 11:10:14 -04:00
CALLBACKS = [
2010-08-02 10:16:02 -04:00
:after_initialize , :after_find , :after_touch , :before_validation , :after_validation ,
2009-09-08 11:22:45 -04:00
:before_save , :around_save , :after_save , :before_create , :around_create ,
:after_create , :before_update , :around_update , :after_update ,
2010-08-20 03:06:25 -04:00
:before_destroy , :around_destroy , :after_destroy , :after_commit , :after_rollback
2009-09-08 11:10:14 -04:00
]
2004-11-23 20:04:44 -05:00
2010-05-08 19:06:05 -04:00
def destroy #:nodoc:
2015-10-28 14:24:07 -04:00
@_destroy_callback_already_called || = false
return if @_destroy_callback_already_called
@_destroy_callback_already_called = true
Revert "Revert "Reduce allocations when running AR callbacks.""
This reverts commit bdc1d329d4eea823d07cf010064bd19c07099ff3.
Before:
Calculating -------------------------------------
22.000 i/100ms
-------------------------------------------------
229.700 (± 0.4%) i/s - 1.166k
Total Allocated Object: 9939
After:
Calculating -------------------------------------
24.000 i/100ms
-------------------------------------------------
246.443 (± 0.8%) i/s - 1.248k
Total Allocated Object: 7939
```
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
# gem 'rails', github: 'rails/rails', ref: 'bdc1d329d4eea823d07cf010064bd19c07099ff3'
gem 'rails', github: 'rails/rails', ref: 'd2876141d08341ec67cf6a11a073d1acfb920de7'
gem 'arel', github: 'rails/arel'
gem 'sqlite3'
gem 'benchmark-ips'
end
require 'active_record'
require 'benchmark/ips'
ActiveRecord::Base.establish_connection('sqlite3::memory:')
ActiveRecord::Migration.verbose = false
ActiveRecord::Schema.define do
create_table :users, force: true do |t|
t.string :name, :email
t.boolean :admin
t.timestamps null: false
end
end
class User < ActiveRecord::Base
default_scope { where(admin: true) }
end
admin = true
1000.times do
attributes = {
name: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
email: "foobar@email.com",
admin: admin
}
User.create!(attributes)
admin = !admin
end
GC.disable
Benchmark.ips(5, 3) do |x|
x.report { User.all.to_a }
end
key =
if RUBY_VERSION < '2.2'
:total_allocated_object
else
:total_allocated_objects
end
before = GC.stat[key]
User.all.to_a
after = GC.stat[key]
puts "Total Allocated Object: #{after - before}"
```
2015-07-15 10:00:36 -04:00
_run_destroy_callbacks { super }
`destroy` shouldn't raise when child associations fail to save
Deep down in the association internals, we're calling `destroy!` rather
than `destroy` when handling things like `dependent` or autosave
association callbacks. Unfortunately, due to the structure of the code
(e.g. it uses callbacks for everything), it's nearly impossible to pass
whether to call `destroy` or `destroy!` down to where we actually need
it.
As such, we have to do some legwork to handle this. Since the callbacks
are what actually raise the exception, we need to rescue it in
`ActiveRecord::Callbacks`, rather than `ActiveRecord::Persistence` where
it matters. (As an aside, if this code wasn't so callback heavy, it
would handling this would likely be as simple as changing `destroy` to
call `destroy!` instead of the other way around).
Since we don't want to lose the exception when `destroy!` is called (in
particular, we don't want the value of the `record` field to change to
the parent class), we have to do some additional legwork to hold onto it
where we can use it.
Again, all of this is ugly and there is definitely a better way to do
this. However, barring a much more significant re-architecting for what
I consider to be a reletively minor improvement, I'm willing to take
this small hit to the flow of this code (begrudgingly).
2015-07-24 11:13:20 -04:00
rescue RecordNotDestroyed = > e
@_association_destroy_exception = e
false
2015-10-28 14:24:07 -04:00
ensure
@_destroy_callback_already_called = false
2004-11-23 20:04:44 -05:00
end
2019-09-05 06:34:04 -04:00
def touch ( * , ** ) #:nodoc:
Revert "Revert "Reduce allocations when running AR callbacks.""
This reverts commit bdc1d329d4eea823d07cf010064bd19c07099ff3.
Before:
Calculating -------------------------------------
22.000 i/100ms
-------------------------------------------------
229.700 (± 0.4%) i/s - 1.166k
Total Allocated Object: 9939
After:
Calculating -------------------------------------
24.000 i/100ms
-------------------------------------------------
246.443 (± 0.8%) i/s - 1.248k
Total Allocated Object: 7939
```
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
# gem 'rails', github: 'rails/rails', ref: 'bdc1d329d4eea823d07cf010064bd19c07099ff3'
gem 'rails', github: 'rails/rails', ref: 'd2876141d08341ec67cf6a11a073d1acfb920de7'
gem 'arel', github: 'rails/arel'
gem 'sqlite3'
gem 'benchmark-ips'
end
require 'active_record'
require 'benchmark/ips'
ActiveRecord::Base.establish_connection('sqlite3::memory:')
ActiveRecord::Migration.verbose = false
ActiveRecord::Schema.define do
create_table :users, force: true do |t|
t.string :name, :email
t.boolean :admin
t.timestamps null: false
end
end
class User < ActiveRecord::Base
default_scope { where(admin: true) }
end
admin = true
1000.times do
attributes = {
name: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
email: "foobar@email.com",
admin: admin
}
User.create!(attributes)
admin = !admin
end
GC.disable
Benchmark.ips(5, 3) do |x|
x.report { User.all.to_a }
end
key =
if RUBY_VERSION < '2.2'
:total_allocated_object
else
:total_allocated_objects
end
before = GC.stat[key]
User.all.to_a
after = GC.stat[key]
puts "Total Allocated Object: #{after - before}"
```
2015-07-15 10:00:36 -04:00
_run_touch_callbacks { super }
2010-08-02 10:16:02 -04:00
end
2018-09-23 14:03:04 -04:00
def increment! ( attribute , by = 1 , touch : nil ) # :nodoc:
2018-05-26 23:06:58 -04:00
touch ? _run_touch_callbacks { super } : super
end
2010-05-08 19:06:05 -04:00
private
2018-09-23 16:25:44 -04:00
def create_or_update ( ** )
Revert "Revert "Reduce allocations when running AR callbacks.""
This reverts commit bdc1d329d4eea823d07cf010064bd19c07099ff3.
Before:
Calculating -------------------------------------
22.000 i/100ms
-------------------------------------------------
229.700 (± 0.4%) i/s - 1.166k
Total Allocated Object: 9939
After:
Calculating -------------------------------------
24.000 i/100ms
-------------------------------------------------
246.443 (± 0.8%) i/s - 1.248k
Total Allocated Object: 7939
```
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
# gem 'rails', github: 'rails/rails', ref: 'bdc1d329d4eea823d07cf010064bd19c07099ff3'
gem 'rails', github: 'rails/rails', ref: 'd2876141d08341ec67cf6a11a073d1acfb920de7'
gem 'arel', github: 'rails/arel'
gem 'sqlite3'
gem 'benchmark-ips'
end
require 'active_record'
require 'benchmark/ips'
ActiveRecord::Base.establish_connection('sqlite3::memory:')
ActiveRecord::Migration.verbose = false
ActiveRecord::Schema.define do
create_table :users, force: true do |t|
t.string :name, :email
t.boolean :admin
t.timestamps null: false
end
end
class User < ActiveRecord::Base
default_scope { where(admin: true) }
end
admin = true
1000.times do
attributes = {
name: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
email: "foobar@email.com",
admin: admin
}
User.create!(attributes)
admin = !admin
end
GC.disable
Benchmark.ips(5, 3) do |x|
x.report { User.all.to_a }
end
key =
if RUBY_VERSION < '2.2'
:total_allocated_object
else
:total_allocated_objects
end
before = GC.stat[key]
User.all.to_a
after = GC.stat[key]
puts "Total Allocated Object: #{after - before}"
```
2015-07-15 10:00:36 -04:00
_run_save_callbacks { super }
2009-09-08 11:10:14 -04:00
end
2005-07-03 05:04:52 -04:00
2016-12-24 08:39:19 -05:00
def _create_record
Revert "Revert "Reduce allocations when running AR callbacks.""
This reverts commit bdc1d329d4eea823d07cf010064bd19c07099ff3.
Before:
Calculating -------------------------------------
22.000 i/100ms
-------------------------------------------------
229.700 (± 0.4%) i/s - 1.166k
Total Allocated Object: 9939
After:
Calculating -------------------------------------
24.000 i/100ms
-------------------------------------------------
246.443 (± 0.8%) i/s - 1.248k
Total Allocated Object: 7939
```
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
# gem 'rails', github: 'rails/rails', ref: 'bdc1d329d4eea823d07cf010064bd19c07099ff3'
gem 'rails', github: 'rails/rails', ref: 'd2876141d08341ec67cf6a11a073d1acfb920de7'
gem 'arel', github: 'rails/arel'
gem 'sqlite3'
gem 'benchmark-ips'
end
require 'active_record'
require 'benchmark/ips'
ActiveRecord::Base.establish_connection('sqlite3::memory:')
ActiveRecord::Migration.verbose = false
ActiveRecord::Schema.define do
create_table :users, force: true do |t|
t.string :name, :email
t.boolean :admin
t.timestamps null: false
end
end
class User < ActiveRecord::Base
default_scope { where(admin: true) }
end
admin = true
1000.times do
attributes = {
name: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
email: "foobar@email.com",
admin: admin
}
User.create!(attributes)
admin = !admin
end
GC.disable
Benchmark.ips(5, 3) do |x|
x.report { User.all.to_a }
end
key =
if RUBY_VERSION < '2.2'
:total_allocated_object
else
:total_allocated_objects
end
before = GC.stat[key]
User.all.to_a
after = GC.stat[key]
puts "Total Allocated Object: #{after - before}"
```
2015-07-15 10:00:36 -04:00
_run_create_callbacks { super }
2009-09-08 11:10:14 -04:00
end
2008-06-03 14:38:00 -04:00
2018-09-23 16:25:44 -04:00
def _update_record
Revert "Revert "Reduce allocations when running AR callbacks.""
This reverts commit bdc1d329d4eea823d07cf010064bd19c07099ff3.
Before:
Calculating -------------------------------------
22.000 i/100ms
-------------------------------------------------
229.700 (± 0.4%) i/s - 1.166k
Total Allocated Object: 9939
After:
Calculating -------------------------------------
24.000 i/100ms
-------------------------------------------------
246.443 (± 0.8%) i/s - 1.248k
Total Allocated Object: 7939
```
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
# gem 'rails', github: 'rails/rails', ref: 'bdc1d329d4eea823d07cf010064bd19c07099ff3'
gem 'rails', github: 'rails/rails', ref: 'd2876141d08341ec67cf6a11a073d1acfb920de7'
gem 'arel', github: 'rails/arel'
gem 'sqlite3'
gem 'benchmark-ips'
end
require 'active_record'
require 'benchmark/ips'
ActiveRecord::Base.establish_connection('sqlite3::memory:')
ActiveRecord::Migration.verbose = false
ActiveRecord::Schema.define do
create_table :users, force: true do |t|
t.string :name, :email
t.boolean :admin
t.timestamps null: false
end
end
class User < ActiveRecord::Base
default_scope { where(admin: true) }
end
admin = true
1000.times do
attributes = {
name: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
email: "foobar@email.com",
admin: admin
}
User.create!(attributes)
admin = !admin
end
GC.disable
Benchmark.ips(5, 3) do |x|
x.report { User.all.to_a }
end
key =
if RUBY_VERSION < '2.2'
:total_allocated_object
else
:total_allocated_objects
end
before = GC.stat[key]
User.all.to_a
after = GC.stat[key]
puts "Total Allocated Object: #{after - before}"
```
2015-07-15 10:00:36 -04:00
_run_update_callbacks { super }
2009-09-08 11:10:14 -04:00
end
2004-11-23 20:04:44 -05:00
end
2005-01-15 12:45:16 -05:00
end