aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGodfrey Chan <godfreykfc@gmail.com>2014-11-17 17:58:49 -0800
committerGodfrey Chan <godfreykfc@gmail.com>2014-11-17 17:58:49 -0800
commit6f7910aed5ceeb8833de760f396858bee917c7bc (patch)
treeffce3edf9c159cfec96371a544b8ababd94097ea
parenteb26f24bde62cbbcd8ef0e7ee9c64060b098baff (diff)
downloadrails-6f7910aed5ceeb8833de760f396858bee917c7bc.tar.gz
rails-6f7910aed5ceeb8833de760f396858bee917c7bc.tar.bz2
rails-6f7910aed5ceeb8833de760f396858bee917c7bc.zip
[PERF] Speed up integer type casting from DB
We don't have the check the range when the value is coming from the DB, so override type_cast_from_database to short-circuit the extra work. type_cast_from_database (small) 3437507.5 (±29.2%) i/s - 14223135 in 4.996973s type_cast_from_database (large) 3158588.7 (±28.3%) i/s - 13265628 in 4.992121s type_cast (small) 481984.8 (±14.2%) i/s - 2352012 in 5.005694s type_cast (large) 477331.8 (±14.2%) i/s - 2332824 in 5.012365s Comparison: type_cast_from_database (small): 3437507.5 i/s type_cast_from_database (large): 3158588.7 i/s - 1.09x slower type_cast (small): 481984.8 i/s - 7.13x slower type_cast (large): 477331.8 i/s - 7.20x slower The difference is huge but the absolute gain is quite small. That being said this is a hotspot and it showed up on the radar when benchmarking discourse.
-rw-r--r--activerecord/lib/active_record/type/integer.rb4
1 files changed, 3 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/type/integer.rb b/activerecord/lib/active_record/type/integer.rb
index d69e5b3f28..fb6d9df58f 100644
--- a/activerecord/lib/active_record/type/integer.rb
+++ b/activerecord/lib/active_record/type/integer.rb
@@ -12,7 +12,9 @@ module ActiveRecord
:integer
end
- alias type_cast_for_database type_cast
+ def type_cast_from_database(value)
+ value.to_i unless value.nil?
+ end
protected