CLDR plural rules in Swift, as a first step toward relative dates without ICU

Following up on What's in the 34 MB of ICU data in lib_FoundationICU.so, where moving formatting into Swift came up as a better path than trimming ICU data. I've been working on one piece of that and would like feedback before going further.

What

swiftlang/swift-foundation#2340 adds the CLDR plural rules ("1 day" vs "2 days") in Swift, as package API in _FoundationInternationalizationData, behind a FOUNDATION_PLURAL_RULES_SWIFT flag. It's a draft, and I'm happy to change its shape or approach.

The data pipeline follows the one in #1991 (list formatting) and #2330 (date patterns): a script reads CLDR XML and generates Swift tables, with --locales to keep only the locales an app needs. Nothing is parsed at run time.

Why

Plural rules are the first thing a Swift Date.RelativeFormatStyle needs ("in 2 days", "3 hours ago"). I have that working on a branch behind its own flag. It would be a separate PR once the plural rules are settled.

On their own, the rules don't make anything smaller: for all locales they take 41,048 bytes in a stripped Android aarch64 .so, close to ICU's plurals.res (35,584 bytes). The gain comes once formatters stop needing ICU and the data can be limited to an app's locales.

How it's checked

I compared it with ICU 76 on 53,762,071 combinations of number, locale and rule type. The only differences are:

  • integers above 2^53, where ICU goes through a Double and this code is exact (3,306);
  • rules that changed in CLDR 48 (3,810: cv, ie, kok, scn).

Questions

  1. Does this fit the plan for moving formatting off ICU, or is plural support already planned in another form?
  2. Is package API in _FoundationInternationalizationData the right place for it?
  3. Keep a flag until the first caller lands, or put the rules and relative dates in one PR?

IMO this is the wrong direction.

Firstly, you are introducing new flags that allow the behaviour of Foundation to differ in different environments. We should be looking to reduce the divergences and increasing the homogeneity of the implementation. If this means that the binary is larger on Android, that is either a price that must be paid or we should be looking at how we can reduce the size by improving the implementation.

Secondly, I suspect that once everything is simply reproduced in Swift, you will have the same data with a new implementation that is now divergent from the canonical definition that is shared nearly universally (AIUI, Windows, macOS, Linux all use the ICU CLDR for the data, and that is routinely updated).

If the ICU data size is the concern, perhaps the better approach here is to work with the Unicode Consortium to redefine how the CLDR is represented so that it is more compact on all targets.

5 Likes

Thanks for the feedback. I should have made the direction clearer: this follows what swift-foundation already does for list formatting. Since #1991, swift-foundation's package and CMake builds use a Swift implementation of ListFormatStyle with tables generated from CLDR by default, and ICU is only used when FOUNDATION_LIST_FORMAT_ICU is set. #2330 does the same for date patterns, behind a flag that goes away once it's complete. The plural rules are meant to follow that path, with data generated from CLDR the same way, not to add an Android-only variant.

You're right that size alone isn't a reason, though: the rules don't make anything smaller by themselves.

1 Like

ICU4X data might be smaller than ICU4C. There's a tutorial on how to reduce code size, etc. ICU4X is written in Rust, but there are also C and C++ APIs.

Thanks for pitching this idea! The idea about the list formatting is that we will be turning on the build flag for the packed list formatting everywhere, so we get the same behavior everywhere. We will then remove the data from the ICU we're dependent on so we don't have repeated data. This second step would require us to rework the logic of packaging the .dat file in swift-foundation-icu, so it's a bigger effort that we haven't gotten to.

I suspect that once everything is simply reproduced in Swift, you will have the same data with a new implementation that is now divergent from the canonical definition that is shared nearly universally (AIUI, Windows, macOS, Linux all use the ICU CLDR for the data, and that is routinely updated).

Regarding this concern, we will still make sure our implementation is compatible with the others, as well as making sure we keep the CLDR source data up to date.

Thanks, that makes the plan clear.

@cjchapman, you're working on reducing ICU use in formatting (#1991, #2330). Do you already plan plural rules? Relative dates, unit and duration formatting all need them, so I'd rather follow your plan than duplicate it. If not, I'll switch #2340 to DataLocaleIdentifier once #2330 lands.

My short-term plan is to only implement the ordinal rules for the ddd day-of-month field, generated into the date data in a follow-up PR after #2330 lands. That covers only ordinals for days 1-31 in the six languages that have ordinal day names, so it won't overlap much with #2340. If #2340 lands before I get to that, I can use it instead. Switching #2340 to DataLocaleIdentifier after #2330 lands sounds fine. Note that Tina mentioned in #2330 that she has a work-in-progress parser for keyword values in ICU-style locale identifiers and BCP 47 tags, which we'll consolidate with DataLocaleIdentifier.