From 8c402ef425771c3dbb4659dd946714e69fdf5ce9 Mon Sep 17 00:00:00 2001
From: Andrew White <andrew.white@unboxed.co>
Date: Thu, 1 Sep 2016 11:28:35 +0100
Subject: Cache to_datetime for performance

As demonstrated in #25880 the to_time method converts the utc time
instance to a local time instance which is an expensive operation.
Since to_datetime involves similar expensive operations we should
also cache it to speed up comparison with lots of values.
---
 activesupport/lib/active_support/time_with_zone.rb | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

(limited to 'activesupport/lib')

diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb
index 74c4a39342..be2fceb123 100644
--- a/activesupport/lib/active_support/time_with_zone.rb
+++ b/activesupport/lib/active_support/time_with_zone.rb
@@ -406,7 +406,7 @@ module ActiveSupport
     #   Time.zone.now.to_datetime                         # => Tue, 18 Aug 2015 02:32:20 +0000
     #   Time.current.in_time_zone('Hawaii').to_datetime   # => Mon, 17 Aug 2015 16:32:20 -1000
     def to_datetime
-      utc.to_datetime.new_offset(Rational(utc_offset, 86_400))
+      @to_datetime ||= utc.to_datetime.new_offset(Rational(utc_offset, 86_400))
     end
 
     # So that +self+ <tt>acts_like?(:time)</tt>.
-- 
cgit v1.2.3