Skip to content

Commit

Permalink
Memoize Locale.hashCode (flutter#33140)
Browse files Browse the repository at this point in the history
* Started memoizing Locale hashCode.

* docstring update

* added comment
  • Loading branch information
gaaclarke authored May 5, 2022
1 parent ea6f1b7 commit 387a9b9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
8 changes: 8 additions & 0 deletions lib/ui/fixtures/ui_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
16 changes: 11 additions & 5 deletions lib/ui/platform_dispatcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 387a9b9

Please sign in to comment.