Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swift build time optimization

Swift build time optimization

Brief sample of some code optimization for the compiler

Jose Torres Cardenas

January 12, 2018
Tweet

More Decks by Jose Torres Cardenas

Other Decks in Programming

Transcript

  1. // Build time: 5238.3ms return CGSize(width: size.width + (rightView?.bounds.width ??

    0) + (leftView?.bounds.width ?? 0) + 22, height: bounds.height) // Build time: 32.4ms var padding: CGFloat = 22 if let rightView = rightView { padding += rightView.bounds.width } if let leftView = leftView { padding += leftView.bounds.width } return CGSizeMake(size.width + padding, bounds.height)
  2. // Build time: 1250.3ms let systemOptions = [ 7, 14,

    30, -1 ] let systemNames = (0...2).map{ String(format: localizedFormat, systemOptions[$0]) } + [NSLocalizedString("everything", comment: "")] // Some code in-between labelNames = Array(systemNames[0..<count]) + [systemNames.last!] // Build time: 25.5ms let systemOptions = [ 7, 14, 30, -1 ] var systemNames = systemOptions.dropLast().map{ String(format: localizedFormat, $0) } systemNames.append(NSLocalizedString("everything", comment: "")) // Some code in-between labelNames = Array(systemNames[0..<count]) labelNames.append(systemNames.last!)
  3. // Build time: 239.0ms let labelNames = type == 0

    ? (1...5).map{type0ToString($0)} : (0...2).map{type1ToString($0)} // Build time: 16.9ms var labelNames: [String] if type == 0 { labelNames = (1...5).map{type0ToString($0)} } else { labelNames = (0...2).map{type1ToString($0)} }
  4. // Build time: 3431.7ms return CGFloat(M_PI) * (CGFloat((hour + hourDelta

    + CGFloat(minute + minuteDelta) / 60) * 5) - 15) * unit / 180 // Build time: 3.0ms return CGFloat(M_PI) * ((hour + hourDelta + (minute + minuteDelta) / 60) * 5 - 15) * unit / 180
  5. // Build time: 1433.7ms let expansion = a - b

    - c + round(d * 0.66) + e // Build time: 34.7ms let expansion = a - b - c + d * 0.66 + e
  6. // 1905.5ms private(set) lazy var chartViewColors: [UIColor] = [ self.chartColor,

    UIColor(red: 86/255, green: 84/255, blue: 124/255, alpha: 1), UIColor(red: 80/255, green: 88/255, blue: 92/255, alpha: 1), UIColor(red: 126/255, green: 191/255, blue: 189/255, alpha: 1), UIColor(red: 161/255, green: 77/255, blue: 63/255, alpha: 1), UIColor(red: 235/255, green: 185/255, blue: 120/255, alpha: 1), UIColor(red: 100/255, green: 126/255, blue: 159/255, alpha: 1), UIColor(red: 160/255, green: 209/255, blue: 109/255, alpha: 1), self.backgroundGradientView.upperColor ]
  7. // 2565.3ms private let createChartViewColors = { () -> [UIColor]

    in let colors = [ UIColor(red: 86/255, green: 84/255, blue: 124/255, alpha: 1), UIColor(red: 80/255, green: 88/255, blue: 92/255, alpha: 1), UIColor(red: 126/255, green: 191/255, blue: 189/255, alpha: 1), UIColor(red: 161/255, green: 77/255, blue: 63/255, alpha: 1), UIColor(red: 235/255, green: 185/255, blue: 120/255, alpha: 1), UIColor(red: 100/255, green: 126/255, blue: 159/255, alpha: 1), UIColor(red: 160/255, green: 209/255, blue: 109/255, alpha: 1), ] return colors }
  8. // Cumulative build time: 56.3ms private(set) lazy var chartViewColors: [UIColor]

    = self.createChartViewColors() // Build time: 6.2ms private func createChartViewColors() -> [UIColor] { return [ chartColor, UIColor(red: 86/255, green: 84/255, blue: 124/25 UIColor(red: 80/255, green: 88/255, blue: 92/255 UIColor(red: 126/255, green: 191/255, blue: 189/ UIColor(red: 161/255, green: 77/255, blue: 63/25 UIColor(red: 235/255, green: 185/255, blue: 120/ UIColor(red: 100/255, green: 126/255, blue: 159/ UIColor(red: 160/255, green: 209/255, blue: 109/ backgroundGradientView.upperColor ] }