From 05a6420ee35773e3a877e4003179c97696f54425 Mon Sep 17 00:00:00 2001 From: Frank Tang Date: Mon, 6 Jan 2025 16:02:00 -0800 Subject: [PATCH] ICU-22991 Reduce Calendar object size Gregorian monthOfYear, dayOfMonth only need one byte dayOfYear only need two bytes to store. --- icu4c/source/i18n/calendar.cpp | 13 ++++++++++--- icu4c/source/i18n/unicode/calendar.h | 14 +++++++------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/icu4c/source/i18n/calendar.cpp b/icu4c/source/i18n/calendar.cpp index ddff6255cd85..0dd0755589d8 100644 --- a/icu4c/source/i18n/calendar.cpp +++ b/icu4c/source/i18n/calendar.cpp @@ -1544,9 +1544,16 @@ void Calendar::computeGregorianFields(int32_t julianDay, UErrorCode& ec) { ec = U_ILLEGAL_ARGUMENT_ERROR; return; } - Grego::dayToFields(julianDay, fGregorianYear, fGregorianMonth, - fGregorianDayOfMonth, gregorianDayOfWeekUnused, - fGregorianDayOfYear, ec); + int32_t gregorianMonth, gregorianDayOfMonth, gregorianDayOfYear; + Grego::dayToFields(julianDay, fGregorianYear, gregorianMonth, + gregorianDayOfMonth, gregorianDayOfWeekUnused, + gregorianDayOfYear, ec); + U_ASSERT(gregorianMonth <= 12); + U_ASSERT(gregorianDayOfMonth <= 31); + U_ASSERT(gregorianDayOfYear <= 366); + fGregorianMonth = gregorianMonth; + fGregorianDayOfMonth = gregorianDayOfMonth; + fGregorianDayOfYear = gregorianDayOfYear; } /** diff --git a/icu4c/source/i18n/unicode/calendar.h b/icu4c/source/i18n/unicode/calendar.h index 0c36185a85c3..bed5bfeef075 100644 --- a/icu4c/source/i18n/unicode/calendar.h +++ b/icu4c/source/i18n/unicode/calendar.h @@ -2256,21 +2256,21 @@ class U_I18N_API Calendar : public UObject { * returned by getGregorianMonth(). * @see #computeGregorianFields */ - int32_t fGregorianMonth; + int8_t fGregorianMonth; /** - * The Gregorian day of the year, as computed by - * computeGregorianFields() and returned by getGregorianDayOfYear(). + * The Gregorian day of the month, as computed by + * computeGregorianFields() and returned by getGregorianDayOfMonth(). * @see #computeGregorianFields */ - int32_t fGregorianDayOfYear; + int8_t fGregorianDayOfMonth; /** - * The Gregorian day of the month, as computed by - * computeGregorianFields() and returned by getGregorianDayOfMonth(). + * The Gregorian day of the year, as computed by + * computeGregorianFields() and returned by getGregorianDayOfYear(). * @see #computeGregorianFields */ - int32_t fGregorianDayOfMonth; + int16_t fGregorianDayOfYear; /* calculations */