Permalink
Browse files

Introduce `ActiveRecord::Base#accessed_fields`

This method can be used to see all of the fields on a model which have
been read. This can be useful during development mode to quickly find
out which fields need to be selected. For performance critical pages, if
you are not using all of the fields of a database, an easy performance
win is only selecting the fields which you need. By calling this method
at the end of a controller action, it's easy to determine which fields
need to be selected.

While writing this, I also noticed a place for an easy performance win
internally which I had been wanting to introduce. You cannot mutate a
field which you have not read. Therefore, we can skip the calculation of
in place changes if we have never read from the field. This can
significantly speed up methods like `#changed?` if any of the fields
have an expensive mutable type (like `serialize`)

```
Calculating -------------------------------------
 #changed? with serialized column (before)
                       391.000  i/100ms
 #changed? with serialized column (after)
                         1.514k i/100ms
-------------------------------------------------
 #changed? with serialized column (before)
                          4.243k (± 3.7%) i/s -     21.505k
 #changed? with serialized column (after)
                         16.789k (± 3.2%) i/s -     84.784k
```
  • Loading branch information...
sgrif committed Jan 20, 2015
1 parent 08fe700 commit be9b68038e83a617eb38c26147659162e4ac3d2c
@@ -1,3 +1,9 @@
+* Add `ActiveRecord::Base#accessed_fields`, which can be used to quickly
+ discover which fields were read from a model when you are looking to only
+ select the data you need from the database.
+
+ *Sean Griffin*
+
* Introduce the `:if_exists` option for `drop_table`.
Example:
@@ -51,7 +51,7 @@ def changed_from?(old_value)
end
def changed_in_place_from?(old_value)
- type.changed_in_place?(old_value, value)
+ has_been_read? && type.changed_in_place?(old_value, value)
end
def with_value_from_user(value)
@@ -78,6 +78,10 @@ def came_from_user?
false
end
+ def has_been_read?

This comment has been minimized.

Show comment
Hide comment
@egilburg

egilburg Jan 21, 2015

Contributor

Perhaps name this accessed? to correspond to accessed method on attribute set? Additionally, "read" is more likely to be confused with "loaded from database".

@egilburg

egilburg Jan 21, 2015

Contributor

Perhaps name this accessed? to correspond to accessed method on attribute set? Additionally, "read" is more likely to be confused with "loaded from database".

+ defined?(@value)
+ end
+
def ==(other)
self.class == other.class &&
name == other.name &&
@@ -369,6 +369,39 @@ def []=(attr_name, value)
write_attribute(attr_name, value)
end
+ # Returns the name of all database fields which have been read from this
+ # model. This can be useful in devleopment mode to determine which fields

This comment has been minimized.

Show comment
Hide comment
@rymai

rymai Jan 24, 2015

Contributor

Typo spotted! devleopment => development :)

@rymai

rymai Jan 24, 2015

Contributor

Typo spotted! devleopment => development :)

This comment has been minimized.

Show comment
Hide comment
@sgrif

sgrif Jan 25, 2015

Member

Pull requests welcome! ;)

@sgrif

sgrif Jan 25, 2015

Member

Pull requests welcome! ;)

+ # need to be selected. For performance critical pages, selecting only the
+ # required fields can be an easy performance win (assuming you aren't using
+ # all of the fields on the model).
+ #
+ # For example:
+ #
+ # class PostsController < ActionController::Base
+ # after_action :print_accessed_fields, only: :index
+ #
+ # def index
+ # @posts = Post.all
+ # end
+ #
+ # private
+ #
+ # def print_accessed_fields
+ # p @posts.first.accessed_fields
+ # end
+ # end
+ #
+ # Which allows you to quickly change your code to:
+ #
+ # class PostsController < ActionController::Base
+ # def index
+ # @posts = Post.select(:id, :title, :author_id, :updated_at)
+ # end
+ # end
+ def accessed_fields
+ @attributes.accessed
+ end
+
protected
def clone_attribute_value(reader_method, attribute_name) # :nodoc:
@@ -64,6 +64,10 @@ def reset(key)
end
end
+ def accessed
+ attributes.select { |_, attr| attr.has_been_read? }.keys
+ end
+
protected
attr_reader :attributes
@@ -937,6 +937,16 @@ def test_came_from_user
assert model.id_came_from_user?
end
+ def test_accessed_fields
+ model = @target.first
+
+ assert_equal [], model.accessed_fields
+
+ model.title
+
+ assert_equal ["title"], model.accessed_fields
+ end
+
private
def new_topic_like_ar_class(&block)
@@ -186,5 +186,16 @@ def attributes_with_uninitialized_key
attributes.freeze
assert_equal({ foo: "1" }, attributes.to_hash)
end
+
+ test "#accessed_attributes returns only attributes which have been read" do
+ builder = AttributeSet::Builder.new(foo: Type::Value.new, bar: Type::Value.new)
+ attributes = builder.build_from_database(foo: "1", bar: "2")
+
+ assert_equal [], attributes.accessed
+
+ attributes.fetch_value(:foo)
+
+ assert_equal [:foo], attributes.accessed
+ end
end
end
@@ -169,5 +169,24 @@ def type_cast_from_database(value)
second = Attribute.from_user(:foo, 1, Type::Integer.new)
assert_not_equal first, second
end
+
+ test "an attribute has not been read by default" do
+ attribute = Attribute.from_database(:foo, 1, Type::Value.new)
+ assert_not attribute.has_been_read?
+ end
+
+ test "an attribute has been read when its value is calculated" do
+ attribute = Attribute.from_database(:foo, 1, Type::Value.new)
+ attribute.value
+ assert attribute.has_been_read?
+ end
+
+ test "an attribute can not be mutated if it has not been read,
+ and skips expensive calculations" do
+ type_which_raises_from_all_methods = Object.new
+ attribute = Attribute.from_database(:foo, "bar", type_which_raises_from_all_methods)
+
+ assert_not attribute.changed_in_place_from?("bar")
+ end
end
end

0 comments on commit be9b680

Please sign in to comment.