Skip to content

Commit

Permalink
Help illinker remove all ICU dependencies in Invariant mode (#37832)
Browse files Browse the repository at this point in the history
* Help illinker remove all ICU dependencies in Invariant mode

Data for browser-wasm hello world
|Mode  | SPC Size | Delta
|-|-|-|
|Default| 1449 KB | |
|Invariant| 1345 KB| -50 KB |

* Review feedback
  • Loading branch information
marek-safar authored Jun 16, 2020
1 parent ea90451 commit bd7b3c5
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -585,13 +585,11 @@ internal static int CompareOrdinalIgnoreCase(ref char strA, int lengthA, ref cha
}
}

if (length == 0)
if (length == 0 || GlobalizationMode.Invariant)
{
return lengthA - lengthB;
}

Debug.Assert(!GlobalizationMode.Invariant);

range -= length;

return CompareStringOrdinalIgnoreCaseCore(ref charA, lengthA - range, ref charB, lengthB - range);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ internal string DisplayName
get
{
string? localizedDisplayName = _sLocalizedDisplayName;
if (localizedDisplayName == null)
if (localizedDisplayName == null && !GlobalizationMode.Invariant)
{
if (IsSupplementalCustomCulture)
{
Expand Down Expand Up @@ -995,7 +995,7 @@ internal string DisplayName
_sLocalizedDisplayName = localizedDisplayName;
}

return localizedDisplayName;
return localizedDisplayName!;
}
}

Expand All @@ -1011,7 +1011,7 @@ internal string EnglishName
get
{
string? englishDisplayName = _sEnglishDisplayName;
if (englishDisplayName == null)
if (englishDisplayName == null && !GlobalizationMode.Invariant)
{
// If its neutral use the language name
if (IsNeutralCulture)
Expand Down Expand Up @@ -1056,7 +1056,7 @@ internal string EnglishName
_sEnglishDisplayName = englishDisplayName;
}

return englishDisplayName;
return englishDisplayName!;
}
}

Expand All @@ -1068,7 +1068,7 @@ internal string NativeName
get
{
string? nativeDisplayName = _sNativeDisplayName;
if (nativeDisplayName == null)
if (nativeDisplayName == null && !GlobalizationMode.Invariant)
{
// If its neutral use the language name
if (IsNeutralCulture)
Expand Down Expand Up @@ -1100,7 +1100,7 @@ internal string NativeName
_sNativeDisplayName = nativeDisplayName;
}

return nativeDisplayName;
return nativeDisplayName!;
}
}

Expand Down Expand Up @@ -1142,7 +1142,7 @@ private string LocalizedLanguageName
{
get
{
if (_sLocalizedLanguage == null)
if (_sLocalizedLanguage == null && !GlobalizationMode.Invariant)
{
// Usually the UI culture shouldn't be different than what we got from WinRT except
// if DefaultThreadCurrentUICulture was set
Expand All @@ -1160,7 +1160,7 @@ private string LocalizedLanguageName
}
}

return _sLocalizedLanguage;
return _sLocalizedLanguage!;
}
}

Expand All @@ -1183,7 +1183,7 @@ internal int GeoId
{
get
{
if (_iGeoId == undef)
if (_iGeoId == undef && !GlobalizationMode.Invariant)
{
_iGeoId = GlobalizationMode.UseNls ? NlsGetGeoId(_sRealName!) : IcuGetGeoId(_sRealName!);
}
Expand All @@ -1199,7 +1199,7 @@ internal string LocalizedCountryName
get
{
string? localizedCountry = _sLocalizedCountry;
if (localizedCountry == null)
if (localizedCountry == null && !GlobalizationMode.Invariant)
{
try
{
Expand All @@ -1214,7 +1214,7 @@ internal string LocalizedCountryName
_sLocalizedCountry = localizedCountry;
}

return localizedCountry;
return localizedCountry!;
}
}

Expand Down Expand Up @@ -1389,7 +1389,7 @@ internal string[] LongTimes
{
get
{
if (_saLongTimes == null)
if (_saLongTimes == null && !GlobalizationMode.Invariant)
{
Debug.Assert(!GlobalizationMode.Invariant);

Expand All @@ -1403,7 +1403,7 @@ internal string[] LongTimes
_saLongTimes = longTimes;
}
}
return _saLongTimes;
return _saLongTimes!;
}
}

Expand All @@ -1415,7 +1415,7 @@ internal string[] ShortTimes
{
get
{
if (_saShortTimes == null)
if (_saShortTimes == null && !GlobalizationMode.Invariant)
{
Debug.Assert(!GlobalizationMode.Invariant);

Expand All @@ -1434,7 +1434,7 @@ internal string[] ShortTimes
// Found short times, use them
_saShortTimes = shortTimes;
}
return _saShortTimes;
return _saShortTimes!;
}
}

Expand Down Expand Up @@ -1569,7 +1569,7 @@ internal int FirstDayOfWeek
{
get
{
if (_iFirstDayOfWeek == undef)
if (_iFirstDayOfWeek == undef && !GlobalizationMode.Invariant)
{
_iFirstDayOfWeek = GlobalizationMode.UseNls ? NlsGetFirstDayOfWeek() : IcuGetFirstDayOfWeek();
}
Expand Down Expand Up @@ -1801,7 +1801,7 @@ private int ReadingLayout
{
get
{
if (_iReadingLayout == undef)
if (_iReadingLayout == undef && !GlobalizationMode.Invariant)
{
Debug.Assert(_sRealName != null, "[CultureData.IsRightToLeft] Expected _sRealName to be populated by already");
_iReadingLayout = GetLocaleInfoCore(LocaleNumberData.ReadingLayout);
Expand Down Expand Up @@ -1853,7 +1853,7 @@ internal int ANSICodePage
{
get
{
if (_iDefaultAnsiCodePage == undef)
if (_iDefaultAnsiCodePage == undef && !GlobalizationMode.Invariant)
{
_iDefaultAnsiCodePage = GlobalizationMode.UseNls ? NlsGetAnsiCodePage(_sRealName!) : IcuGetAnsiCodePage(_sRealName!);
}
Expand All @@ -1868,7 +1868,7 @@ internal int OEMCodePage
{
get
{
if (_iDefaultOemCodePage == undef)
if (_iDefaultOemCodePage == undef && !GlobalizationMode.Invariant)
{
_iDefaultOemCodePage = GlobalizationMode.UseNls ? NlsGetOemCodePage(_sRealName!) : IcuGetOemCodePage(_sRealName!);
}
Expand All @@ -1883,7 +1883,7 @@ internal int MacCodePage
{
get
{
if (_iDefaultMacCodePage == undef)
if (_iDefaultMacCodePage == undef && !GlobalizationMode.Invariant)
{
_iDefaultMacCodePage = GlobalizationMode.UseNls ? NlsGetMacCodePage(_sRealName!) : IcuGetMacCodePage(_sRealName!);
}
Expand All @@ -1898,7 +1898,7 @@ internal int EBCDICCodePage
{
get
{
if (_iDefaultEbcdicCodePage == undef)
if (_iDefaultEbcdicCodePage == undef && !GlobalizationMode.Invariant)
{
_iDefaultEbcdicCodePage = GlobalizationMode.UseNls ? NlsGetEbcdicCodePage(_sRealName!) : IcuGetEbcdicCodePage(_sRealName!);
}
Expand All @@ -1910,7 +1910,7 @@ internal int LCID
{
get
{
if (_iLanguage == 0)
if (_iLanguage == 0 && !GlobalizationMode.Invariant)
{
Debug.Assert(_sRealName != null, "[CultureData.LCID] Expected this.sRealName to be populated already");
_iLanguage = GlobalizationMode.UseNls ? NlsLocaleNameToLCID(_sRealName) : IcuLocaleNameToLCID(_sRealName);
Expand Down Expand Up @@ -1978,7 +1978,7 @@ internal string TimeSeparator
{
get
{
if (_sTimeSeparator == null)
if (_sTimeSeparator == null && !GlobalizationMode.Invariant)
{
string? longTimeFormat = GlobalizationMode.UseNls ? NlsGetTimeFormatString() : IcuGetTimeFormatString();
if (string.IsNullOrEmpty(longTimeFormat))
Expand All @@ -1989,7 +1989,7 @@ internal string TimeSeparator
// Compute STIME from time format
_sTimeSeparator = GetTimeSeparator(longTimeFormat);
}
return _sTimeSeparator;
return _sTimeSeparator!;
}
}

Expand All @@ -1998,6 +1998,11 @@ internal string TimeSeparator
/// </summary>
internal string DateSeparator(CalendarId calendarId)
{
if (GlobalizationMode.Invariant)
{
return "/";
}

if (calendarId == CalendarId.JAPAN && !LocalAppContextSwitches.EnforceLegacyJapaneseDateParsing)
{
// The date separator is derived from the default short date pattern. So far this pattern is using
Expand Down Expand Up @@ -2252,21 +2257,41 @@ internal void GetNFIValues(NumberFormatInfo nfi)
/// </remarks>
internal static string AnsiToLower(string testString) => TextInfo.ToLowerAsciiInvariant(testString);

private int GetLocaleInfoCore(LocaleNumberData type) => GlobalizationMode.UseNls ?
NlsGetLocaleInfo(type) :
IcuGetLocaleInfo(type);
private int GetLocaleInfoCore(LocaleNumberData type)
{
// This is never reached but helps illinker statically remove dependencies
if (GlobalizationMode.Invariant)
return 0;

private string GetLocaleInfoCore(LocaleStringData type) => GlobalizationMode.UseNls ?
NlsGetLocaleInfo(type) :
IcuGetLocaleInfo(type);
return GlobalizationMode.UseNls ? NlsGetLocaleInfo(type) : IcuGetLocaleInfo(type);
}

private string GetLocaleInfoCore(string localeName, LocaleStringData type) => GlobalizationMode.UseNls ?
NlsGetLocaleInfo(localeName, type) :
IcuGetLocaleInfo(localeName, type);
private string GetLocaleInfoCore(LocaleStringData type)
{
// This is never reached but helps illinker statically remove dependencies
if (GlobalizationMode.Invariant)
return null!;

private int[] GetLocaleInfoCore(LocaleGroupingData type) => GlobalizationMode.UseNls ?
NlsGetLocaleInfo(type) :
IcuGetLocaleInfo(type);
return GlobalizationMode.UseNls ? NlsGetLocaleInfo(type) : IcuGetLocaleInfo(type);
}

private string GetLocaleInfoCore(string localeName, LocaleStringData type)
{
// This is never reached but helps illinker statically remove dependencies
if (GlobalizationMode.Invariant)
return null!;

return GlobalizationMode.UseNls ? NlsGetLocaleInfo(localeName, type) : IcuGetLocaleInfo(localeName, type);
}

private int[] GetLocaleInfoCore(LocaleGroupingData type)
{
// This is never reached but helps illinker statically remove dependencies
if (GlobalizationMode.Invariant)
return null!;

return GlobalizationMode.UseNls ? NlsGetLocaleInfo(type) : IcuGetLocaleInfo(type);
}

/// <remarks>
/// The numeric values of the enum members match their Win32 counterparts. The CultureData Win32 PAL implementation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2037,8 +2037,6 @@ internal TokenHashValue[] CreateTokenHashTable()
{
temp = new TokenHashValue[TOKEN_HASH_SIZE];

bool koreanLanguage = LanguageName.Equals(KoreanLangName);

string sep = TimeSeparator.Trim();
if (IgnorableComma != sep) InsertHash(temp, IgnorableComma, TokenType.IgnorableSymbol, 0);
if (IgnorablePeriod != sep) InsertHash(temp, IgnorablePeriod, TokenType.IgnorableSymbol, 0);
Expand Down Expand Up @@ -2087,7 +2085,7 @@ internal TokenHashValue[] CreateTokenHashTable()
}

// TODO: This ignores other custom cultures that might want to do something similar
if (koreanLanguage)
if (LanguageName.Equals(KoreanLangName))
{
// Korean suffix
InsertHash(temp, KoreanHourSuff, TokenType.SEP_HourSuff, 0);
Expand Down Expand Up @@ -2206,37 +2204,40 @@ internal TokenHashValue[] CreateTokenHashTable()
InsertHash(temp, GetAbbreviatedEraName(i), TokenType.EraToken, i);
}

if (LanguageName.Equals(JapaneseLangName))
if (!GlobalizationMode.Invariant)
{
// Japanese allows day of week forms like: "(Tue)"
for (int i = 0; i < 7; i++)
{
string specialDayOfWeek = "(" + GetAbbreviatedDayName((DayOfWeek)i) + ")";
InsertHash(temp, specialDayOfWeek, TokenType.DayOfWeekToken, i);
}
if (Calendar.GetType() != typeof(JapaneseCalendar))
if (LanguageName.Equals(JapaneseLangName))
{
// Special case for Japanese. If this is a Japanese DTFI, and the calendar is not Japanese calendar,
// we will check Japanese Era name as well when the calendar is Gregorian.
DateTimeFormatInfo jaDtfi = GetJapaneseCalendarDTFI();
for (int i = 1; i <= jaDtfi.Calendar.Eras.Length; i++)
// Japanese allows day of week forms like: "(Tue)"
for (int i = 0; i < 7; i++)
{
InsertHash(temp, jaDtfi.GetEraName(i), TokenType.JapaneseEraToken, i);
InsertHash(temp, jaDtfi.GetAbbreviatedEraName(i), TokenType.JapaneseEraToken, i);
// m_abbrevEnglishEraNames[0] contains the name for era 1, so the token value is i+1.
InsertHash(temp, jaDtfi.AbbreviatedEnglishEraNames[i - 1], TokenType.JapaneseEraToken, i);
string specialDayOfWeek = "(" + GetAbbreviatedDayName((DayOfWeek)i) + ")";
InsertHash(temp, specialDayOfWeek, TokenType.DayOfWeekToken, i);
}
if (Calendar.GetType() != typeof(JapaneseCalendar))
{
// Special case for Japanese. If this is a Japanese DTFI, and the calendar is not Japanese calendar,
// we will check Japanese Era name as well when the calendar is Gregorian.
DateTimeFormatInfo jaDtfi = GetJapaneseCalendarDTFI();
for (int i = 1; i <= jaDtfi.Calendar.Eras.Length; i++)
{
InsertHash(temp, jaDtfi.GetEraName(i), TokenType.JapaneseEraToken, i);
InsertHash(temp, jaDtfi.GetAbbreviatedEraName(i), TokenType.JapaneseEraToken, i);
// m_abbrevEnglishEraNames[0] contains the name for era 1, so the token value is i+1.
InsertHash(temp, jaDtfi.AbbreviatedEnglishEraNames[i - 1], TokenType.JapaneseEraToken, i);
}
}
}
}
// TODO: This prohibits similar custom cultures, but we hard coded the name
else if (CultureName.Equals("zh-TW"))
{
DateTimeFormatInfo twDtfi = GetTaiwanCalendarDTFI();
for (int i = 1; i <= twDtfi.Calendar.Eras.Length; i++)
// TODO: This prohibits similar custom cultures, but we hard coded the name
else if (CultureName.Equals("zh-TW"))
{
if (twDtfi.GetEraName(i).Length > 0)
DateTimeFormatInfo twDtfi = GetTaiwanCalendarDTFI();
for (int i = 1; i <= twDtfi.Calendar.Eras.Length; i++)
{
InsertHash(temp, twDtfi.GetEraName(i), TokenType.TEraToken, i);
if (twDtfi.GetEraName(i).Length > 0)
{
InsertHash(temp, twDtfi.GetEraName(i), TokenType.TEraToken, i);
}
}
}
}
Expand Down

0 comments on commit bd7b3c5

Please sign in to comment.