Browse files

Merge pull request #9254 from warnickr/9253_avoid_bigdecimal_performa…


Before writing a numeric attribute value, ActiveRecord does an implicit conversion of boolean types (true => 1 and false => 0). If the numeric value being assigned is a BigDecimal, then ActiveRecord compares a BigDecimal to true and false.

This is known to be very slow in Ruby 1.9.3 (see and

The Ruby core team has implemented a fix for this issue that will be included in Ruby 2, but they appear to have no intention of back-porting this fix to Ruby 1.9.3. In my case, the performance impact of this issue was enormous (200% - 300% performance hit) for some of the pages in my application.

See c75b5a8 applied to 3-2-stable branch for more reasoning and a benchmark.
  • Loading branch information...
2 parents f36b80b + 0c0ee13 commit 1fd78305b5812c186d9eed9475677f90946eba5f @carlosantoniodasilva carlosantoniodasilva committed Feb 14, 2013
Showing with 5 additions and 4 deletions.
  1. +5 −4 activerecord/lib/active_record/connection_adapters/column.rb
@@ -74,12 +74,13 @@ def binary?
def type_cast_for_write(value)
return value unless number?
- if value == false
+ case value
+ when FalseClass
- elsif value == true
+ when TrueClass
- elsif value.is_a?(String) && value.blank?
- nil
+ when String
+ value.presence

0 comments on commit 1fd7830

Please sign in to comment.