Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Switched Locale deprecation maps to jumptables and fixed minor bug with deprecated countries #33123

Closed
wants to merge 1 commit into from

Conversation

gaaclarke
Copy link
Member

  1. Fixed bug with Locale where deprecated countries would return all the same data, but the would not be considered ==
  2. Switched the const Map data representing the deprecated languages and countries to jump tables. In microbenchmarks it behaves 2x faster in jit x64.
  3. I reordered the == operator to prefer less lookup calls to deprecated tables.

I made this change because Locale is sometimes used as a key into a map, as it is in Flutter gallery. When it is used in a Map it's hashCode is calculated, which is based on the country code and the language code, which requires lookups into these maps. I tried to memoize the values and the hashCode, but because of the limitations of Dart I couldn't do that (dart-lang/sdk#48948).

The reason I identified this as a potential performance fix is that I setup Flutter to continuously build frames and navigated through the Material section of Flutter gallery and noticed that Locale.hashCode shows up pretty high. I also saw other Flutter users in Google using Locale as a key.
Screen Shot 2022-05-03 at 10 47 11 AM

Note:

  1. I don't expect this to make a difference in customer: money.
  2. While this is verbose code, it should actually compile to smaller binary size.

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide and the C++, Objective-C, Java style guides.
  • I listed at least one issue that this PR fixes in the description above.
  • I added new tests to check the change I am making or feature I am adding, or Hixie said the PR is test-exempt. See testing the engine for instructions on
    writing and running engine tests.
  • I updated/added relevant documentation (doc comments with ///).
  • I signed the CLA.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

Copy link
Contributor

@dnfield dnfield left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this fater in AOT or just JIT?


// This map is generated by //flutter/tools/gen_locale.dart
// Mappings generated for language subtag registry as of 2019-02-27.
// static const Map<String, String> _deprecatedLanguageSubtagMap = <String, String>{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add something about this map being a more readable version of the jump table below.

return x;
}

// This map is generated by //flutter/tools/gen_locale.dart
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably replace that file with your gist, no?

@gaaclarke
Copy link
Member Author

@dnfield, slava pointed out that we don't actually compile to jump tables with Dart, so the performance isn't always going to be faster than a map. I can switch it to a proper jump table like lex does but it's a bit harder to read and know it's correct. I'm on the fence about wether it's worth doing. I'm going to pull this out of review and think about it.

@gaaclarke gaaclarke marked this pull request as draft May 5, 2022 16:05
@gaaclarke
Copy link
Member Author

Abandoned for the alternative: #33140

@gaaclarke gaaclarke closed this May 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants