From 0db9c83a604b19e764293d0cb8cfe4b982497278 Mon Sep 17 00:00:00 2001 From: Ankit Gupta Date: Sun, 10 Apr 2016 16:51:45 -0700 Subject: [PATCH 1/2] Freezing the dots as they are causing extra string initialization on every iteration of calling the helper. Eases on some memory bloat --- .../number_helper/number_to_delimited_converter.rb | 2 +- .../active_support/number_helper/number_to_rounded_converter.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/activesupport/lib/active_support/number_helper/number_to_delimited_converter.rb b/activesupport/lib/active_support/number_helper/number_to_delimited_converter.rb index 45ae8f1a93..43c5540b6f 100644 --- a/activesupport/lib/active_support/number_helper/number_to_delimited_converter.rb +++ b/activesupport/lib/active_support/number_helper/number_to_delimited_converter.rb @@ -12,7 +12,7 @@ module ActiveSupport private def parts - left, right = number.to_s.split('.') + left, right = number.to_s.split('.'.freeze) left.gsub!(delimiter_pattern) do |digit_to_delimit| "#{digit_to_delimit}#{options[:delimiter]}" end diff --git a/activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb b/activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb index 981c562551..21e4fd672f 100644 --- a/activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb +++ b/activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb @@ -30,7 +30,7 @@ module ActiveSupport formatted_string = if BigDecimal === rounded_number && rounded_number.finite? s = rounded_number.to_s('F') + '0'*precision - a, b = s.split('.', 2) + a, b = s.split('.'.freeze, 2) a + '.' + b[0, precision] else "%00.#{precision}f" % rounded_number From 9c30e1b167054aa57e56e1df5a9e151c70332522 Mon Sep 17 00:00:00 2001 From: Ankit Gupta Date: Mon, 11 Apr 2016 13:25:04 -0700 Subject: [PATCH 2/2] Freezing couple of more string '0' & '.' and using the string appending to do the same string manipulation. This was we avoid the duplicate strings with freeze and append modifies existing string --- .../number_helper/number_to_rounded_converter.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb b/activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb index 21e4fd672f..9fb7dfb779 100644 --- a/activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb +++ b/activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb @@ -29,9 +29,11 @@ module ActiveSupport formatted_string = if BigDecimal === rounded_number && rounded_number.finite? - s = rounded_number.to_s('F') + '0'*precision + s = rounded_number.to_s('F') + s << '0'.freeze * precision a, b = s.split('.'.freeze, 2) - a + '.' + b[0, precision] + a << '.'.freeze + a << b[0, precision] else "%00.#{precision}f" % rounded_number end