1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activemodel/lib/active_model/attribute_set.rb
Ryuta Kamizono 0adcec4954 PERF: Avoid extra delegation to LazyAttributeHash
The extra delegation to `LazyAttributeHash` has non-negligible overhead.

Avoiding that delegation makes attributes access about 45% faster for
readonly (non-mutation) usage.

https://gist.github.com/kamipo/4002c96a02859d8fe6503e26d7be4ad8

Before:

```
IPS
Warming up --------------------------------------
    attribute access     1.000  i/100ms
Calculating -------------------------------------
    attribute access      3.444  (± 0.0%) i/s -     18.000  in   5.259030s
MEMORY
Calculating -------------------------------------
    attribute access    38.902M memsize (     0.000  retained)
                       350.044k objects (     0.000  retained)
                        15.000  strings (     0.000  retained)
```

After (with `immutable_strings_by_default = true`):

```
IPS
Warming up --------------------------------------
    attribute access     1.000  i/100ms
Calculating -------------------------------------
    attribute access      5.066  (±19.7%) i/s -     25.000  in   5.024650s
MEMORY
Calculating -------------------------------------
    attribute access    27.382M memsize (     0.000  retained)
                       160.044k objects (     0.000  retained)
                        15.000  strings (     0.000  retained)
```
2020-06-15 09:26:24 +09:00

105 lines
2.1 KiB
Ruby

# frozen_string_literal: true
require "active_support/core_ext/object/deep_dup"
require "active_model/attribute_set/builder"
require "active_model/attribute_set/yaml_encoder"
module ActiveModel
class AttributeSet # :nodoc:
delegate :each_value, :fetch, :except, to: :attributes
def initialize(attributes)
@attributes = attributes
end
def [](name)
@attributes[name] || default_attribute(name)
end
def []=(name, value)
@attributes[name] = value
end
def values_before_type_cast
attributes.transform_values(&:value_before_type_cast)
end
def to_hash
keys.index_with { |name| self[name].value }
end
alias :to_h :to_hash
def key?(name)
attributes.key?(name) && self[name].initialized?
end
def keys
attributes.each_key.select { |name| self[name].initialized? }
end
def fetch_value(name, &block)
self[name].value(&block)
end
def write_from_database(name, value)
@attributes[name] = self[name].with_value_from_database(value)
end
def write_from_user(name, value)
raise FrozenError, "can't modify frozen attributes" if frozen?
@attributes[name] = self[name].with_value_from_user(value)
value
end
def write_cast_value(name, value)
@attributes[name] = self[name].with_cast_value(value)
value
end
def freeze
attributes.freeze
super
end
def deep_dup
AttributeSet.new(attributes.deep_dup)
end
def initialize_dup(_)
@attributes = @attributes.dup
super
end
def initialize_clone(_)
@attributes = @attributes.clone
super
end
def reset(key)
if key?(key)
write_from_database(key, nil)
end
end
def accessed
attributes.each_key.select { |name| self[name].has_been_read? }
end
def map(&block)
new_attributes = attributes.transform_values(&block)
AttributeSet.new(new_attributes)
end
def ==(other)
attributes == other.attributes
end
protected
attr_reader :attributes
private
def default_attribute(name)
Attribute.null(name)
end
end
end