aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/attribute_set_test.rb
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2015-01-20 14:09:53 -0700
committerSean Griffin <sean@thoughtbot.com>2015-01-20 14:42:15 -0700
commitbe9b68038e83a617eb38c26147659162e4ac3d2c (patch)
tree49c393437f2d92da3f6cf6a1ef09ee7eb9b4b6ca /activerecord/test/cases/attribute_set_test.rb
parent08fe700e2fd57a63d1ee899b63e0e818bc0f4e69 (diff)
downloadrails-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/test/cases/attribute_set_test.rb')
-rw-r--r--activerecord/test/cases/attribute_set_test.rb11
1 files changed, 11 insertions, 0 deletions
diff --git a/activerecord/test/cases/attribute_set_test.rb b/activerecord/test/cases/attribute_set_test.rb
index ba53612d30..8025c7c4d2 100644
--- a/activerecord/test/cases/attribute_set_test.rb
+++ b/activerecord/test/cases/attribute_set_test.rb
@@ -186,5 +186,16 @@ module ActiveRecord
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