Skip to content
This repository
Browse code

Numerous performance improvements for datetimes and timespans

  • Loading branch information...
commit b713d9f09f601ed0db3e27bb3d31f9909a09450f 1 parent 43165d9
authored January 23, 2013
11  lib/twitter_cldr/formatters/calendars/datetime_formatter.rb
@@ -41,7 +41,12 @@ class DateTimeFormatter < Base
41 41
       }
42 42
 
43 43
       def initialize(options = {})
44  
-        @tokenizer = TwitterCldr::Tokenizers::DateTimeTokenizer.new(:locale => extract_locale(options), :calendar_type => options[:calendar_type])
  44
+        locale = extract_locale(options)
  45
+        cache_key = TwitterCldr::Utils.compute_cache_key(locale, options[:calendar_type])
  46
+        @tokenizer = tokenizer_cache[cache_key] ||= TwitterCldr::Tokenizers::DateTimeTokenizer.new(
  47
+          :locale => locale,
  48
+          :calendar_type => options[:calendar_type]
  49
+        )
45 50
       end
46 51
 
47 52
       def result_for_token(token, index, date)
@@ -58,6 +63,10 @@ def self.additional_formats_for(locale)
58 63
 
59 64
       protected
60 65
 
  66
+      def tokenizer_cache
  67
+        @@tokenizer_cache ||= {}
  68
+      end
  69
+
61 70
       # there is incomplete era data in CLDR for certain locales like Hindi
62 71
       # fall back if that happens
63 72
       def era(date, pattern, length)
7  lib/twitter_cldr/formatters/plurals/rules.rb
@@ -30,7 +30,12 @@ def rule_for(number, locale = TwitterCldr.locale)
30 30
 
31 31
           def get_resource(locale)
32 32
             locale = TwitterCldr.convert_locale(locale)
33  
-            eval(TwitterCldr.get_locale_resource(locale, :plurals)[locale])[locale][:i18n][:plural]
  33
+            cache_key = TwitterCldr::Utils.compute_cache_key(locale)
  34
+            locale_cache[cache_key] ||= eval(TwitterCldr.get_locale_resource(locale, :plurals)[locale])[locale][:i18n][:plural]
  35
+          end
  36
+
  37
+          def locale_cache
  38
+            @locale_cache ||= {}
34 39
           end
35 40
 
36 41
         end
19  lib/twitter_cldr/tokenizers/base.rb
@@ -54,10 +54,9 @@ def tokens_for(path, additional_cache_key_params = [])
54 54
       end
55 55
 
56 56
       def tokens_for_pattern(pattern, path, additional_cache_key_params = [])
57  
-        @@token_cache ||= {}
58 57
         cache_key = TwitterCldr::Utils.compute_cache_key(@locale, path.join('.'), type, format || "nil", *additional_cache_key_params)
59 58
 
60  
-        unless @@token_cache.include?(cache_key)
  59
+        unless token_cache.include?(cache_key)
61 60
           result = []
62 61
           tokens = expand_pattern(pattern)
63 62
 
@@ -69,25 +68,29 @@ def tokens_for_pattern(pattern, path, additional_cache_key_params = [])
69 68
             end
70 69
           end
71 70
 
72  
-          @@token_cache[cache_key] = result
  71
+          token_cache[cache_key] = result
73 72
         end
74 73
 
75  
-        @@token_cache[cache_key]
  74
+        token_cache[cache_key]
76 75
       end
77 76
 
78 77
       def tokens_with_placeholders_for(key)
79  
-        @@token_cache ||= {}
80 78
         cache_key = compute_cache_key(@locale, key, type)
81 79
 
82  
-        unless @@token_cache.include?(cache_key)
  80
+        unless token_cache.include?(cache_key)
83 81
           result = []
84 82
           tokens = tokenize_pattern(pattern_for(traverse(key)))
85 83
           tokens.each do |token|
86 84
             result << token
87 85
           end
88  
-          @@token_cache[cache_key] = result
  86
+          token_cache[cache_key] = result
89 87
         end
90  
-        @@token_cache[cache_key]
  88
+
  89
+        token_cache[cache_key]
  90
+      end
  91
+
  92
+      def token_cache
  93
+        @@token_cache ||= {}
91 94
       end
92 95
 
93 96
       def compute_cache_key(*pieces)
7  lib/twitter_cldr/tokenizers/calendars/additional_date_format_selector.rb
@@ -16,7 +16,8 @@ def find_closest(goal_pattern)
16 16
         if !goal_pattern || goal_pattern.strip.empty?
17 17
           nil
18 18
         else
19  
-          rank(goal_pattern).min do |(p1, score1), (p2, score2)|
  19
+          cache_key = TwitterCldr::Utils.compute_cache_key(goal_pattern)
  20
+          pattern_cache[cache_key] ||= rank(goal_pattern).min do |(p1, score1), (p2, score2)|
20 21
             score1 <=> score2
21 22
           end.first
22 23
         end
@@ -28,6 +29,10 @@ def patterns
28 29
 
29 30
       protected
30 31
 
  32
+      def pattern_cache
  33
+        @pattern_cache ||= {}
  34
+      end
  35
+
31 36
       def separate(pattern_key)
32 37
         last_char = ""
33 38
         pattern_key.each_char.each_with_index.inject([]) do |ret, (char, index)|
4  lib/twitter_cldr/tokenizers/calendars/datetime_tokenizer.rb
@@ -116,6 +116,10 @@ def pattern_for(resource)
116 116
             resource.is_a?(Hash) ? resource[:pattern] : resource
117 117
         end
118 118
       end
  119
+
  120
+      def pattern_cache
  121
+        @@pattern_cache ||= {}
  122
+      end
119 123
     end
120 124
   end
121 125
 end
16  lib/twitter_cldr/tokenizers/calendars/timespan_tokenizer.rb
@@ -58,18 +58,24 @@ def initialize(options = {})
58 58
       def tokens(options = {})
59 59
         path = full_path(options[:direction], options[:unit], options[:type])
60 60
         pluralization = options[:rule] || TwitterCldr::Formatters::Plurals::Rules.rule_for(options[:number], @locale)
  61
+        available = traverse(path)
61 62
 
62 63
         case pluralization # sometimes the plural rule will return ":one" when the resource only contains a path with "1"
63 64
           when :zero
64  
-            pluralization = 0 if token_exists(path + [0])
  65
+            pluralization = 0 if available.include?(0)
65 66
           when :one
66  
-            pluralization = 1 if token_exists(path + [1])
  67
+            pluralization = 1 if available.include?(1)
67 68
           when :two
68  
-            pluralization = 2 if token_exists(path + [2])
  69
+            pluralization = 2 if available.include?(2)
69 70
         end
70  
-        path << pluralization
71 71
 
72  
-        tokens_with_placeholders_for(path) if token_exists(path)
  72
+        if available.include?(pluralization)
  73
+          path << pluralization
  74
+        else
  75
+          path << available.keys.first
  76
+        end
  77
+
  78
+        tokens_with_placeholders_for(path)
73 79
       end
74 80
 
75 81
       def token_exists(path)

0 notes on commit b713d9f

Please sign in to comment.
Something went wrong with that request. Please try again.