Skip to content

Commit 387a9b9

Browse files
authored
Memoize Locale.hashCode (flutter#33140)
* Started memoizing Locale hashCode. * docstring update * added comment
1 parent ea6f1b7 commit 387a9b9

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

lib/ui/fixtures/ui_test.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,14 @@ void hooksTests() {
450450
}
451451
});
452452

453+
test('deprecated region equals', () {
454+
// These are equal because ZR is deprecated and was mapped to CD.
455+
const Locale x = Locale('en', 'ZR');
456+
const Locale y = Locale('en', 'CD');
457+
expectEquals(x, y);
458+
expectEquals(x.countryCode, y.countryCode);
459+
});
460+
453461
test('Window padding/insets/viewPadding/systemGestureInsets', () {
454462
_callHook(
455463
'_updateWindowMetrics',

lib/ui/platform_dispatcher.dart

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,17 +1992,23 @@ class Locale {
19921992
if (other is! Locale) {
19931993
return false;
19941994
}
1995-
final String? countryCode = _countryCode;
1995+
final String? thisCountryCode = countryCode;
19961996
final String? otherCountryCode = other.countryCode;
19971997
return other.languageCode == languageCode
19981998
&& other.scriptCode == scriptCode // scriptCode cannot be ''
1999-
&& (other.countryCode == countryCode // Treat '' as equal to null.
2000-
|| otherCountryCode != null && otherCountryCode.isEmpty && countryCode == null
2001-
|| countryCode != null && countryCode.isEmpty && other.countryCode == null);
1999+
&& (other.countryCode == thisCountryCode // Treat '' as equal to null.
2000+
|| otherCountryCode != null && otherCountryCode.isEmpty && thisCountryCode == null
2001+
|| thisCountryCode != null && thisCountryCode.isEmpty && other.countryCode == null);
20022002
}
20032003

20042004
@override
2005-
int get hashCode => hashValues(languageCode, scriptCode, countryCode == '' ? null : countryCode);
2005+
int get hashCode => _hashCode[this] ??= hashValues(
2006+
languageCode,
2007+
scriptCode,
2008+
countryCode == '' ? null : countryCode,
2009+
);
2010+
// Memoize hashCode since languageCode and countryCode require lookups.
2011+
static final Expando<int> _hashCode = Expando<int>();
20062012

20072013
static Locale? _cachedLocale;
20082014
static String? _cachedLocaleString;

0 commit comments

Comments
 (0)