Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PERF: Recover marshaling dump/load performance #31827

Merged
merged 2 commits into from Feb 1, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 5 additions & 11 deletions activemodel/lib/active_model/attribute_set/builder.rb
Expand Up @@ -22,12 +22,12 @@ def build_from_database(values = {}, additional_types = {})
class LazyAttributeHash # :nodoc:
delegate :transform_values, :each_key, :each_value, :fetch, :except, to: :materialize

def initialize(types, values, additional_types, default_attributes)
def initialize(types, values, additional_types, default_attributes, delegate_hash = {})
@types = types
@values = values
@additional_types = additional_types
@materialized = false
@delegate_hash = {}
@delegate_hash = delegate_hash
@default_attributes = default_attributes
end

Expand Down Expand Up @@ -76,19 +76,13 @@ def ==(other)
end

def marshal_dump
if @materialized
materialize
else
[@types, @values, @additional_types, @default_attributes]
end
[@types, @values, @additional_types, @default_attributes, @delegate_hash]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will lose any mutations that have been made to the record. Can we just stick @delegate_hash in here and lose the materialized branch entirely?

Good point... I've fixed in 7c7e7b7.

end

def marshal_load(values)
if values.is_a?(Hash)
@delegate_hash = values
@types = {}
@values = {}
@additional_types = {}
empty_hash = {}.freeze
initialize(empty_hash, empty_hash, empty_hash, empty_hash, values)
@materialized = true
else
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like we only need the else branch here, don't we?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see -- compatibility with old marshaled values.

initialize(*values)
Expand Down
10 changes: 8 additions & 2 deletions activemodel/test/cases/attribute_set_test.rb
Expand Up @@ -221,9 +221,15 @@ def assert_valid_value(*)
builder = AttributeSet::Builder.new(foo: Type::String.new)
attributes = builder.build_from_database(foo: "1")

attributes.fetch(:foo) # force materialized
attributes = Marshal.load(Marshal.dump(attributes))
attributes.instance_variable_get(:@attributes).instance_eval do
class << self
def marshal_dump
materialize
end
end
end

attributes = Marshal.load(Marshal.dump(attributes))
assert_equal({ foo: "1" }, attributes.to_hash)
end

Expand Down