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

Swift Performance

9bf923e39671cde83584e3e926296c13?s=47 Kishikawa Katsumi
February 20, 2016
1.3k

Swift Performance

Swift Optimization Tips

9bf923e39671cde83584e3e926296c13?s=128

Kishikawa Katsumi

February 20, 2016
Tweet

Transcript

  1. Swift Performance

  2. https://github.com/kishikawakatsumi/ kishikawa katsumi http://kishikawakatsumi.hatenablog.com/ Realm

  3. ࠷దԽͷલʹ ʮૣ͗͢Δ࠷దԽ͸ॾѱͷࠜݯͰ͋Δʯ

  4. ࠷దԽͷલʹ ˖ 鎘庠ׅ׷ ˖ չ〳铣䚍պהךزٖ٦سؔؿ ˖ اؿزؐؑ،ךˑٕ٦ٕ˒

  5. ܭଌ͢Δ

  6. ܭଌ͢Δ func measure(call: () -> Void) { let startTime =

    CACurrentMediaTime() call() let endTime = CACurrentMediaTime() print("Time - \(endTime - startTime)") }
  7. ܭଌ͢Δ func measure(call: () -> Void) { let startTime =

    CACurrentMediaTime() call() let endTime = CACurrentMediaTime() print("Time - \(endTime - startTime)") } measure { // Heavy task }
  8. ίʔυͷੑೳΛ্͛Δʹ͸ ˖ &YQMJDJU  ״׶⸬桦ך葺ְ،ٕ؞ٔؤيח㢌ִ׷  ٍؗحءُ׾㼪Ⰵׅ׷  ؝٦سך㢌刿չ㣐պ 

    〳铣䚍הךزٖ٦سؔؿ ˖ *NQMJDJU  鎉铂ך剑黝⻉׾ⵃ欽ׅ׷  ؝٦سך㢌刿չ㼭պ  鎉铂暴剣
  9. ࠷దԽΛ༗ޮʹ͢Δ ˖ 3FMFBTF圓䧭דؽٕسׅ׷  ؝ٝػ؎ٓח״׷剑黝⻉׾剣⸬חׅ׷

  10. ࠷దԽΛ༗ޮʹ͢Δ ˖ 3FMFBTF圓䧭דؽٕسׅ׷  ؝ٝػ؎ٓח״׷剑黝⻉׾剣⸬חׅ׷ IUUQTHJUIVCDPNBQQMFTXJGUCMPCNBTUFSEPDT0QUJNJ[BUJPO5JQTSTU

  11. ࠷దԽΛ֬ೝ͢Δʹ͸ $ xcrun swiftc --help OVERVIEW: Swift compiler USAGE: swiftc

    [options] <inputs> MODES: -dump-ast Parse and type-check input file(s) and dump AST(s) -dump-parse Parse input file(s) and dump AST(s) -dump-type-refinement-contexts Type-check input file(s) and dump type refinement contexts(s) -emit-assembly Emit assembly file(s) (-S) -emit-bc Emit LLVM BC file(s) -emit-executable Emit a linked executable -emit-ir Emit LLVM IR file(s) ...
  12. ࠷దԽΛ֬ೝ͢Δʹ͸ ˖ FNJUBTTFNCMZ&NJUBTTFNCMZMF T  4  ˖ FNJUJS&NJU--7.*3MF T

     ˖ FNJUTJCHFO&NJUTFSJBMJ[FE"45 SBX4*-MF T  ˖ FNJUTJM&NJUDBOPOJDBM4*-MF T
  13. ࠷దԽΛ֬ೝ͢Δʹ͸ $ xcrun swiftc -emit-sil sourceFile.swift -o outputFile

  14. finalΛ࢖͏ final class ... final var ... final func ...

  15. privateΛ࢖͏ private class ... private var ... private func ...

  16. finalΛ࢖͏ ˖ ⹛涸ر؍أػحث׾鼘ֽ׷ ˖ QSJWBUFכ荈⹛涸חOBM䪔ְחז׷ IUUQTHJUIVCDPNBQQMFTXJGUCMPCNBTUFSEPDT0QUJNJ[BUJPO5JQTSTU

  17. Reference TypeΑΓValueTypeΛ࢖͏ ˖ *NNVUBCMF ˖ أٖحسإ٦ؿ ˖ ًٌٔ،ٗ؛٦ءّٝ

  18. Reference TypeΑΓValueTypeΛ࢖͏ struct NumberValue { let x: Int } class

    NumberReference { let x: Int init(x: Int) { self.x = x } }
  19. Reference TypeΑΓValueTypeΛ࢖͏ ... callq __TFV3out11NumberValueCfMS0_FT1xSi_S0_ movq %rax, -16(%rbp) movq -48(%rbp),

    %rdi addq %rax, %rdi ...
  20. Reference TypeΑΓValueTypeΛ࢖͏ ... callq __TFC3out15NumberReferenceCfMS0_FT1xSi_S0_ movq %rax, -32(%rbp) movq -144(%rbp),

    %rdi movq %rax, -160(%rbp) callq _swift_release ...
  21. Reference TypeΑΓValueTypeΛ࢖͏ ... callq __TFC3out15NumberReferenceCfMS0_FT1xSi_S0_ movq %rax, -32(%rbp) movq -144(%rbp),

    %rdi movq %rax, -160(%rbp) callq _swift_release ...
  22. ReferenceTypeΛ഑ྻʹೖΕΔ৔߹͸ContiguousArrayΛ࢖͏͜ͱΛݕ౼͢Δ ˖ 3FGFSFODF5ZQF׾ꂁ⴨חⰅ׸׷㜥さכ$POUJHVPVT"SSBZ׾⢪ֲֿה׾嗚 鎢ׅ׷ ഑ྻʹReferece TypeΛ֨ೲ͢ΔͷΛආ͚Δ IUUQTHJUIVCDPNBQQMFTXJGUCMPCNBTUFSEPDT0QUJNJ[BUJPO5JQTSTU // Don't use

    a class here. struct PhonebookEntry { var name : String var number : [Int] } var a : [PhonebookEntry]
  23. ഑ྻʹReferece TypeΛ֨ೲ͢ΔͷΛආ͚Δ IUUQTHJUIVCDPNBQQMFTXJGUCMPCNBTUFSEPDT0QUJNJ[BUJPO5JQTSTU ReferenceTypeΛ഑ྻʹೖΕΔ৔߹͸ContiguousArrayΛ࢖͏͜ͱΛݕ౼͢Δ ˖ 3FGFSFODF5ZQF׾ꂁ⴨חⰅ׸׷㜥さכ$POUJHVPVT"SSBZ׾⢪ֲֿה׾嗚 鎢ׅ׷ class C {

    ... } var a: ContiguousArray<C> = [C(...), C(...), ..., C(...)]
  24. Objective-CΛආ͚Δ ˖ ⹛涸ر؍أػحثָ涪欰ׅ׷  ؝ٝػ؎ָٓ剑黝⻉דֹזְ

  25. SwiftͷίʔυΛObjective-Cʹެ։͠ͳ͍ ˖ ⹛涸ر؍أػحثָ涪欰ׅ׷  ؝ٝػ؎ָٓ剑黝⻉דֹזְ

  26. ஗ԆʢLazyʣͤ͞Δ

  27. ඞཁʹԠͯ͡Lazyͳม਺Λ࢖͏ ˖ (MPCBM7BSJBCMFT ˖ 5ZQF7BSJBCMFT ˖ -B[Z1SPQFSUJFT ˖ $PNQVUFE1SPQFSUJFT

  28. LazyCollection let numbers = Array(1...1000) let doubledNumbers = numbers.map {

    $0 * 2 } doubledNumbers.last
  29. LazyCollection

  30. LazyCollection let numbers = Array(1...1000) let lazyNumbers = numbers.lazy let

    doubledNumbers = lazyNumbers.map { $0 * 2 } doubledNumbers.last let lazyNumbers2 = LazyCollection(numbers) let doubledNumbers2 = lazyNumbers.map { $0 * 2 } doubledNumbers2.last
  31. LazyCollection

  32. ࢀߟࢿྉ  HJUIVCDPNBQQMFTXJGUCMPCNBTUFSEPDT0QUJNJ[BUJPO5JQTSTU  HJUIVCDPNBQQMFTXJGUCMPCNBTUFSEPDT"3$0QUJNJ[BUJPOSTU  88%$4FTTJPO0QUJNJ[JOH4XJGU1FSGPSNBODF  *ODSFBTJOH1FSGPSNBODFCZ3FEVDJOH%ZOBNJD%JTQBUDI 

    IUUQTEFWFMPQFSBQQMFDPNTXJGUCMPHBSUJDMF