Memoize Locale.hashCode (flutter/engine#33140)

* Started memoizing Locale hashCode.

* docstring update

* added comment
This commit is contained in:
gaaclarke
2022-05-05 13:52:27 -07:00
committed by GitHub
parent 2373205814
commit dd0763ad5a
2 changed files with 19 additions and 5 deletions

View File

@@ -450,6 +450,14 @@ void hooksTests() {
}
});
test('deprecated region equals', () {
// These are equal because ZR is deprecated and was mapped to CD.
const Locale x = Locale('en', 'ZR');
const Locale y = Locale('en', 'CD');
expectEquals(x, y);
expectEquals(x.countryCode, y.countryCode);
});
test('Window padding/insets/viewPadding/systemGestureInsets', () {
_callHook(
'_updateWindowMetrics',

View File

@@ -1992,17 +1992,23 @@ class Locale {
if (other is! Locale) {
return false;
}
final String? countryCode = _countryCode;
final String? thisCountryCode = countryCode;
final String? otherCountryCode = other.countryCode;
return other.languageCode == languageCode
&& other.scriptCode == scriptCode // scriptCode cannot be ''
&& (other.countryCode == countryCode // Treat '' as equal to null.
|| otherCountryCode != null && otherCountryCode.isEmpty && countryCode == null
|| countryCode != null && countryCode.isEmpty && other.countryCode == null);
&& (other.countryCode == thisCountryCode // Treat '' as equal to null.
|| otherCountryCode != null && otherCountryCode.isEmpty && thisCountryCode == null
|| thisCountryCode != null && thisCountryCode.isEmpty && other.countryCode == null);
}
@override
int get hashCode => hashValues(languageCode, scriptCode, countryCode == '' ? null : countryCode);
int get hashCode => _hashCode[this] ??= hashValues(
languageCode,
scriptCode,
countryCode == '' ? null : countryCode,
);
// Memoize hashCode since languageCode and countryCode require lookups.
static final Expando<int> _hashCode = Expando<int>();
static Locale? _cachedLocale;
static String? _cachedLocaleString;