Hey Folks,
we experienced a massive slowdown in archiving our app with a growing struct with a lot of properties in it.
We use a json
to codable struct
construct to handle our translations/localization in our app.
With a growing amount of properties in this struct, the archiving time grows up to 120-150 min.
The debug compile times are always pretty decent and on a low level at 4-6 min.
Here is how the struct looks like:
struct Translation: Codable {
var aboutAdvantages = ""
var aboutEnvironmentText = ""
// Another 800+ properties
var youHaveOneOpenOrder = ""
var youHaveOpenOrdersHeadline = ""
}
This translation struct is used in pretty much in every view in the application and some services and converter/mapper/view models.
import SwiftUI
extension View {
var translation: Translation {
// logic to get the translation from the dependency injection contianer
}
}
struct SampleView: View {
var body: some View {
Text(translation.aboutAdvantages)
}
}
Now we figured out that changing this to an enum based system like the code below is improving the archiving speed very drastically down to around 5-6 min.
struct Translation {
private var translations: [String: Any]
init(translations: [String: Any]) {
self.translations = translations
}
enum Keys: String {
case aboutAdvantages
case aboutEnvironmentText
// Another 800+ fields
case youHaveOneOpenOrder
case youHaveOpenOrdersHeadline
}
func t(_ key: Keys) -> String {
if translations[key.rawValue] == nil {
return "Key is missing \(key.rawValue)"
}
return translations[key.rawValue] as! String
}
}
struct SampleView: View {
var body: some View {
Text(translation.t(.aboutAdvantages))
}
}
I assume that optimization on the archive build got crazy at some point and need to do a huge amount of extra rounds because of this big fat beefy struct property class.
Is there an explanation for this that a normal non-compiler programmer can understand these circumstances?
Here are some data:
swift-driver version: 1.45.2 Apple Swift version 5.6.1 (swiftlang-5.6.0.323.66 clang-1316.0.20.12)
Target: arm64-apple-macosx12.0
Xcode Version 13.4.1 (13F100)
Let me know if someone needs more information regarding this topic.
Thanks a lot for your time :)