aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/attribute_methods
diff options
context:
space:
mode:
authorSean Griffin <sean@seantheprogrammer.com>2015-09-28 17:12:28 -0400
committerSean Griffin <sean@seantheprogrammer.com>2015-10-02 08:03:11 -0400
commit07723c23a7dc570beae73c074ad37227e3e8a06e (patch)
tree442e264cad0f2c99c8402d3a7ceea73beb2dadbc /activerecord/lib/active_record/attribute_methods
parent9db73a2591e43d1851411727d6594a72efa35663 (diff)
downloadrails-07723c23a7dc570beae73c074ad37227e3e8a06e.tar.gz
rails-07723c23a7dc570beae73c074ad37227e3e8a06e.tar.bz2
rails-07723c23a7dc570beae73c074ad37227e3e8a06e.zip
Further encapsulate dirty checking on `Attribute`
We can skip the allocation of a full `AttributeSet` by changing the semantics of how we structure things. Instead of comparing two separate `AttributeSet` objects, and `Attribute` is now a singly linked list of every change that has happened to it. Since the attribute objects are immutable, to apply the changes we simply need to copy the head of the list. It's worth noting that this causes one subtle change in the behavior of AR. When a record is saved successfully, the `before_type_cast` version of everything will be what was sent to the database. I honestly think these semantics make more sense, as we could have just as easily had the DB do `RETURNING *` and updated the record with those if we had things like timestamps implemented at the DB layer. This brings our performance closer to 4.2, but we're still not quite there.
Diffstat (limited to 'activerecord/lib/active_record/attribute_methods')
-rw-r--r--activerecord/lib/active_record/attribute_methods/dirty.rb11
1 files changed, 7 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/attribute_methods/dirty.rb b/activerecord/lib/active_record/attribute_methods/dirty.rb
index 43c15841c2..a2e4e0ac31 100644
--- a/activerecord/lib/active_record/attribute_methods/dirty.rb
+++ b/activerecord/lib/active_record/attribute_methods/dirty.rb
@@ -41,13 +41,15 @@ module ActiveRecord
def init_internals
super
- @mutation_tracker = AttributeMutationTracker.new(@attributes, @attributes.dup)
+ @mutation_tracker = AttributeMutationTracker.new(@attributes)
end
def initialize_dup(other) # :nodoc:
super
- @mutation_tracker = AttributeMutationTracker.new(@attributes,
- self.class._default_attributes.deep_dup)
+ @attributes = self.class._default_attributes.map do |attr|
+ attr.with_value_from_user(@attributes.fetch_value(attr.name))
+ end
+ @mutation_tracker = AttributeMutationTracker.new(@attributes)
end
def changes_applied
@@ -122,7 +124,8 @@ module ActiveRecord
end
def store_original_attributes
- @mutation_tracker = @mutation_tracker.now_tracking(@attributes)
+ @attributes = @attributes.map(&:forgetting_assignment)
+ @mutation_tracker = AttributeMutationTracker.new(@attributes)
end
def previous_mutation_tracker