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

為什麼 App 卡卡的

Johnlin
November 08, 2020

為什麼 App 卡卡的

iPlayground 2020 Keynote

Johnlin

November 08, 2020
Tweet

More Decks by Johnlin

Other Decks in Programming

Transcript

  1. Safe Area
    @johnlinvc
    ҝॄኄ App 㠡㠡తʁ
    John Lin ( @johnlinvc )

    View full-size slide

  2. Safe Area
    @johnlinvc
    John Lin (@johnlinvc)
    • ኺ iPhone OS 2 ։࢝ሜ Obj-Cɻ

    • ໨લࡏ West Pharmaceutical ᙛ DevOpsɻ

    ሢ໳ሜڅ։ᚙऀʢแؚࣗݾʣ༻త޻۩ɻ

    ؐ༗मผਓత Bugɻ

    View full-size slide

  3. Safe Area
    @johnlinvc
    تᓣჩՃࣾ܈׆ಈ
    Functional Tuesday
    Ruby Taiwan
    Elixier Taiwan
    Taipei.py
    Clojure Taiwan

    View full-size slide

  4. Safe Area
    @johnlinvc
    Swift Taipei
    • ኺ 2016 ೥ 1/14։࢝ɼࡏ㑌ݸ݄తୈҰݸᜌ፨ೋᎯ㭎ɻ౸໨લቮៃ㭎ྃ
    ઀ۙ 60 ࣍ᡉ။ɻ

    • ఏڙେՈҰݸೳⴺ์ᱷ౼࿦ Swift త஍ํɻ

    • ڧྗ㐸ٻߨऀதɻ୞ཁ࿨ Swift ᜗ඍ༗૬᮫త౎ඇৗᓣܴɻ

    View full-size slide

  5. Safe Area
    @johnlinvc
    ৗৗࡏݚ౼။্෼ڗ
    RubyConf CN

    View full-size slide

  6. Safe Area
    @johnlinvc
    iPlayground
    • ڈྃ iOSDC Japan ޙ᧷ಘ୆ᖯ໵ጯ֘༗

    • ᙛ৔ങྃ iosdc.tw ໢Ҭɻ

    • ޙိܾఆཁڣ iPlaygroundɻ

    iosdc.tw ။ಋ౸ iplayground.io

    View full-size slide

  7. Safe Area
    @johnlinvc
    ୈҰ࣍ߨ Keynote
    • ᅳ㚎֎ߨྃेز࣍ɼײँओ㭎ํڅզṜݸػ။ိᙛ Keynote ߨऀɻ

    • ऴԙՄҎ༻ Keynote ိ၏ Keynote ྃɻ

    View full-size slide

  8. Safe Area
    @johnlinvc

    View full-size slide

  9. Safe Area
    @johnlinvc
    iPlayground తओ୊
    • ᙛવؐੋᅴ៴ࡏ App (ෆੋ˖ʽҰʽҰ) ্,ɻ

    • ෼ڗ૬᮫తመ຿ʢҏཕʣៃᱛɻ

    • ୳ࡧߋଟ։ᚙ্తՄೳੑɻ

    View full-size slide

  10. Safe Area
    @johnlinvc
    Ұݸ޷ App తᑍ݅
    1. ූ߹঎ۀधٻɻ

    2. ༻ىိॱखɻ

    3. Optional<ඬ྄తఔࣜᛰ>ɻ

    View full-size slide

  11. Safe Area
    @johnlinvc
    Ұݸ޷ App తᑍ݅
    1. ූ߹঎ۀधٻɻ

    2. ༻ىိॱखɻ

    3. Optional<ඬ྄తఔࣜᛰ>ɻ

    View full-size slide

  12. Safe Area
    @johnlinvc
    ၏աҰਞࢠతਓػհ໘ݚڀ

    View full-size slide

  13. Safe Area
    @johnlinvc
    ॄኄੋॱख
    • ֎᧺ඬ྄

    • ᔒ༗൓ਓྨతૢ࡞ྲྀఔ

    • ߦҝූ߹࢖༻ऀ༬ظ

    View full-size slide

  14. Safe Area
    @johnlinvc
    ॄኄੋॱख
    • ֎᧺ඬ྄

    • ᔒ༗൓ਓྨతૢ࡞ྲྀఔ

    • ߦҝූ߹࢖༻ऀ༬ظ

    View full-size slide

  15. Safe Area
    @johnlinvc
    ࢖༻ऀ༬ظతߦҝ
    • ࢖༻ऀత༌ೖཁ༗దᙛతճᰶɻ

    • ྗճᰶɻ

    • ࢹ᧷ճᰶɻ

    View full-size slide

  16. Safe Area
    @johnlinvc
    ྗճᰶ
    • ଧ伴൫త࣌ީ௨ৗෆ။؃伴൫ɻ

    ػց伴൫తྗճᰶᩋզ၇҈ىိൺֱਅመɻ

    • iPhone 7/8 ্తHome伴ɻ

    ଖመੋݻఆతɼᯪ਒ಈᩋզ၇ײ᧷༗҈Լڈతײ᧷ɻ

    View full-size slide

  17. Safe Area
    @johnlinvc
    ࢹ᧷ճᰶ
    • ᦊນ্త࢖༻ऀհ໘౎ੋࢹ᧷ճᰶతҰ෦෼ɻ

    • ᴍ㐝ޙతճᰶɼ૾ੋબᎩޙతᏓ৭ɼ

    ҃ੋҠಈ౸ԼҰݸᙘ໘ɻ

    • 䇪ಈޙతճᰶɼ૾ੋᰖֹࣔ֎త㚎༰ɼ

    ҃ੋҠಈᙘ໘্త෺ᱪɻ

    View full-size slide

  18. Safe Area
    @johnlinvc
    ॄኄੋ㠡㠡తʁ
    • ࿝䷩㘸㠡㠡తɻ

    • ᙘ໘ෆ࿈᠃ɻ

    • ༌ೖ౸ࢹ᧷ճᰶత࣌ؒࠩଠ௕ɻ

    View full-size slide

  19. Safe Area
    @johnlinvc
    ᙘ໘త࿈᠃ੑ
    • ਅመੈքత෺ᱪతҠಈੋ࿈᠃తɻ

    • ࡏᰖࣔ෺ᱪҠಈ࣌ɼ೗Ռਓ؟ೳⴺᚙݱ

    தؒ༗ඃ௓աతᙘ໘ɻब။᧷ಘ㠡㠡తɻ

    View full-size slide

  20. Safe Area
    @johnlinvc
    ਓ؟తහ㕔౓
    • 1991 ೥తݚڀɻࢹਆៃࡉ๔ሣᦊນરᗜత൓ጯɻ

    • ༻ిۃఁଌਓᱪࢹਆៃሣ CRT ᦊນરᗜత൓ጯɻ

    • Human electroretinogram responses to video displays,

    fluorescent lighting, and other high frequency sources

    View full-size slide

  21. Safe Area
    @johnlinvc
    ਓ؟ՄҎ؃౸ 76 Hz తરᗜ

    View full-size slide

  22. Safe Area
    @johnlinvc
    ༌ೖ౸ࢹ᧷ճᰶత࣌ؒࠩ
    • ਅመੈքత෺ᱪडྗޙ။ཱࠁҠಈɼ

    ᔒ༗೚Կ࣌ؒࠩɻ

    • ࢖༻ऀૢ࡞ޙաྃଠٱ࠽㗞ੜࢹ᧷ճᰶత࿩ɼ

    ब။᧷ಘ㠡㠡తɻ

    View full-size slide

  23. Safe Area
    @johnlinvc
    ճᰶཁଟշ࠽ⴺշ
    • 2015 ೥తݚڀɼ

    ଌࢼ࢖༻ऀሣᨀ߇༌ೖతࢹ᧷ճᰶԆᬌ

    త༰೜౓ɻ

    • How Much Faster is Fast Enough? 

    User Perception of Latency &

    Latency Improvements in

    Direct and Indirect Touch

    View full-size slide

  24. Safe Area
    @johnlinvc
    ճᰶཁଟշ࠽ⴺշ
    • ሣԙᴍ㐝࿨䇪፮༗ෆಉత༰೜౓ɻ

    • ᴍ㐝 69 ms

    • 䇪፮ 11 ms

    View full-size slide

  25. Safe Area
    @johnlinvc
    ҝॄኄ။㠡㠡తʁ

    View full-size slide

  26. Safe Area
    @johnlinvc
    > զ၇ိ໰Ұݸ኷؆ᄸత໰୊ɿզ၇ॴሜաతఔࣜɼେଟ౎ੋኺ಄౸ඌɺ
    ҰߦҰߦԟԼࣥߦɼࣥߦ׬ඟɼఔࣜब݁ଋʀಹኄɼҰݸ GUI ጯ༻ఔࣜ
    —ແ࿦ੋզ၇ݱࡏਖ਼ࡏላशత iOS ᢛ Mac OS Xɺؐੋଖଞฏ୆—ҝॄኄ
    ෆੋଧ։೭ޙҰ࿏ࣥߦ౸ఈ݁ଋɼࣕੋ။ఀཹࡏᦊນத౳଴զ၇ૢ࡞ʁ

    KKBOX iOS/Mac OS X جૅ։ᚙڭࡐ

    View full-size slide

  27. Safe Area
    @johnlinvc
    Run loop

    View full-size slide

  28. Safe Area
    @johnlinvc
    Ұݸ loop ཫ໘ཁ၏తࣄ
    ߋ৽ᙘ໘
    Auto Release
    ᨀᚙ Timer
    ઀Ꮕࣄ݅
    ႔ཧࣄ݅

    View full-size slide

  29. Safe Area
    @johnlinvc
    Run Loop ᔒ༗ݻఆతिظ
    • ཁ౳౸ݱ༗త loop 䋯׬೭ޙɼ࠽။䋯ԼҰݸ loopɻ

    • ೗Ռࡏ Event Handler ཫ໘၏ྃଠଟࣄɼब။ᩋԼҰݸ loop Ԇޙ։
    ࢝ɻ


    0 35 70 105 140
    Loop 1 Loop 2 Loop 2

    View full-size slide

  30. Safe Area
    @johnlinvc
    Ұݸ loop Ֆྃଠଟ࣌ؒ
    • ႔ཧ׈ಈ௒ա 11 ms => ׈ಈ㠡㠡త

    • ႔ཧᴍ㐝௒ա 69 ms => ᴍ㐝㠡㠡త

    View full-size slide

  31. Safe Area
    @johnlinvc
    ղܾํ๏
    • ॏෳར༻ view/cell/asset ɻ

    • ޷޷ར༻ଟ֩৺႔ཧثɻ

    View full-size slide

  32. Safe Area
    @johnlinvc
    ղܾํ๏
    • ॏෳར༻ view/cell/asset ɻ

    • ޷޷ར༻ଟ֩৺႔ཧثɻ

    View full-size slide

  33. Safe Area
    @johnlinvc
    ॏෳ࢖༻ View/Cell
    • UICollectionView

    dequeueReusableCell(withReuseIdentifier:for:)

    • UIImage

    Cached: init(named:)

    Not Cached: init(contentsOfFile:)

    • MKMapView

    dequeueReusableAnnotationView(withIdentifier:)

    View full-size slide

  34. Safe Area
    @johnlinvc
    Reusable
    • https://github.com/AliSoftware/Reusable

    • ՄҎ༻ Type Safe తํ๏ိఆٛ Reuse identifier.

    final class CustomCell: UITableViewCell, Reusable
    tableView.register(cellType: theCellClass.self)
    let cell = tableView.dequeueReusableCell(for: indexPath) as
    MyCustomCell

    View full-size slide

  35. Safe Area
    @johnlinvc
    ղܾํ๏
    • ॏෳར༻ view/cell/asset ɻ

    • ޷޷ར༻ଟ֩৺႔ཧثɻ

    View full-size slide

  36. Safe Area
    @johnlinvc
    ଟ֩৺႔ཧث
    • iPhone 12 ্༗ 6 ݸ CPU ֩৺ɻ

    • ՄҎಉ࣌ࣥߦଟݸࣥߦॹɻ

    • ୞ཁ೺ࣄ৘෼ࢄ౸֤ݸ֩৺ɼबՄҎॖ୹ run loop ႔ཧ࣌ؒɻ

    View full-size slide

  37. Safe Area
    @johnlinvc
    ࡏผత thread ၏ࣄతํ๏
    • Dispatch

    • Combine

    View full-size slide

  38. Safe Area
    @johnlinvc
    ࡏผత thread ၏ࣄతํ๏
    • Dispatch

    • Combine

    View full-size slide

  39. Safe Area
    @johnlinvc
    Dispatch
    • ࢖༻ Obj-C block & Swift closure ိ෧᧋ Thread APIɻ

    • جຊ্बੋ೺ code 㟚౸ queue ཫɼሏब။㢨㟬ࣥߦɻ

    View full-size slide

  40. Safe Area
    @johnlinvc
    Dispatch
    • ༻ DispatchQueue.async बՄҎᩋ code ࡏผత Thread 䋯ɻ

    DispatchQueue.global(qos: .background).async {
    // code to run
    }

    View full-size slide

  41. Safe Area
    @johnlinvc
    Refactoring legacy and concurrent thread API
    with Testing

    View full-size slide

  42. Safe Area
    @johnlinvc
    ࡏผత thread ၏ࣄతํ๏
    • Dispatch

    • Combine

    View full-size slide

  43. Safe Area
    @johnlinvc
    Combine
    • ༻એࠂࣜతޠ๏ိఆٛࢿྉᢛࣄ݅႔ཧྲྀఔɻ

    • Scheduler ՄҎઃఆ૝ཁࡏ䬟Ұݸ Thread ্႔ཧࢿྉᢛࣄ݅ɻ

    View full-size slide

  44. Safe Area
    @johnlinvc
    Combine
    • ՄҎ༻ receive(on:) ੾׵ Thread/Queue

    Just(1)
    .map { _ in print(Thread.isMainThread) } // true
    .receive(on: DispatchQueue.global())
    .map { print(Thread.isMainThread) } // false
    .sink { print(Thread.isMainThread) } // false

    View full-size slide

  45. Safe Area
    @johnlinvc
    ଧ։ Combine తҾ№֖ိ؃؃

    View full-size slide

  46. Safe Area
    @johnlinvc
    UIKit ୞ೳࡏ main Thread ݺڣ
    • UIKit ᔒ༗ Thread Safetyɻ

    • ࡏผత஍ํݺڣత࿩။༗ Error ɻ

    • UI API called on a background thread: -[UIView initWithFrame:]

    View full-size slide

  47. Safe Area
    @johnlinvc
    ආ໔ UIKit thread ໰୊
    • ࡏ Dispatch ཫ༻ DispatchQueue.main ੾׵ճ Main Thread

    • ࡏ Combine ཫ༻ receive(on:) ੾׵ճ Main Thread

    • ࢖༻ SwiftUI

    View full-size slide

  48. Safe Area
    @johnlinvc
    ආ໔ UIKit thread ໰୊
    • ࡏ Dispatch ཫ༻ DispatchQueue.main ੾׵ճ Main Thread

    • ࡏ Combine ཫ༻ receive(on:) ੾׵ճ Main Thread

    • ࢖༻ SwiftUI

    View full-size slide

  49. Safe Area
    @johnlinvc
    Swift UI
    • એࠂࣜత UI Frameworkɻ

    • ލฏ୆ɻ

    • Auto Layoutɻ

    View full-size slide

  50. Safe Area
    @johnlinvc
    ࢖༻ Swift UI
    struct ContentView: View {
    @State var clickCount : Int = 0
    var body: some View {
    Button(action: add) {
    Text("\(clickCount)")
    .font(.system(size: 120))
    }
    }
    func add() {
    clickCount += 1
    }
    }

    View full-size slide

  51. Safe Area
    @johnlinvc
    ࢖༻ Swift UI
    struct ContentView: View {
    @State var clickCount : Int = 0
    var body: some View {
    Button(action: add) {
    Text("\(clickCount)")
    .font(.system(size: 120))
    }
    }
    func add() {
    clickCount += 1
    }
    }

    View full-size slide

  52. Safe Area
    @johnlinvc
    ࢖༻ Swift UI
    struct ContentView: View {
    @State var clickCount : Int = 0
    var body: some View {
    Button(action: add) {
    Text("\(clickCount)")
    .font(.system(size: 120))
    }
    }
    func add() {
    DispatchQueue.global().async{
    clickCount += 1 }
    }
    }

    View full-size slide

  53. Safe Area
    @johnlinvc
    ࢖༻ Swift UI
    struct ContentView: View {
    @State var clickCount : Int = 0
    var body: some View {
    Button(action: add) {
    Text("\(clickCount)")
    .font(.system(size: 120))
    }
    }
    func add() {
    DispatchQueue.global().async{
    clickCount += 1 }
    }
    }

    View full-size slide

  54. Safe Area
    @johnlinvc
    ࢖༻ Swift UI
    • ߋ৽UI ௨ৗ౎ੋಁա @State property wrapperɻ

    • @State ੋ Thread safe తɻՄҎኺ೚Կ Thread मվଞతᆴɻ

    View full-size slide

  55. Safe Area
    @johnlinvc
    ղܾํ๏
    • ॏෳར༻ view/cell/asset ɻ

    • ޷޷ར༻ଟ֩৺႔ཧثɻ

    View full-size slide

  56. Safe Area
    @johnlinvc
    ෆ஌ಓኺ䬟։࢝վʁ

    View full-size slide

  57. Safe Area
    @johnlinvc
    ෧᧋ʂ෧᧋ʂ࠶෧᧋ʂզཁ੒ҝ෧᧋େࢣʂ

    View full-size slide

  58. Safe Area
    @johnlinvc
    ์։ಹݸ MVC

    View full-size slide

  59. Safe Area
    @johnlinvc
    ख೺खት㟬༻ Clean Swift ॏߏ MVC, ฒସ֩৺ఔࣜᛰḤ
    ሜᄸݩଌࢼ

    View full-size slide

  60. Safe Area
    @johnlinvc
    ዑվᆦʁዑվଠٱʁ

    View full-size slide

  61. Safe Area
    @johnlinvc
    ኺᘐཱ։ᚙ౸େܕሢҊత CI/CD ߈ུ

    View full-size slide

  62. Safe Area
    @johnlinvc
    iOS ։ᚙऀ 13 ഒ଎ٕ޼

    View full-size slide

  63. Safe Area
    @johnlinvc
    ࡏผత thread ၏ࣄతํ๏
    • Dispatch

    • Combine

    View full-size slide

  64. Safe Area
    @johnlinvc
    ೗Ռ֩৺໵શᕝྃʁ
    • ਅత༗ଠଟࣄ৘ཁ၏ɼबࢉ༻ྃ Dispatch/Combine/async ໵။㠡ʁ

    • ਅతᔒ༗㭎๏ྃ䆩ʁ

    View full-size slide

  65. Safe Area
    @johnlinvc
    㟬༗ᔒ༗૝աʁ
    • ᙛఔࣜ㠡ॅత࣌ީɼॴ༗తૢ࡞౎။㠡ॅɻ

    • ୞༗Ұछૢ࡞ෆ။ɼܥ౷㚎ݐతɻ૾ੋ Home 伴ɻ

    View full-size slide

  66. Safe Area
    @johnlinvc
    ҝॄኄ Home 伴័ੋ༗༻ʁ
    • ཧ࿦্ɼᙛఔࣜ㠡ࡏ run loop தత࣌ީɼੋᔒ㭎๏઀Ꮕᨀ߇ࣄ݅తɻ

    • طવᔒ㭎๏઀Ꮕࣄ݅ɼ޲্׈తख੎ጯ֘ᔒ༗೚Կ࡞༻࠽ሣʁ

    View full-size slide

  67. Safe Area
    @johnlinvc
    ిᡵՊላܯࠂ
    ిᡵՊላ ࠷৽తᐽՍ
    ೈᱪ޻ఔࢣ 


    View full-size slide

  68. Safe Area
    @johnlinvc
    Darwin-XNU
    macOS & iOS ౳౳࢖༻తఈ
    ૚֩৺

    Mach ੋඍ֩৺

    BSD ఏڙ POSIX & Unix API

    View full-size slide

  69. Safe Area
    @johnlinvc
    ෼࣌ܥ౷
    • iPhone 12 ༗࿡ݸ֩৺ɼୠੋզ၇ჟՄҎಉ࣌䋯্ඦݸ
    DispatchQueue/Threadɻ

    • ҼҝҰݸ֩৺্ՄҎಁա෼࣌ܥ౷䋯௒աҰݸthreadɻ

    View full-size slide

  70. Safe Area
    @johnlinvc
    ෼࣌ܥ౷
    Thread

    View full-size slide

  71. Safe Area
    @johnlinvc
    Context Switch
    • ೺໨લఔࣜతهԱᱪ࿨࢑ଘثత㚎༰

    ه㑚ىိɻ

    • ೺ԼҰݸఔࣜతهԱᱪ࿨࢑ଘثత㚎༰

    ኺه㑚ཫճ෴ɻ

    • ೺ PC (Program Counterʣࢦ޲ԼҰݸఔࣜɼ

    ໵बੋ೺߇੍ᒟަڅԼҰݸఔࣜɻ
    Thread

    View full-size slide

  72. Safe Area
    @johnlinvc
    Context Switch
    • ࡞ۀܥ౷ཁዎኄ೺߇੍ᒟ፤ճိʁ

    • ߹࡞ࣜ੾׵ɿࡏಛఆ࡞ۀ࣌੾׵

    • ඇ߹࡞ࣜ੾׵ɿ࡞ۀܥ౷ܾఆ
    Thread

    View full-size slide

  73. Safe Area
    @johnlinvc
    Context Switch
    • ࡞ۀܥ౷ཁዎኄ೺߇੍ᒟ፤ճိʁ

    • ߹࡞ࣜ੾׵ɿࡏಛఆ࡞ۀ࣌੾׵

    • ඇ߹࡞ࣜ੾׵ɿ࡞ۀܥ౷ܾఆ
    Thread

    View full-size slide

  74. Safe Area
    @johnlinvc
    ߹࡞ࣜ੾׵
    • ᙛఔࣜࣥߦ system call ࣌ ( IO, ݐཱ Threadʣ။೺߇੍ᒟަؐڅ࡞ۀ
    ܥ౷ɼṜݸ࣌ީब༗ػ။၏੾׵ɻ

    • ᙛఔࣜࣥߦඇ๏ࢦྩ࣌ (1/0, ᩇෆ߹๏తهԱᱪҐஔʣ။ᨀᚙ trapɼṜ
    ࣌ީܥ౷໵။औಘ߇੍ᒟɼ໵༗ػ။၏੾׵ɻ

    View full-size slide

  75. Safe Area
    @johnlinvc
    ߹࡞ࣜ੾׵త໰୊
    • ೗ՌఔࣜӬԕෆݺڣsystem call ɻ૾ੋԼ໘Ṝᒬɼब။Ӭԕᔒ㭎๏੾
    ׵ɻ

    • for true { 1+1}

    • MacOS 9 Ҏલੋ࢖༻߹࡞ࣜ੾׵ɼॴҎᄸҰఔࣜ༗Մೳ㠡ॅ੔ݸܥ౷

    View full-size slide

  76. Safe Area
    @johnlinvc
    Run loop ੋҰछ߹࡞ࣜ੾׵
    • 㑌Ұ࣍త loop ౎ཁ౳ Event Handler ႔ཧ׬࠽။ਐԼҰݸ loopɻ

    • Dispatch ཁ౳ݱࡏత task ႔ཧ׬࠽။ަग़߇੍ᒟɻ

    • ॴҎ༗೚ԿҰݸ Event/Task ႔ཧଠٱɼ App ब။㠡ॅɻ

    View full-size slide

  77. Safe Area
    @johnlinvc
    Context Switch
    • ࡞ۀܥ౷ཁዎኄ೺߇੍ᒟ፤ճိʁ

    • ߹࡞ࣜ੾׵ɿࡏಛఆ࡞ۀ࣌੾׵

    • ඇ߹࡞ࣜ੾׵ɿ࡞ۀܥ౷ܾఆ
    Thread

    View full-size slide

  78. Safe Area
    @johnlinvc
    ඇ߹࡞ࣜ੾׵
    • ܥ౷ཁ೺߇੍ᒟަڅԼҰݸ࣌ɼ။ઃఆҰݸߗᱪܭ࣌ثɻ

    • ܭ࣌ث࣌ؒ౸ޙ။ᨀᚙҰݸTrapɼ೺߇੍ᒟڧ੍ަڅܥ౷ɻ

    • ೗Ռ࣌ؒ౸લबަճ߇੍ᒟత࿩ɼबॏஔܭ࣌ثɻ

    View full-size slide

  79. Safe Area
    @johnlinvc
    0x8BADF00D
    • ೗Ռ༗ఔࣜաྃ኷ٱ౎ᔒ༗ਐలɻ

    • Darwin ။⬏ಈ WatchDog Timerɻ೺ಹݸఔࣜऴࢭɻ

    • ࡨޡ୅ᛰबੋ 0x8BADF00D, ate bad foodɻ

    View full-size slide

  80. Safe Area
    @johnlinvc
    Darwin-XNU ࢖༻ඇ߹࡞ࣜ੾׵
    • ᙛᄸҰఔࣜ㠡ॅత࣌ީɼෆ။Өڹଖଞఔࣜɻ

    • ᙛ App 㠡ॅత࣌ީɼ SpringBoard ෆ။ඃӨڹɼॴҎ Home ؐੋՄҎ
    ਖ਼ৗӡ࡞ɻ

    View full-size slide

  81. Safe Area
    @johnlinvc
    ᩋ Run loop ༻ඇ߹࡞੾׵?
    • ઃఆ㑌ݸ loop ՄҎ༻ိ႔ཧ Event త࣌ؒɻᙛ࣌ؒ౸త࣌ީबڧ੍ަ
    ग़߇੍ᒟɻ

    • ԼҰݸ loop ࠶៺᠃߶߶ᔒ၏׬తࣄɻ

    View full-size slide

  82. Safe Area
    @johnlinvc
    Bringing Theory Into Practice: A Userspace Library for Multicore
    Real-Time Scheduling
    • ࡏ Linux త Userspace ্ࣥߦత noncooperative schedulerɻ

    • ՄҎࡏ࢖༻ऀతఔࣜ㚎໛ٖ OS త੾׵ɻ

    • ໵बੋ㘸ɼՄҎᩋ UI ෆ။㠡ॅɻ

    View full-size slide

  83. Safe Area
    @johnlinvc
    Մೳతመ࡞ํ๏
    • ಛ੡త Combine Scheduler ɼᙛ࣌ؒ౸बఀࢭɼԼҰݸ loop ࣗಈॏ
    ࢼɻ

    • Swift main branch መᱛதత Async/Await + Actorɼᙛᔒ༗ڞڗهԱᱪ
    ࣌ɼtask बՄҎॏࢼ׌ᔒ༗ෆ૝ཁత෭࡞༻ɻ

    View full-size slide

  84. Safe Area
    @johnlinvc
    run loop ඇ߹࡞੾׵త᠍ᴍ
    • ᔒሜ޷త࿩ɼՄೳ။༗ଟ࣍త෭࡞༻ɼ૾ੋ print 䋯ྃၷ࣍ɻ

    • ᙘ໘Մೳ။༗ෆ׬੔తߋ৽ɻҼҝߋ৽౸Ұ൒बඃதᏗྃɻ

    View full-size slide

  85. Safe Area
    @johnlinvc
    UIKit SwiftUI
    ඇ߹࡞ࣜ 

    run loop
    ༏ᴍ ࠷੒ख़
    ՄҎ೺ॴ༗తࣄ౎
    Ҡ౸ଖଞ Thread
    ሜग़ိతAppҰఆෆ
    ။㠡㠡
    ᠍ᴍ
    ࠷೉ሜग़ෆ။㠡㠡
    తApp
    ؐෆ੒ख़
    ᙘ໘Մೳߋ৽ෆ׬શ

    ޷૾ؐᔒ༗ਓ၏

    View full-size slide

  86. Safe Area
    @johnlinvc
    ॄኄੋ App 㠡㠡 ?
    • ᙘ໘తߋ৽ස཰௿ԙ 76Hz

    • ׈ಈ௒ա 11 ms ᔒ༗ճᰶ

    • ᴍ㐝௒ա 69 ms ᔒ༗ճᰶ

    View full-size slide

  87. Safe Area
    @johnlinvc
    App 㠡㠡త
    • Run loop తҰݸ loop 䋯ྃଠٱɻ䇪౸ྃԼҰݸ loopɻ

    • ᔒ㭎๏ߋ৽ᙘ໘ɻ

    • ᔒ㭎๏઀Ꮕ༌ೖɻ

    View full-size slide

  88. Safe Area
    @johnlinvc
    App 㠡㠡త႔ཧํ๏
    • ॖ୹ӡࢉ࣌ؒɼ࢖༻շऔɼCell Reuseɻ

    • ࢖༻ GCD, Combine ိ೺ࣄ৘Ҡ౸ run loop ֎ࣥߦɻ

    View full-size slide

  89. Safe Area
    @johnlinvc
    ະိల๬
    • Async / Await ՄҎᩋզ၇ߋ༰қ೺ࣄ৘Ҡ౸ଖଞ Thread ႔ཧɻ

    • 㘸ෆఆՄҎࡏ App 㚎၏ඇ߹࡞ࣜ schedulingɼ༗ڵझతՄҎ౳౳ိҰ
    ىݚڀݚڀɻ

    View full-size slide

  90. Safe Area
    @johnlinvc
    References
    • https://stackoverflow.com/questions/18738453/how-does-the-compiler-
    know-a-button-has-been-pressed/18739381

    • https://www.objc.io/issues/3-views/moving-pixels-onto-the-screen/

    • https://zonble.gitbooks.io/kkbox-ios-dev/content/responder/run_loop.html

    • http://pages.cs.wisc.edu/~remzi/OSTEP/

    • https://yuriktech.com/2020/03/07/User-Space-Scheduling/

    • Apple Developer Documentation

    View full-size slide

  91. Safe Area
    @johnlinvc
    References
    • https://semianalysis.com/apples-a14-packs-134-million-transistors-
    mm2-but-falls-far-short-of-tsmcs-density-claims/

    • https://ckitakishi.com/2016/08/17/WatchDog-
    %E6%9C%BA%E5%88%B6/

    View full-size slide

  92. Safe Area
    @johnlinvc
    Q&A
    • ౤ӨยతҐஔࡏ https://bit.ly/3n4rZO4

    • John Lin

    Twitter: @johnlinvc

    • ᓣܴိ Swift Taipei ෼ڗ҃ᡅᡅ

    View full-size slide