From a295dba82d68680c079a8b36e93bade1d667b908 Mon Sep 17 00:00:00 2001 From: Kazato Sugimoto Date: Fri, 26 Apr 2024 20:45:45 +0900 Subject: [PATCH 1/3] Optimize I18n::Locale::Fallbacks#compute --- lib/i18n/locale/fallbacks.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/i18n/locale/fallbacks.rb b/lib/i18n/locale/fallbacks.rb index 14744683..79c63dbe 100644 --- a/lib/i18n/locale/fallbacks.rb +++ b/lib/i18n/locale/fallbacks.rb @@ -90,11 +90,16 @@ def inspect protected def compute(tags, include_defaults = true, exclude = []) - 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 + tags = Array(tags).flat_map do |tag| + I18n::Locale::Tag.tag(tag).self_and_parents.map! { |t| t.to_sym } end + tags -= exclude + + result = tags + tags.each do |tag| + result += compute(@map[tag], false, exclude + result) if @map[tag] + end + result.push(*defaults) if include_defaults result.uniq! result.compact! From 3d19b689afdf59a2ebf7a8b0bbac22bafe73524f Mon Sep 17 00:00:00 2001 From: Kazato Sugimoto Date: Fri, 26 Apr 2024 23:04:54 +0900 Subject: [PATCH 2/3] Use left recursion --- lib/i18n/locale/fallbacks.rb | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/i18n/locale/fallbacks.rb b/lib/i18n/locale/fallbacks.rb index 79c63dbe..5106c03e 100644 --- a/lib/i18n/locale/fallbacks.rb +++ b/lib/i18n/locale/fallbacks.rb @@ -90,14 +90,12 @@ def inspect protected def compute(tags, include_defaults = true, exclude = []) - tags = Array(tags).flat_map do |tag| - I18n::Locale::Tag.tag(tag).self_and_parents.map! { |t| t.to_sym } - end - tags -= exclude - - result = tags - tags.each do |tag| - result += compute(@map[tag], false, exclude + result) if @map[tag] + result = [] + Array(tags).each do |tag| + tags = I18n::Locale::Tag.tag(tag).self_and_parents.map! { |t| t.to_sym } - exclude + result += tags + tags.each { |_tag| result += compute(@map[_tag], false, exclude + result) if @map[_tag] } + result end result.push(*defaults) if include_defaults From fb8e34e1af6bb10e4a1ef39e66a3eae66b12c1d2 Mon Sep 17 00:00:00 2001 From: Kazato Sugimoto Date: Sat, 27 Apr 2024 09:24:31 +0900 Subject: [PATCH 3/3] Remove unnecessary line --- lib/i18n/locale/fallbacks.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/i18n/locale/fallbacks.rb b/lib/i18n/locale/fallbacks.rb index 5106c03e..56d08d71 100644 --- a/lib/i18n/locale/fallbacks.rb +++ b/lib/i18n/locale/fallbacks.rb @@ -95,7 +95,6 @@ def compute(tags, include_defaults = true, exclude = []) tags = I18n::Locale::Tag.tag(tag).self_and_parents.map! { |t| t.to_sym } - exclude result += tags tags.each { |_tag| result += compute(@map[_tag], false, exclude + result) if @map[_tag] } - result end result.push(*defaults) if include_defaults