From dd0763ad5a25dc9b92b199c93e96b81f1dc16910 Mon Sep 17 00:00:00 2001 From: gaaclarke <30870216+gaaclarke@users.noreply.github.com> Date: Thu, 5 May 2022 13:52:27 -0700 Subject: [PATCH] Memoize Locale.hashCode (flutter/engine#33140) * Started memoizing Locale hashCode. * docstring update * added comment --- engine/src/flutter/lib/ui/fixtures/ui_test.dart | 8 ++++++++ .../src/flutter/lib/ui/platform_dispatcher.dart | 16 +++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/engine/src/flutter/lib/ui/fixtures/ui_test.dart b/engine/src/flutter/lib/ui/fixtures/ui_test.dart index 032e227b87..b0c7cdb697 100644 --- a/engine/src/flutter/lib/ui/fixtures/ui_test.dart +++ b/engine/src/flutter/lib/ui/fixtures/ui_test.dart @@ -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', diff --git a/engine/src/flutter/lib/ui/platform_dispatcher.dart b/engine/src/flutter/lib/ui/platform_dispatcher.dart index 0ae3d6ae49..ef22df4bae 100644 --- a/engine/src/flutter/lib/ui/platform_dispatcher.dart +++ b/engine/src/flutter/lib/ui/platform_dispatcher.dart @@ -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 _hashCode = Expando(); static Locale? _cachedLocale; static String? _cachedLocaleString;