diff options
author | Sean Griffin <sean@thoughtbot.com> | 2015-01-20 14:09:53 -0700 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2015-01-20 14:42:15 -0700 |
commit | be9b68038e83a617eb38c26147659162e4ac3d2c (patch) | |
tree | 49c393437f2d92da3f6cf6a1ef09ee7eb9b4b6ca /activerecord/lib/active_record | |
parent | 08fe700e2fd57a63d1ee899b63e0e818bc0f4e69 (diff) | |
download | rails-be9b68038e83a617eb38c26147659162e4ac3d2c.tar.gz rails-be9b68038e83a617eb38c26147659162e4ac3d2c.tar.bz2 rails-be9b68038e83a617eb38c26147659162e4ac3d2c.zip |
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
```
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/attribute.rb | 6 | ||||
-rw-r--r-- | activerecord/lib/active_record/attribute_methods.rb | 33 | ||||
-rw-r--r-- | activerecord/lib/active_record/attribute_set.rb | 4 |
3 files changed, 42 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/attribute.rb b/activerecord/lib/active_record/attribute.rb index 51e4fae62e..73bb059495 100644 --- a/activerecord/lib/active_record/attribute.rb +++ b/activerecord/lib/active_record/attribute.rb @@ -51,7 +51,7 @@ module ActiveRecord 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 @@ module ActiveRecord false end + def has_been_read? + defined?(@value) + end + def ==(other) self.class == other.class && name == other.name && diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb index 8f165fb1dc..b2db0ceae7 100644 --- a/activerecord/lib/active_record/attribute_methods.rb +++ b/activerecord/lib/active_record/attribute_methods.rb @@ -369,6 +369,39 @@ module ActiveRecord 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 + # 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: diff --git a/activerecord/lib/active_record/attribute_set.rb b/activerecord/lib/active_record/attribute_set.rb index 66fcaf6945..fdce68ce45 100644 --- a/activerecord/lib/active_record/attribute_set.rb +++ b/activerecord/lib/active_record/attribute_set.rb @@ -64,6 +64,10 @@ module ActiveRecord end end + def accessed + attributes.select { |_, attr| attr.has_been_read? }.keys + end + protected attr_reader :attributes |