diff options
author | schneems <richard.schneeman+foo@gmail.com> | 2018-09-07 21:41:55 -0500 |
---|---|---|
committer | schneems <richard.schneeman+foo@gmail.com> | 2018-10-17 11:05:05 -0500 |
commit | 04454839a1a07cacac58cdf756a6b8e3adde0ef5 (patch) | |
tree | e45385ac4bd770874fa76e0237987969184cf790 /activestorage/lib/active_storage/service.rb | |
parent | ead868315f9b0fedb351c9b451aa1f66a2dc8038 (diff) | |
download | rails-04454839a1a07cacac58cdf756a6b8e3adde0ef5.tar.gz rails-04454839a1a07cacac58cdf756a6b8e3adde0ef5.tar.bz2 rails-04454839a1a07cacac58cdf756a6b8e3adde0ef5.zip |
Use raw time string from DB to generate ActiveRecord#cache_version
Currently, the `updated_at` field is used to generate a `cache_version`. Some database adapters return this timestamp value as a string that must then be converted to a Time value. This process requires a lot of memory and even more CPU time. In the case where this value is only being used for a cache version, we can skip the Time conversion by using the string value directly.
- This PR preserves existing cache format by converting a UTC string from the database to `:usec` format.
- Some databases return an already converted Time object, in those instances, we can directly use `created_at`.
- The `updated_at_before_type_cast` can be a value that comes from either the database or the user. We only want to optimize the case where it is from the database.
- If the format of the cache version has been changed, we cannot apply this optimization, and it is skipped.
- If the format of the time in the database is not UTC, then we cannot use this optimization, and it is skipped.
Some databases (notably PostgreSQL) returns a variable length nanosecond value in the time string. If the value ends in a zero, then it is truncated For instance instead of `2018-10-12 05:00:00.000000` the value `2018-10-12 05:00:00` is returned. We detect this case and pad the remaining zeros to ensure consistent cache version generation.
Before: Total allocated: 743842 bytes (6626 objects)
After: Total allocated: 702955 bytes (6063 objects)
(743842 - 702955) / 743842.0 # => 5.4% ⚡️⚡️⚡️⚡️⚡️
Using the CodeTriage application and derailed benchmarks this PR shows between 9-11% (statistically significant) performance improvement versus the commit before it.
Special thanks to @lsylvester for helping to figure out a way to preserve the usec format and for helping with many implementation details.
Diffstat (limited to 'activestorage/lib/active_storage/service.rb')
0 files changed, 0 insertions, 0 deletions