Permalink
Browse files

Be more lazy about creating time zone objects. Decreases startup time…

… by about 10%. (#734)
  • Loading branch information...
1 parent 6c5f67c commit bfb9e61a9f12024ebe999216d4d17fdb53765883 @jonleighton jonleighton committed Aug 16, 2011
Showing with 13 additions and 2 deletions.
  1. +13 −2 activesupport/lib/active_support/values/time_zone.rb
@@ -337,7 +337,12 @@ def all
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 @@ def [](arg)
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 @@ def us_zones
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

0 comments on commit bfb9e61

Please sign in to comment.