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

Tomasz Gebarowski & Wojciech Łukaszuk: SWIFT AT SCALE

1fa9cb8c7997c8c4d3d251fb5e41f749?s=47 Realm
October 22, 2016

Tomasz Gebarowski & Wojciech Łukaszuk: SWIFT AT SCALE

Is Swift ready for large scale business apps? We would like to share our experience on writing one of the most popular mobile banking apps in Poland. The mBank app, that is entirely written in Swift. The problems we faced, lessons learnt, pitfalls and benefits

1fa9cb8c7997c8c4d3d251fb5e41f749?s=128

Realm

October 22, 2016
Tweet

Transcript

  1. Swift at scale @tgebarowski @wojteklu

  2. one of the biggest mobile banking app >3 years

  3. Rewrite legacy code new API we are smarter now

  4. June 2, 2014

  5. Why Swift?

  6. 1984

  7. None
  8. None
  9. 1. Wprowadzenie - Info o autorach - Kilka słów o

    aplikacji: - ilość linii kodu/klas/protokołów - historia przepisywania na Swift (kiedy się zaczęła, ile trwała, ile osób nad tym pracowało) 3. Co nam dał Swift? - Swift jest bezpieczniejszy (dzięki optionals, czyli weliminowalismy NPE) - Bardziej kompaktowa składnia - Krótsza ścieżka wdrożenia dla nowych devów (zwłaszcza na codzień piszacych w C#) - Genericsy, Enumy, Protocols - Swift jest szybszy (porównanie z ObjC, dynamiczne biblioteki) - Swift Playgrounds (szybkie prototypy) C) Analiza kompilacji poszczegolnych plikow (który kod powoduje dłuzsza kompilację)
  10. [Objective-C]

  11. not the only reason…

  12. timing was right ⌛

  13. interoperability with Objective-C

  14. clear syntax

  15. // Objective-C if(delegate != nil) { if([delegate respondsToSelector:@selector(didMove:)]) { [delegate

    didMove:player]; } } // Swift delegate?.didMove?(player: player)
  16. boilerplate

  17. // Objective-C @interface Foo : NSObject @property (readonly) int bar;

    - (instancetype)initWithBar:(int)bar; @end @implementation Foo - (instancetype)initWithBar:(int)bar { self = [super init]; if (self) { _bar = bar; } return self; } @end // Swift struct Foo { let bar: Int }
  18. protocols everywhere

  19. Behaviour by protocol conformance class ViewController: UIViewController, MBDisableSwipeBackType, MBHideNavigationBarType {

  20. functional magic

  21. ~1 year of development ~140k lines of code ~2k files

  22. Not everything went smoothly Compilation Times Migrations Xcode

  23. compilation times https://xkcd.com/303/

  24. 1 minute?

  25. 5 minutes?

  26. up to 40 minutes…

  27. Compilation times -Onone -Owholemodule Core i5 ~40 min ~11 min

    Core i7 ~30 min ~8 min
  28. Faster build times No debug symbols Stepping behaves oddly Much

    slower build times No incremental builds * Incremental builds * supported since Swift 3 and Xcode 8 vs -Owholemodule -Onone
  29. Problem with Whole Module Optimisation getconf ARG_MAX 262144 uses absolute

    path of every Swift file in project +1500 Swift files Assume long project dir path MacOS command line
 max length
  30. Problem with -Onone Assuming we have 1500+ Swift files in

    project Single Swift file compilation takes about 1-2 seconds
  31. How did we approach the situation? Sources: http://irace.me/swift-profiling Profiling Swift

    compilation times xcodebuild -workspace App.xcworkspace -scheme App clean build OTHER_SWIFT_FLAGS="-Xfrontend -debug-time-function-bodies" | grep .[0-9]ms | grep -v ^0.[0-9]ms | sort -nr > culprits.txt 1 2 Inspecting culprits 58.2ms /Users/tomasz/workspace/iOS/Sources/FileA.swift:23:14 @objc get {} 59.8ms /Users/tomasz/workspace/iOS/Sources/FileA.swift:23:52 (closure)
  32. Frameworks! One Monolithic Mobile App MBFoundation MBServiceKit MBUtilityKit MBCoreKit MBGlueKit

    MobileApp dropping iOS 7 support
  33. Compilation times -Onone -Owholemodule Core i5 ~18 min
 ~40 min

    ~10 min
 ~11 min Core i7 13 min
 ~30 min 8 min
 ~8 min + no need to recompile whole project when -Owholemodule used
  34. Frameworks pitfalls Done post-factum Code had to be decoupled Class

    scope properly annotated Missing imports added Problems with Xcode
  35. Frameworks benefits Much faster compilation times Less coupled code Frameworks

    could be separately tested No need to recompile frameworks each time
  36. Migrations Swift 1.2 Swift 2.1 Swift 2.2 2 days 2

    attempts (3 weeks totally) author: Christophe Boisson
  37. The migration process Auto migration Manual fixes Code Review ~9

    hrs Unplanned tasks compiler bug workarounds
  38. Migrations pitfalls Migration tool is solving only a % of

    your problems swiftc in Xcode 7.2 was crashing when
 compiling our project Lots of warnings generated (2.1 -> 2.2) Need for manual review of auto migrated code
 (i.e. force unwrap)
  39. Mixes of old and new Swift patterns Fix all warnings

    ASAP otherwise they
 will be out of your control All developers has to migrate at once Hard to back port code Migrations pitfalls (II)
  40. The +1500 Swift files problem (Xcode 7.2) +1500 Swift files?

    what?
  41. Why so many files? Auto generated Service Layer Single responsibility

    principle Protocols in independent files Extensions
  42. Auto generated Service Layer Single responsibility principle Protocols in independent

    files Extensions +1500 files Why so many files?
  43. The +1500 Swift files problem (Xcode 7.2) Whole Module Optimisation

    swiftc crash
  44. swiftc frontend swiftc swiftc frontend posix_spawn swiftc frontend … crash

    The +1500 Swift files problem (Xcode 7.2)
  45. Reported problem to Apple (SR-280) Fixed in Xcode 7.3 Provided

    a workaround for 7.2 The +1500 Swift files problem (Xcode 7.2) swiftc wrapper swiftc -num-threads 0 with fork instead of posix_spawn Xcode More info: https://github.com/tgebarowski/swiftc-wrapper
  46. Xcode - we have a problem! Refactoring not supported lldb

    not working in - Owholemodule Symbols not indexed properly when inside frameworks 1 2 3 4
  47. What’s next?

  48. Swift 3 migration

  49. go / no go?

  50. GO Successfully deployed rewritten app Developers are more enthusiastic about

    Swift We observed less crashes after deploying Swift version of app Less runtime errors 23% less lines of code
  51. Thank You!

  52. We are hiring!

  53. @tgebarowski @wojteklu Swift at scale