Skip to content

Commit

Permalink
Switch to Concurrent::Map + use it in other places
Browse files Browse the repository at this point in the history
  • Loading branch information
radar committed Oct 1, 2017
1 parent 6d2ec72 commit 3da644f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
9 changes: 7 additions & 2 deletions lib/i18n.rb
@@ -1,4 +1,4 @@
require 'concurrent/hash'
require 'concurrent/map'

require 'i18n/version'
require 'i18n/exceptions'
Expand All @@ -14,6 +14,11 @@ module I18n
RESERVED_KEYS = [:scope, :default, :separator, :resolve, :object, :fallback, :fallback_in_progress, :format, :cascade, :throw, :raise, :deep_interpolation]
RESERVED_KEYS_PATTERN = /%\{(#{RESERVED_KEYS.join("|")})\}/


def self.new_double_nested_cache # :nodoc:
Concurrent::Map.new { |h,k| h[k] = Concurrent::Map.new }
end

module Base
# Gets I18n configuration object.
def config
Expand Down Expand Up @@ -323,7 +328,7 @@ def handle_exception(handling, exception, locale, key, options)
end
end

@@normalized_key_cache = Concurrent::Hash.new { |h, k| h[k] = Concurrent::Hash.new }
@@normalized_key_cache = I18n.new_double_nested_cache

def normalize_key(key, separator)
@@normalized_key_cache[separator][key] ||=
Expand Down
6 changes: 3 additions & 3 deletions lib/i18n/backend/flatten.rb
Expand Up @@ -43,7 +43,7 @@ def normalize_flat_keys(locale, key, scope, separator)

# Store flattened links.
def links
@links ||= Hash.new { |h,k| h[k] = {} }
@links ||= I18n.new_double_nested_cache
end

# Flatten keys for nested Hashes by chaining up keys:
Expand Down Expand Up @@ -99,7 +99,7 @@ def resolve_link(locale, key)
end

def find_link(locale, key) #:nodoc:
links[locale].each do |from, to|
links[locale].each_pair do |from, to|
return [from, to] if key[0, from.length] == from
end && nil
end
Expand All @@ -110,4 +110,4 @@ def escape_default_separator(key) #:nodoc:

end
end
end
end
2 changes: 1 addition & 1 deletion lib/i18n/backend/memoize.rb
Expand Up @@ -34,7 +34,7 @@ def lookup(locale, key, scope = nil, options = {})
end

def memoized_lookup
@memoized_lookup ||= Hash.new { |h, k| h[k] = {} }
@memoized_lookup ||= I18n.new_double_nested_cache
end

def reset_memoizations!(locale=nil)
Expand Down

0 comments on commit 3da644f

Please sign in to comment.