From 6ba38858f691f014e3a258efc74207c215c9fa39 Mon Sep 17 00:00:00 2001 From: Haoyu Qiu Date: Wed, 16 Oct 2024 20:23:59 +0800 Subject: [PATCH] Cache results for `TranslationServer.compare_locales()` (cherry picked from commit 7ebb63628d01a1d42a354c33a2898c0f4cd4c542) --- core/translation.cpp | 40 +++++++++++++++++++++++++++------------- core/translation.h | 2 ++ 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/core/translation.cpp b/core/translation.cpp index 4ad17f1d159..0db222494e7 100644 --- a/core/translation.cpp +++ b/core/translation.cpp @@ -359,32 +359,46 @@ String TranslationServer::standardize_locale(const String &p_locale) const { } int TranslationServer::compare_locales(const String &p_locale_a, const String &p_locale_b) const { + if (p_locale_a == p_locale_b) { + // Exact match. + return 10; + } + + const String cache_key = p_locale_a + "|" + p_locale_b; + const int *cached_result = locale_compare_cache.getptr(cache_key); + if (cached_result) { + return *cached_result; + } + String locale_a = standardize_locale(p_locale_a); String locale_b = standardize_locale(p_locale_b); if (locale_a == locale_b) { // Exact match. + locale_compare_cache.set(cache_key, 10); return 10; } Vector locale_a_elements = locale_a.split("_"); Vector locale_b_elements = locale_b.split("_"); - if (locale_a_elements[0] == locale_b_elements[0]) { - // Matching language, both locales have extra parts. - // Return number of matching elements. - int matching_elements = 1; - for (int i = 1; i < locale_a_elements.size(); i++) { - for (int j = 1; j < locale_b_elements.size(); j++) { - if (locale_a_elements[i] == locale_b_elements[j]) { - matching_elements++; - } - } - } - return matching_elements; - } else { + if (locale_a_elements[0] != locale_b_elements[0]) { // No match. + locale_compare_cache.set(cache_key, 0); return 0; } + + // Matching language, both locales have extra parts. + // Return number of matching elements. + int matching_elements = 1; + for (int i = 1; i < locale_a_elements.size(); i++) { + for (int j = 1; j < locale_b_elements.size(); j++) { + if (locale_a_elements[i] == locale_b_elements[j]) { + matching_elements++; + } + } + } + locale_compare_cache.set(cache_key, matching_elements); + return matching_elements; } String TranslationServer::get_locale_name(const String &p_locale) const { diff --git a/core/translation.h b/core/translation.h index d4b2b0fb5fa..815c0409e57 100644 --- a/core/translation.h +++ b/core/translation.h @@ -87,6 +87,8 @@ class TranslationServer : public Object { Ref tool_translation; Ref doc_translation; + mutable HashMap locale_compare_cache; + bool enabled; static TranslationServer *singleton;