Navigation Menu

Skip to content

Commit

Permalink
Cache to_time and to_datetime for performance
Browse files Browse the repository at this point in the history
As demonstrated in #25880 the to_time method converts the utc time
instance to a local time instance which is an expensive operation
so since ActiveSupport::TimeWithZone is a value object and shouldn't
be mutated we can safely cache it without worrying about invalidation.
  • Loading branch information
pixeltrix committed Aug 31, 2016
1 parent 0e6e4a1 commit c2c2f79
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Gemfile.lock
Expand Up @@ -293,3 +293,6 @@ DEPENDENCIES
turbolinks
uglifier (>= 1.3.0)
w3c_validators

BUNDLED WITH
1.12.5
4 changes: 2 additions & 2 deletions activesupport/lib/active_support/time_with_zone.rb
Expand Up @@ -318,11 +318,11 @@ def to_r

# Return an instance of Time in the system timezone.
def to_time
utc.to_time
@to_time ||= utc.to_time
end

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>.
Expand Down

0 comments on commit c2c2f79

Please sign in to comment.