Skip to content

Commit

Permalink
Merge pull request #12188 from SamSaffron/master
Browse files Browse the repository at this point in the history
Perf: avoid dupes add fallback logic for coders
  • Loading branch information
tenderlove committed Sep 11, 2013
2 parents bca623b + 975c9c9 commit 3d60e9d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
15 changes: 8 additions & 7 deletions activerecord/lib/active_record/attribute_methods/read.rb
Expand Up @@ -109,13 +109,14 @@ def read_attribute(attr_name)
# We use #[] first as a perf optimization for non-nil values. See https://gist.github.com/jonleighton/3552829.
name = attr_name.to_s
@attributes_cache[name] || @attributes_cache.fetch(name) {
column = @columns_hash.fetch(name) {
return @attributes.fetch(name) {
if name == 'id' && self.class.primary_key != name
read_attribute(self.class.primary_key)
end
}
}
column = @column_types_override[name] if @column_types_override
column ||= @column_types[name]

return @attributes.fetch(name) {
if name == 'id' && self.class.primary_key != name
read_attribute(self.class.primary_key)
end
} unless column

value = @attributes.fetch(name) {
return block_given? ? yield(name) : nil
Expand Down
6 changes: 4 additions & 2 deletions activerecord/lib/active_record/core.rb
Expand Up @@ -168,7 +168,8 @@ def initialize(attributes = nil)
defaults.each { |k, v| defaults[k] = v.dup if v.duplicable? }

@attributes = self.class.initialize_attributes(defaults)
@columns_hash = self.class.column_types.dup
@column_types_override = nil
@column_types = self.class.column_types

init_internals
init_changed_attributes
Expand All @@ -193,7 +194,8 @@ def initialize(attributes = nil)
# post.title # => 'hello world'
def init_with(coder)
@attributes = self.class.initialize_attributes(coder['attributes'])
@columns_hash = self.class.column_types.merge(coder['column_types'] || {})
@column_types_override = coder['column_types']
@column_types = self.class.column_types

init_internals

Expand Down
5 changes: 3 additions & 2 deletions activerecord/lib/active_record/persistence.rb
Expand Up @@ -383,9 +383,10 @@ def reload(options = nil)
end

@attributes.update(fresh_object.instance_variable_get('@attributes'))
@columns_hash = fresh_object.instance_variable_get('@columns_hash')

@attributes_cache = {}
@column_types = self.class.column_types
@column_types_override = fresh_object.instance_variable_get('@columns_types_override')

This comment has been minimized.

Copy link
@carlosantoniodasilva

carlosantoniodasilva Sep 12, 2013

Member

There was a typo over here on the instance var get call, which was raising tons of warning on travis, fixed in 423f3b8.

@attributes_cache = {}
self
end

Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/serialized_attribute_test.rb
Expand Up @@ -245,7 +245,7 @@ def test_serialized_column_should_not_be_wrapped_twice
Topic.create(content: myobj)

Topic.all.each do |topic|
type = topic.instance_variable_get("@columns_hash")["content"]
type = Topic.column_types["content"]
assert !type.instance_variable_get("@column").is_a?(ActiveRecord::AttributeMethods::Serialization::Type)
end
end
Expand Down

0 comments on commit 3d60e9d

Please sign in to comment.