Skip to content

rdar://152532669 (Work around inefficient compiler-synthesized Codable conformance of enums) #1433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,11 @@ extension AttributeScopes.FoundationAttributes {
case spelledOutValue
case unit(Unit)
case actualByteCount

private typealias ValueCodingKeys = EmptyCodingKeys
private typealias SpelledOutValueCodingKeys = EmptyCodingKeys
private typealias UnitCodingKeys = DefaultAssociatedValueCodingKeys1
private typealias ActualByteCountCodingKeys = EmptyCodingKeys
}

public enum Unit: Codable, Sendable {
Expand All @@ -532,6 +537,16 @@ extension AttributeScopes.FoundationAttributes {
case eb
case zb
case yb

private typealias ByteCodingKeys = EmptyCodingKeys
private typealias KbCodingKeys = EmptyCodingKeys
private typealias MbCodingKeys = EmptyCodingKeys
private typealias GbCodingKeys = EmptyCodingKeys
private typealias TbCodingKeys = EmptyCodingKeys
private typealias PbCodingKeys = EmptyCodingKeys
private typealias EbCodingKeys = EmptyCodingKeys
private typealias ZbCodingKeys = EmptyCodingKeys
private typealias YbCodingKeys = EmptyCodingKeys
}
}

Expand Down Expand Up @@ -583,6 +598,11 @@ extension AttributeScopes.FoundationAttributes {
case int(Int64)
case double(Double)
case decimal(Decimal)

private typealias UintCodingKeys = DefaultAssociatedValueCodingKeys1
private typealias IntCodingKeys = DefaultAssociatedValueCodingKeys1
private typealias DoubleCodingKeys = DefaultAssociatedValueCodingKeys1
private typealias DecimalCodingKeys = DefaultAssociatedValueCodingKeys1
}
}

Expand Down Expand Up @@ -941,5 +961,8 @@ extension AttributedString {
/// - Note: In vertical scripts, this equivalent to a bottom-to-top
/// writing direction.
case rightToLeft

private typealias LeftToRightCodingKeys = EmptyCodingKeys
private typealias RightToLeftCodingKeys = EmptyCodingKeys
}
}
28 changes: 28 additions & 0 deletions Sources/FoundationEssentials/Calendar/Calendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,34 @@ public struct Calendar : Hashable, Equatable, Sendable {

@available(FoundationPreview 6.2, *)
case vietnamese

private typealias GregorianCodingKeys = EmptyCodingKeys
private typealias ChineseCodingKeys = EmptyCodingKeys
private typealias BuddhistCodingKeys = EmptyCodingKeys
private typealias CopticCodingKeys = EmptyCodingKeys
private typealias EthiopicAmeteMihretCodingKeys = EmptyCodingKeys
private typealias EthiopicAmeteAlemCodingKeys = EmptyCodingKeys
private typealias HebrewCodingKeys = EmptyCodingKeys
private typealias Iso8601CodingKeys = EmptyCodingKeys
private typealias IndianCodingKeys = EmptyCodingKeys
private typealias IslamicCodingKeys = EmptyCodingKeys
private typealias IslamicCivilCodingKeys = EmptyCodingKeys
private typealias JapaneseCodingKeys = EmptyCodingKeys
private typealias PersianCodingKeys = EmptyCodingKeys
private typealias RepublicOfChinaCodingKeys = EmptyCodingKeys
private typealias IslamicTabularCodingKeys = EmptyCodingKeys
private typealias IslamicUmmAlQuraCodingKeys = EmptyCodingKeys
private typealias BanglaCodingKeys = EmptyCodingKeys
private typealias GujaratiCodingKeys = EmptyCodingKeys
private typealias KannadaCodingKeys = EmptyCodingKeys
private typealias MalayalamCodingKeys = EmptyCodingKeys
private typealias MarathiCodingKeys = EmptyCodingKeys
private typealias OdiaCodingKeys = EmptyCodingKeys
private typealias TamilCodingKeys = EmptyCodingKeys
private typealias TeluguCodingKeys = EmptyCodingKeys
private typealias VikramCodingKeys = EmptyCodingKeys
private typealias DangiCodingKeys = EmptyCodingKeys
private typealias VietnameseCodingKeys = EmptyCodingKeys

package static let cldrKeywordKey = "ca"
package static let legacyKeywordKey = ICULegacyKey("calendar")
Expand Down
11 changes: 11 additions & 0 deletions Sources/FoundationEssentials/CodableUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -687,3 +687,14 @@ extension BufferView where Element == UInt8 {
}
}

// Non-RawRepresentable Codable enum cases with the same number and labels of associated values all share equivalent CodingKeys, but the compiler-synthesized implementation generates a new type for each case.
// Each of these types has their own set of `metadata instantiation cache for protocol conformance descriptor` symbols for each of 5 protocol conformances which consumes DATA space.
// Instead of using the synthesized CodingKey types, you can make a `private typealias <CaseName>CodingKeys = <one of these types>` inside the Codable enum to reduce the amount of redundant DATA.
package enum EmptyCodingKeys: CodingKey { }
package enum DefaultAssociatedValueCodingKeys1: String, CodingKey {
case _0
}
package enum DefaultAssociatedValueCodingKeys2: String, CodingKey {
case _0
case _1
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
extension PredicateExpressions {
public enum ArithmeticOperator: Codable, Sendable {
case add, subtract, multiply

private typealias AddCodingKeys = EmptyCodingKeys
private typealias SubtractCodingKeys = EmptyCodingKeys
private typealias MultiplyCodingKeys = EmptyCodingKeys
}

public struct Arithmetic<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
extension PredicateExpressions {
public enum ComparisonOperator: Codable, Sendable {
case lessThan, lessThanOrEqual, greaterThan, greaterThanOrEqual

private typealias LessThanCodingKeys = EmptyCodingKeys
private typealias LessThanOrEqualCodingKeys = EmptyCodingKeys
private typealias GreaterThanCodingKeys = EmptyCodingKeys
private typealias GreaterThanOrEqualCodingKeys = EmptyCodingKeys
}

public struct Comparison<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ extension Date {
enum ComponentAdjustmentStrategy : Codable, Hashable {
case alignedWithComponentBoundary
case rounded

private typealias AlignedWithComponentBoundaryCodingKeys = EmptyCodingKeys
private typealias RoundedCodingKeys = EmptyCodingKeys
}

var componentAdjustmentStrategy: ComponentAdjustmentStrategy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,11 @@ extension Date {
enum InnerStyle: Codable, Hashable {
case formatStyle(Date.FormatStyle)
case verbatimFormatStyle(VerbatimFormatStyle)

private typealias FormatStyleCodingKeys = DefaultAssociatedValueCodingKeys1
private typealias VerbatimFormatStyleCodingKeys = DefaultAssociatedValueCodingKeys1
}

var innerStyle: InnerStyle

init(style: InnerStyle) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ extension Decimal.FormatStyle {
case decimal(Decimal.FormatStyle)
case currency(Decimal.FormatStyle.Currency)
case percent(Decimal.FormatStyle.Percent)

private typealias DecimalCodingKeys = DefaultAssociatedValueCodingKeys1
private typealias CurrencyCodingKeys = DefaultAssociatedValueCodingKeys1
private typealias PercentCodingKeys = DefaultAssociatedValueCodingKeys1
}

var style: Style
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,10 @@ extension FloatingPointFormatStyle {
case floatingPoint(FloatingPointFormatStyle)
case currency(FloatingPointFormatStyle.Currency)
case percent(FloatingPointFormatStyle.Percent)

private typealias FloatingPointCodingKeys = DefaultAssociatedValueCodingKeys1
private typealias CurrencyCodingKeys = DefaultAssociatedValueCodingKeys1
private typealias PercentCodingKeys = DefaultAssociatedValueCodingKeys1

var formatter: ICUNumberFormatterBase? {
switch self {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ internal final class ICULegacyNumberFormatter : @unchecked Sendable {
case percent(NumberFormatStyleConfiguration.Collection)
case currency(CurrencyFormatStyleConfiguration.Collection, currencyCode: String)
case descriptive(DescriptiveNumberFormatConfiguration.Collection)

private typealias NumberCodingKeys = DefaultAssociatedValueCodingKeys1
private typealias PercentCodingKeys = DefaultAssociatedValueCodingKeys1
private typealias DescriptiveCodingKeys = DefaultAssociatedValueCodingKeys1
}

private struct Signature : Hashable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,10 @@ extension IntegerFormatStyle {
case integer(IntegerFormatStyle)
case percent(IntegerFormatStyle.Percent)
case currency(IntegerFormatStyle.Currency)

private typealias IntegerCodingKeys = DefaultAssociatedValueCodingKeys1
private typealias PercentCodingKeys = DefaultAssociatedValueCodingKeys1
private typealias CurrencyCodingKeys = DefaultAssociatedValueCodingKeys1
}

var style: Style
Expand Down