Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #9254 from warnickr/9253_avoid_bigdecimal_performa…

…nce_issue

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 http://www.ruby-forum.com/topic/4409452 and https://bugs.ruby-lang.org/issues/7645#change-35188).

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...
commit 1fd78305b5812c186d9eed9475677f90946eba5f 2 parents f36b80b + 0c0ee13
@carlosantoniodasilva carlosantoniodasilva authored
Showing with 5 additions and 4 deletions.
  1. +5 −4 activerecord/lib/active_record/connection_adapters/column.rb
View
9 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
0
- elsif value == true
+ when TrueClass
1
- elsif value.is_a?(String) && value.blank?
- nil
+ when String
+ value.presence
else
value
end
Please sign in to comment.
Something went wrong with that request. Please try again.