From bfb9e61a9f12024ebe999216d4d17fdb53765883 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Tue, 16 Aug 2011 01:33:34 +0100 Subject: Be more lazy about creating time zone objects. Decreases startup time by about 10%. (#734) --- activesupport/lib/active_support/values/time_zone.rb | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'activesupport/lib/active_support/values/time_zone.rb') diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index 728921a069..323d5d3df7 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -337,7 +337,12 @@ module ActiveSupport end def zones_map - @zones_map ||= Hash[MAPPING.map { |place, _| [place, create(place)] }] + @zones_map ||= begin + new_zones_names = MAPPING.keys - lazy_zones_map.keys + new_zones = Hash[new_zones_names.map { |place| [place, create(place)] }] + + lazy_zones_map.merge(new_zones) + end end # Locate a specific time zone object. If the argument is a string, it @@ -349,7 +354,7 @@ module ActiveSupport case arg when String begin - zones_map[arg] ||= lookup(arg).tap { |tz| tz.utc_offset } + lazy_zones_map[arg] ||= lookup(arg).tap { |tz| tz.utc_offset } rescue TZInfo::InvalidTimezoneIdentifier nil end @@ -372,6 +377,12 @@ module ActiveSupport def lookup(name) (tzinfo = find_tzinfo(name)) && create(tzinfo.name.freeze) end + + def lazy_zones_map + @lazy_zones_map ||= Hash.new do |hash, place| + hash[place] = create(place) if MAPPING.has_key?(place) + end + end end end end -- cgit v1.2.3 From 0d3615f04c79f6e90d8ab33fdfc920b8faac9cb8 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Tue, 16 Aug 2011 02:42:30 +0100 Subject: Fix tzinfo require (it broke test_raises_when_an_invalid_timezone_is_defined_in_the_config in railties) --- activesupport/lib/active_support/values/time_zone.rb | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'activesupport/lib/active_support/values/time_zone.rb') diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index 323d5d3df7..4fb487ade1 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -195,12 +195,8 @@ module ActiveSupport # (GMT). Seconds were chosen as the offset unit because that is the unit that # Ruby uses to represent time zone offsets (see Time#utc_offset). def initialize(name, utc_offset = nil, tzinfo = nil) - begin - require 'tzinfo' - rescue LoadError => e - $stderr.puts "You don't have tzinfo installed in your application. Please add it to your Gemfile and run bundle install" - raise e - end + self.class.send(:require_tzinfo) + @name = name @utc_offset = utc_offset @tzinfo = tzinfo || TimeZone.find_tzinfo(name) @@ -372,6 +368,15 @@ module ActiveSupport @us_zones ||= all.find_all { |z| z.name =~ /US|Arizona|Indiana|Hawaii|Alaska/ } end + protected + + def require_tzinfo + require 'tzinfo' + rescue LoadError + $stderr.puts "You don't have tzinfo installed in your application. Please add it to your Gemfile and run bundle install" + raise + end + private def lookup(name) @@ -379,6 +384,8 @@ module ActiveSupport end def lazy_zones_map + require_tzinfo + @lazy_zones_map ||= Hash.new do |hash, place| hash[place] = create(place) if MAPPING.has_key?(place) end -- cgit v1.2.3 From 2147f35df251f215d845e870f21fd4010976eba3 Mon Sep 17 00:00:00 2001 From: Tim Lucas Date: Wed, 7 Sep 2011 14:21:18 +0300 Subject: Make sure require_tzinfo only calls Kernel#require if TZInfo isn't already present. This wasn't a problem when require_tzinfo was only called from TimeZone#initialize, but now it's being called for every lookup to TimeZone.[] (via lazy_zones_map) TimeZone lookup can occur when unmarshalling TimeWithZone objects, which is where I first saw the big slowdown (500ms for each Rails action that loaded from Rails.cache) --- activesupport/lib/active_support/values/time_zone.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activesupport/lib/active_support/values/time_zone.rb') diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index 4fb487ade1..35f400f9df 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -371,7 +371,7 @@ module ActiveSupport protected def require_tzinfo - require 'tzinfo' + require 'tzinfo' unless defined?(::TZInfo) rescue LoadError $stderr.puts "You don't have tzinfo installed in your application. Please add it to your Gemfile and run bundle install" raise -- cgit v1.2.3