Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bunch of speed and memory optimizations #527

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ def normalize_key(key, separator)
@@normalized_key_cache[separator][key] ||=
case key
when Array
key.map { |k| normalize_key(k, separator) }.flatten
key.flat_map { |k| normalize_key(k, separator) }
else
keys = key.to_s.split(separator)
keys.delete('')
Expand Down
10 changes: 6 additions & 4 deletions lib/i18n/locale/fallbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def initialize(*mappings)
end

def defaults=(defaults)
@defaults = defaults.map { |default| compute(default, false) }.flatten
@defaults = defaults.flat_map { |default| compute(default, false) }
end
attr_reader :defaults

Expand All @@ -84,13 +84,15 @@ def map(mappings)
protected

def compute(tags, include_defaults = true, exclude = [])
result = Array(tags).collect do |tag|
result = Array(tags).flat_map do |tag|
tags = I18n::Locale::Tag.tag(tag).self_and_parents.map! { |t| t.to_sym } - exclude
tags.each { |_tag| tags += compute(@map[_tag], false, exclude + tags) if @map[_tag] }
tags
end.flatten
end
result.push(*defaults) if include_defaults
result.uniq.compact
result.uniq!
result.compact!
result
end
end
end
Expand Down
14 changes: 8 additions & 6 deletions lib/i18n/locale/tag/parents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ module Locale
module Tag
module Parents
def parent
@parent ||= begin
segs = to_a.compact
segs.length > 1 ? self.class.tag(*segs[0..(segs.length-2)].join('-')) : nil
end
@parent ||=
begin
segs = to_a
segs.compact!
segs.length > 1 ? self.class.tag(*segs[0..(segs.length - 2)].join('-')) : nil
end
end

def self_and_parents
@self_and_parents ||= [self] + parents
@self_and_parents ||= [self].concat parents
end

def parents
@parents ||= ([parent] + (parent ? parent.parents : [])).compact
@parents ||= parent ? [parent].concat(parent.parents) : []
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/locale/tag/simple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def initialize(*tag)
end

def subtags
@subtags = tag.to_s.split('-').map { |subtag| subtag.to_s }
@subtags = tag.to_s.split('-').map!(&:to_s)
end

def to_sym
Expand Down