ConcurrencyWithGCD

 ConcurrencyWithGCD

近年のモバイルデバイスのプロセッサはマルチコア化が進んでおり、iOSデバイスも例外ではありません。たとえば、最新のiPhoneには6つのコアが搭載されています。

今後もコアが増えていくことは確実で、iOSエンジニアである私たちにとって多くのコアを活かすための知見はますます重要になっています。

そこで、このトークではGrand Central DispatchとSwiftを使ってソートや探索といった基本的なアルゴリズムを並行化する手法を学び、コアをもっと有効に使うための考え方を説明します。

このトークを観て、Grand Central Dispatchを使ったマルチスレッドプログラミングの新たな可能性に触れてみましょう。

Da1305e5281b5208da85d14a356d01f3?s=128

Takanori Hirobe

September 06, 2020
Tweet

Transcript

  1. Takanori Hirobe Grand Central DispatchʹΑΔ
 ฒߦΞϧΰϦζϜೖ໳ iOSDC JAPAN 2020

  2. ࣗݾ঺հ • ኍ෦ وಙ( Takanori Hirobe ) • Twitter, Github,

    Qiita: @taka1068
  3. ࣗݾ঺հ • ኍ෦ وಙ( Takanori Hirobe ) • Twitter, Github,

    Qiita: @taka1068 • ϑΣϯϦϧגࣜձࣾॴଐ
  4. ࣗݾ঺հ • ኍ෦ وಙ( Takanori Hirobe ) • Twitter, Github,

    Qiita: @taka1068 • ϑΣϯϦϧגࣜձࣾॴଐ ϓϥνφεϙϯαʔ
  5. ࢿྉʹ͍ͭͯ ϋογϡλά෇͖ͰπΠʔτ͠·͢ • εϥΠυpdf • https://speakerdeck.com/taka1068/concurrencywithgcd
 
 
 • શαϯϓϧίʔυ

    • https://github.com/taka1068/iOSDC-2020-demo
  6. ΞδΣϯμ • ࠷ۙͷiOSσόΠεͱϚϧνίΞԽʹؔ͢Δ՝୊ • for loopΛฒߦԽ͢Δ • ΫΠοΫιʔτΛฒߦԽ͢Δ • ͍Ζ͍ΖͳΦʔόʔϔου

  7. ΞδΣϯμ • ࠷ۙͷiOSσόΠεͱϚϧνίΞԽʹؔ͢Δ՝୊ • for loopΛฒߦԽ͢Δ • ΫΠοΫιʔτΛฒߦԽ͢Δ • ͍Ζ͍ΖͳΦʔόʔϔου

  8. ࠷ۙͷiOSσόΠεͱϚϧνίΞ • iPhone 11 • 6 ίΞ • iPad Pro

    • 8 ίΞ
  9. ࠷ۙͷiOSσόΠεͱϚϧνίΞ • iPhone 11 • 6 ίΞ • iPad Pro

    • 8 ίΞ • Mac Pro • ࠷େ28 ίΞ
  10. • ࠓޙ΋ϚϧνίΞԽ͕ਐΉ͜ͱ͸࣮֬ • iOSΤϯδχΞʹͱͬͯɺϚϧνίΞΛ׆͔͢஌ݟ΋ॏཁʹͳͬͯ͘Δ͸ͣ

  11. • ࠓޙ΋ϚϧνίΞԽ͕ਐΉ͜ͱ͸࣮֬ • iOSΤϯδχΞʹͱͬͯɺϚϧνίΞΛ׆͔͢஌ݟ΋ॏཁʹͳͬͯ͘Δ͸ͣ

  12. Grand Central Dispatch

  13. Grand Central Dispatch • Apple७ਖ਼ͷϑϨʔϜϫʔΫ • Swift΍Xcodeͱͷ਌࿨ੑ͕ߴ͍ • watchOS͔ΒmacOS·Ͱ࢖͑Δ •

    pthreadͳͲͱൺ΂Δͱ࢖͍΍͍͢
  14. DispatchQueue.main.async {} ͔͠࢖ͬͨ͜ͱͳ͍ ϚϧνεϨου͸ͳΜͱͳۤ͘ख

  15. Grand Central Dispatchʹ਌͠΋͏

  16. Grand Central Dispatchʹ਌͠΋͏ • Grand Central DispatchΛ࢖ͬͨฒߦΞϧΰϦζϜʹೖ໳

  17. ฒߦΞϧΰϦζϜͱ͸ • ΞϧΰϦζϜͷ֤෦෼Λฒߦʹॲཧ͢ΔΞϧΰϦζϜ

  18. ฒߦΞϧΰϦζϜͱ͸ • ΞϧΰϦζϜͷ֤෦෼Λฒߦʹॲཧ͢ΔΞϧΰϦζϜ

  19. ฒߦΞϧΰϦζϜͷ۩ମྫ

  20. ΞδΣϯμ • ࠷ۙͷiOSσόΠεͱϚϧνίΞԽʹؔ͢Δ՝୊ • for loopΛฒߦԽ͢Δ • ΫΠοΫιʔτΛฒߦԽ͢Δ • ͍Ζ͍ΖͳΦʔόʔϔου

  21. ՝୊ • Stringͷ഑ྻͷ .count ͷ߹ܭΛܭࢉ͍ͨ͠

  22. ՝୊ • Stringͷ഑ྻͷ .count ͷ߹ܭΛܭࢉ͍ͨ͠

  23. ՝୊ • Stringͷ഑ྻͷ .count ͷ߹ܭΛܭࢉ͍ͨ͠ 36͔ࠃޠͷѫࡰ

  24. ՝୊ • Stringͷ഑ྻͷ .count ͷ߹ܭΛܭࢉ͍ͨ͠

  25. ՝୊ • Stringͷ഑ྻͷ .count ͷ߹ܭΛܭࢉ͍ͨ͠

  26. ՝୊ • Stringͷ഑ྻͷ .count ͷ߹ܭΛܭࢉ͍ͨ͠ map Λฒߦॲཧ͍ͨ͠

  27. Grand Central DispatchΛ࢖͑͹ ؆୯ʹॻ͚Δ

  28. https://developer.apple.com/documentation/dispatch/dispatchqueue/2016088-concurrentperform

  29. None
  30. ΫϩʔδϟࣜΛฒߦॲཧ

  31. doSomething(0), doSomething(1), ..., doSomething(9) ͕ฒߦͰॲཧ͞ΕΔ

  32. • DispatchQueue.concurrentPerform ͸forจͷฒߦॲཧ൛ͱݴ͑Δ

  33. • DispatchQueue.concurrentPerform ͸forจͷฒߦॲཧ൛ͱݴ͑Δ • forจ͕͋Ε͹mapΛॻ͘ͷ͸؆୯

  34. SwiftͱGrandCentralDispatchͰ ฒߦmapΛॻ͍ͯΈΔ

  35. None
  36. None
  37. nil ͰॳظԽ͓ͯ͘͠

  38. ͜ͷ෦෼͕ฒྻॲཧ͞ΕΔ

  39. None
  40. None
  41. None
  42. unwrap ͯ͠ return

  43. None
  44. ෳ਺εϨου͔Βॻ͖ࠐΈ

  45. ෳ਺εϨου͔Βॻ͖ࠐΈ ΫϥογϡͷՄೳੑ͕͋Δ

  46. None
  47. γϦΞϧͳDispatchQueue

  48. γϦΞϧͳDispatchQueue ෳ਺εϨου͔Βͷಉ࣌ΞΫηεΛ๷͙

  49. None
  50. ϕϯνϚʔΫ

  51. ϕϯνϚʔΫ • 36ϲࠃޠͷѫࡰͷcountΛ߹ܭ

  52. ϕϯνϚʔΫ • 36ϲࠃޠͷѫࡰͷcountΛ߹ܭ - 5ճଌఆͨ͠ฏۉ஋Ͱ͢ɻ - εϥΠυʹ৐ͤͨίʔυ͸આ໌༻Ͱ͢ɻ࣮ࡍʹଌఆʹར༻ͨ͠ίʔυ͸Githubʹ͋Γ·͢ɻ - MacBook Pro

    15-inch, 2018, Core i9 2.9 GHz. - macOS Catalina 10.15.6, Swift 5.2.4, ࠷దԽ͋Γ ( -O ) Array.map concurrentMap 0.00024 seconds 0.00064 seconds
  53. ϕϯνϚʔΫ • 36ϲࠃޠͷѫࡰͷcountΛ߹ܭ - 5ճଌఆͨ͠ฏۉ஋Ͱ͢ɻ - εϥΠυʹ৐ͤͨίʔυ͸આ໌༻Ͱ͢ɻ࣮ࡍʹଌఆʹར༻ͨ͠ίʔυ͸Githubʹ͋Γ·͢ɻ - MacBook Pro

    15-inch, 2018, Core i9 2.9 GHz. - macOS Catalina 10.15.6, Swift 5.2.4, ࠷దԽ͋Γ ( -O ) Array.map concurrentMap 0.00024 seconds 0.00064 seconds 0 sec 0.001 sec Array.map concurrentMap
  54. ϕϯνϚʔΫ • 36ϲࠃޠͷѫࡰͷcountΛ߹ܭ • ฒߦԽͨ͠ͷʹ஗͘ͳ͍ͬͯΔ - 5ճଌఆͨ͠ฏۉ஋Ͱ͢ɻ - εϥΠυʹ৐ͤͨίʔυ͸આ໌༻Ͱ͢ɻ࣮ࡍʹଌఆʹར༻ͨ͠ίʔυ͸Githubʹ͋Γ·͢ɻ -

    MacBook Pro 15-inch, 2018, Core i9 2.9 GHz. - macOS Catalina 10.15.6, Swift 5.2.4, ࠷దԽ͋Γ ( -O ) Array.map concurrentMap 0.00024 seconds 0.00064 seconds 0 sec 0.001 sec Array.map concurrentMap
  55. ͳͥ஗͘ͳͬͨͷ͔ • ѫࡰఔ౓ͷจࣈ਺Χ΢ϯτͳΒॠ࣌ʹܭࢉͰ͖Δ • Ή͠ΖΦʔόʔϔουͰੑೳ͕མ͍ͪͯΔ

  56. ͳͥ஗͘ͳͬͨͷ͔ • ѫࡰఔ౓ͷจࣈ਺Χ΢ϯτͳΒॠ࣌ʹܭࢉͰ͖Δ • Ή͠ΖΦʔόʔϔουͰੑೳ͕མ͍ͪͯΔ γϦΞϧͳQueueʹΑΔɺෳ਺εϨου͔Βͷॻ͖ࠐΈʹର͢Δอޢ

  57. ݁Ռͷॻ͖ࠐΈ͸ҰͭͷεϨουͷΈ

  58. ݁Ռͷॻ͖ࠐΈ͸ҰͭͷεϨουͷΈ Φʔόʔϔου

  59. ίΞ0 ίΞ1 ίΞ2 ίΞ3 .count .count .count .count ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ

    ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ Overhead Overhead Overhead .count .count .count .count Overhead ഑ྻ΁ͷ୅ೖ Overhead Overhead ࣌ؒ
  60. ίΞ0 ίΞ1 ίΞ2 ίΞ3 .count .count .count .count ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ

    ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ Overhead Overhead Overhead .count .count .count .count Overhead ഑ྻ΁ͷ୅ೖ Overhead Overhead ͋ΔॠؒͰͷ഑ྻ΁ͷ୅ೖ͸ɺ
 1ͭͷεϨου͔͠ڐ͞Εͳ͍ ࣌ؒ
  61. ίΞ0 ίΞ1 ίΞ2 ίΞ3 .count .count .count .count Overhead Overhead

    Overhead .count .count .count Overhead Overhead Overhead ͋ΔॠؒͰͷ഑ྻ΁ͷ୅ೖ͸ɺ
 1ͭͷεϨου͔͠ڐ͞Εͳ͍ ࣌ؒ ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ .count ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ
  62. ίΞ0 ίΞ1 ίΞ2 ίΞ3 .count .count .count .count ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ

    ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ Overhead Overhead Overhead .count .count .count .count Overhead ഑ྻ΁ͷ୅ೖ Overhead Overhead ଞͷεϨου͕ॻ͖ࠐΜͰ͍Δ࣌ɺ
 ͦΕ͕ऴΘΔ·Ͱ଴ͭඞཁ͕͋Δ ࣌ؒ
  63. ίΞ0 ίΞ1 ίΞ2 ίΞ3 .count .count .count .count ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ

    ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ Overhead Overhead Overhead .count .count .count .count Overhead ഑ྻ΁ͷ୅ೖ Overhead Overhead ଞͷεϨου͕ॻ͖ࠐΜͰ͍Δ࣌ɺ
 ͦΕ͕ऴΘΔ·Ͱ଴ͭඞཁ͕͋Δ ଴ͪ࣌ؒ: Φʔόʔϔου ࣌ؒ
  64. ίΞ0 ίΞ1 ίΞ2 ίΞ3 .count .count .count .count ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ

    ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ Overhead Overhead Overhead .count .count .count .count Overhead ഑ྻ΁ͷ୅ೖ Overhead Overhead ଞͷεϨου͕ॻ͖ࠐΜͰ͍Δ࣌ɺ
 ͦΕ͕ऴΘΔ·Ͱ଴ͭඞཁ͕͋Δ ଴ͪ࣌ؒ: Φʔόʔϔου ࣌ؒ
  65. ίΞ0 ίΞ1 ίΞ2 ίΞ3 .count .count .count .count ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ

    ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ Overhead Overhead Overhead .count .count .count .count Overhead ഑ྻ΁ͷ୅ೖ Overhead Overhead ฒߦԽͷԸܙΑΓ Φʔόʔϔουͷํ͕ϖφϧςΟ͕େ͖͍ ࣌ؒ
  66. ίΞ0 ίΞ1 ίΞ2 ίΞ3 .count .count .count .count ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ

    ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ Overhead Overhead Overhead .count .count .count .count Overhead ഑ྻ΁ͷ୅ೖ Overhead Overhead < Ըܙ ϖφϧςΟ ࣌ؒ
  67. ίΞ0 ίΞ1 ίΞ2 ίΞ3 .count .count .count .count ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ

    ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ Overhead Overhead Overhead .count .count .count .count Overhead ഑ྻ΁ͷ୅ೖ Overhead Overhead ҰͭҰͭͷλεΫ ͕খ͗͢͞Δ ࣌ؒ
  68. ίΞ0 ίΞ1 ίΞ2 ίΞ3 λεΫ λεΫ λεΫ λεΫ Overhead Overhead

    Overhead λεΫ λεΫ λεΫ ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ λεΫ ࣌ؒ
  69. ίΞ0 ίΞ1 ίΞ2 ίΞ3 λεΫ λεΫ λεΫ λεΫ Overhead Overhead

    Overhead λεΫ λεΫ λεΫ ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ λεΫ > Ըܙ ϖφϧςΟ Φʔόʔϔουͷ઎ΊΔׂ߹ΛݮΒ͢ ࣌ؒ
  70. ॏ͍ॲཧͰϕϯνϚʔΫ

  71. ॏ͍ॲཧͰϕϯνϚʔΫ • Ն໨ᕸੴͷʮ͜͜Ζʯͷ๯಄ͷ 57631 จࣈ͔Β׽ࣈͷݸ਺ΛΧ΢ϯτ • ഑ྻͷཁૉʹ͸1ஈམΛ·Δ͝ͱೖΕͯܭଌ

  72. None
  73. None
  74. ॏ͍ॲཧͰϕϯνϚʔΫ • Ն໨ᕸੴͷʮ͜͜Ζʯͷ๯಄ͷ 57631 จࣈ͔Β׽ࣈͷݸ਺ΛΧ΢ϯτ • ഑ྻͷཁૉʹ͸1ஈམΛ·Δ͝ͱೖΕͯܭଌ

  75. ॏ͍ॲཧͰϕϯνϚʔΫ • Ն໨ᕸੴͷʮ͜͜Ζʯͷ๯಄ͷ 57631 จࣈ͔Β׽ࣈͷݸ਺ΛΧ΢ϯτ • ഑ྻͷཁૉʹ͸1ஈམΛ·Δ͝ͱೖΕͯܭଌ - 5ճଌఆͨ͠ฏۉ஋Ͱ͢ɻ -

    εϥΠυʹ৐ͤͨίʔυ͸આ໌༻Ͱ͢ɻ
 ࣮ࡍʹଌఆʹར༻ͨ͠ίʔυ͸Githubʹ͋Γ·͢ɻ - MacBook Pro 15-inch, 2018, Core i9 2.9 GHz. - macOS Catalina 10.15.6, Swift 5.2.4, ࠷దԽ͋Γ ( -O ) Array.map concurrentMap 0.0082 seconds 0.0047 seconds
  76. ॏ͍ॲཧͰϕϯνϚʔΫ • Ն໨ᕸੴͷʮ͜͜Ζʯͷ๯಄ͷ 57631 จࣈ͔Β׽ࣈͷݸ਺ΛΧ΢ϯτ • ഑ྻͷཁૉʹ͸1ஈམΛ·Δ͝ͱೖΕͯܭଌ - 5ճଌఆͨ͠ฏۉ஋Ͱ͢ɻ -

    εϥΠυʹ৐ͤͨίʔυ͸આ໌༻Ͱ͢ɻ
 ࣮ࡍʹଌఆʹར༻ͨ͠ίʔυ͸Githubʹ͋Γ·͢ɻ - MacBook Pro 15-inch, 2018, Core i9 2.9 GHz. - macOS Catalina 10.15.6, Swift 5.2.4, ࠷దԽ͋Γ ( -O ) Array.map concurrentMap 0.0082 seconds 0.0047 seconds 0 sec 0.01 sec Array.map concurrentMap
  77. ஫ҙ఺ ฒߦԽ͸ສೳͰ͸ͳ͍ • े෼ʹॏ͍ॲཧΛฒߦԽ͠ͳ͍ͱҙຯ͕ͳ͍ɻ • ฒߦԽͷԸܙ͕ɺΦʔόʔϔουʹΑΔϖφϧςΟΛ্ճΔඞཁ͕͋Δɻ

  78. ஫ҙ఺ ฒߦԽ͸ສೳͰ͸ͳ͍ • े෼ʹॏ͍ॲཧΛฒߦԽ͠ͳ͍ͱҙຯ͕ͳ͍ɻ • ฒߦԽͷԸܙ͕ɺΦʔόʔϔουʹΑΔϖφϧςΟΛ্ճΔඞཁ͕͋Δɻ • ฒߦԽͷԸܙΛड͚ΒΕΔΑ͏ʹλεΫΛׂΓৼΔඞཁ͕͋Δɻ

  79. ͳʹ͕ѱ͔ͬͨͷ͔ ࠷ॳͷྫʹ໭Δ

  80. ͳʹ͕ѱ͔ͬͨͷ͔ ࠷ॳͷྫʹ໭Δ • ҰͭҰͭͷλεΫ(഑ྻͷཁૉ͕ࡉ੾Εʹͳ͍ͬͯΔ)

  81. ͳʹ͕ѱ͔ͬͨͷ͔ ࠷ॳͷྫʹ໭Δ • ҰͭҰͭͷλεΫ(഑ྻͷཁૉ͕ࡉ੾Εʹͳ͍ͬͯΔ) • ຊ౰ʹ΍Γ͍ͨλεΫΑΓΦʔόʔϔουʹ࣌ؒΛඅ΍ͯ͠͠·ͬͨ

  82. ͜͜·Ͱͷ·ͱΊ • DispatchQueue.concurrentPerform͸forจͷฒߦॲཧ൛ • ฒߦԽʹͱ΋ͳ͍ɺಛผͳέΞ͕ඞཁʹͳΔ͜ͱ͕͋Δ

  83. ͜͜·Ͱͷ·ͱΊ • DispatchQueue.concurrentPerform͸forจͷฒߦॲཧ൛ • ฒߦԽʹͱ΋ͳ͍ɺಛผͳέΞ͕ඞཁʹͳΔ͜ͱ͕͋Δ • CPUͷίΞ͕ຊ౰ʹ΍Γ͍ͨ͜ͱʹ࣌ؒΛඅ΍ͤΔΑ͏ʹ͓હཱͯͯ͋͛͠ Δͷ͕๻ͨͪͷ࢓ࣄ

  84. ΞδΣϯμ • ࠷ۙͷiOSσόΠεͱϚϧνίΞԽʹؔ͢Δ՝୊ • for loopΛฒߦԽ͢Δ • ΫΠοΫιʔτΛฒߦԽ͢Δ • ͍Ζ͍ΖͳΦʔόʔϔου

  85. ΫΠοΫιʔτ

  86. array 0 14 1 2 3 4 5 6 7

    8 9 10 11 12 13 59 75 92 99 77 15 13 2 68 31 83 35 67 51 70
  87. array 0 14 1 2 3 4 5 6 7

    8 9 10 11 12 13 59 75 92 99 77 15 13 2 68 31 83 35 67 51 70 pivot
  88. array 0 14 1 2 3 4 5 6 7

    8 9 10 11 12 13 59 75 92 99 77 15 13 2 68 31 83 35 67 51 70 array 0 14 1 2 3 4 5 6 7 8 9 10 11 12 13 15 13 92 2 31 35 51 59 75 99 77 68 83 67 70 Partition
  89. array 0 14 1 2 3 4 5 6 7

    8 9 10 11 12 13 59 75 92 99 77 15 13 2 68 31 83 35 67 51 70 array 0 14 1 2 3 4 5 6 7 8 9 10 11 12 13 15 13 92 2 31 35 51 59 75 99 77 68 83 67 70 <= 59 Partition
  90. array 0 14 1 2 3 4 5 6 7

    8 9 10 11 12 13 59 75 92 99 77 15 13 2 68 31 83 35 67 51 70 array 0 14 1 2 3 4 5 6 7 8 9 10 11 12 13 15 13 92 2 31 35 51 59 75 99 77 68 83 67 70 <= 59 59 < Partition
  91. array 0 1 2 3 4 5 15 13 2

    31 35 51 14 7 8 9 10 11 12 13 92 75 99 77 68 83 67 70
  92. array 0 1 2 3 4 5 15 13 2

    31 35 51 14 7 8 9 10 11 12 13 92 75 99 77 68 83 67 70 pivot pivot
  93. array 0 1 2 3 4 5 15 13 2

    31 35 51 Partition 14 7 8 9 10 11 12 13 92 75 99 77 68 83 67 70 Partition
  94. array 0 1 2 3 4 5 15 13 2

    31 35 51 Partition array 0 1 2 3 4 5 2 13 15 31 35 51 14 7 8 9 10 11 12 13 92 75 99 77 68 83 67 70 Partition 14 7 8 9 10 11 12 13 99 67 68 70 75 77 83 92
  95. array 0 1 2 3 4 5 15 13 2

    31 35 51 Partition array 0 1 2 3 4 5 2 13 15 31 35 51 14 7 8 9 10 11 12 13 92 75 99 77 68 83 67 70 Partition 14 7 8 9 10 11 12 13 99 67 68 70 75 77 83 92
  96. 59 99 75 92 15 13 2 31 35 68

    83 67 51 70 77 a 0 14 1 2 3 4 5 6 7 8 9 10 11 12 13 15 2 13 31 35 51 59 99 35 75 83 67 51 70 77 a 0 14 1 2 3 4 5 6 7 8 9 10 11 12 13 2 15 13 31 35 51 a 0 1 2 3 4 5 68 75 67 70 92 83 77 99 14 7 8 9 10 11 12 13 a 2 13 a 0 1 31 35 51 3 4 5 35 51 4 5 51 5 13 1 68 67 70 7 8 9 68 70 8 9 70 9 83 77 92 99 14 11 12 13 99 14 77 83 11 12 83 12
  97. 59 99 75 92 15 13 2 31 35 68

    83 67 51 70 77 a 0 14 1 2 3 4 5 6 7 8 9 10 11 12 13 15 2 13 31 35 51 99 35 75 83 67 51 70 77 a 0 14 1 2 3 4 5 6 7 8 9 10 11 12 13 2 13 31 35 51 a 0 1 2 3 4 5 68 67 70 92 83 77 99 14 7 8 9 10 11 12 13 a 13 a 0 1 35 51 3 4 5 51 4 5 5 1 68 70 7 8 9 70 8 9 9 83 77 99 14 11 12 13 14 83 11 12 12 59 15 75 2 31 35 51 13 67 68 70 92 99 77 83
  98. 59 15 75 2 31 35 51 13 67 68

    70 92 99 77 83
  99. 59 15 75 2 31 35 51 13 67 68

    70 92 99 77 83
  100. ΫΠοΫιʔτͷΞϧΰϦζϜ

  101. ΫΠοΫιʔτͷΞϧΰϦζϜ array 0 1 2 3 4 5 6 7

    5 2 1 4 6 8 7 3
  102. ΫΠοΫιʔτͷΞϧΰϦζϜ 1. ϐϘοτΛબͼ array 0 1 2 3 4 5

    6 7 5 2 1 4 6 8 7 3
  103. ΫΠοΫιʔτͷΞϧΰϦζϜ 1. ϐϘοτΛબͼ 2. ഑ྻΛϐϘοτҎԼͷ෦෼ͱϐϘοτΑΓେ͖͍෦෼ͱʹ෼͚Δ array 0 1 2 3

    4 5 6 7 2 1 4 3 5 8 7 6
  104. ΫΠοΫιʔτͷΞϧΰϦζϜ 1. ϐϘοτΛબͼ 2. ഑ྻΛϐϘοτҎԼͷ෦෼ͱϐϘοτΑΓେ͖͍෦෼ͱʹ෼͚Δ array 0 1 2 3

    4 5 6 7 2 1 4 3 5 8 7 6 ύʔςΟγϣϯͱ΋ݺͿ
  105. ΫΠοΫιʔτͷΞϧΰϦζϜ 1. ϐϘοτΛબͼ 2. ഑ྻΛϐϘοτҎԼͷ෦෼ͱϐϘοτΑΓେ͖͍෦෼ͱʹ෼͚Δ array 0 1 2 3

    4 5 6 7 2 1 4 3 5 8 7 6
  106. ΫΠοΫιʔτͷΞϧΰϦζϜ 1. ϐϘοτΛબͼ 2. ഑ྻΛϐϘοτҎԼͷ෦෼ͱϐϘοτΑΓେ͖͍෦෼ͱʹ෼͚Δ array 0 1 2 3

    4 5 6 7 2 1 4 3 5 8 7 6
  107. ΫΠοΫιʔτͷΞϧΰϦζϜ 1. ϐϘοτΛબͼ 2. ഑ྻΛϐϘοτҎԼͷ෦෼ͱϐϘοτΑΓେ͖͍෦෼ͱʹ෼͚Δ 3. ϐϘοτҎԼͷ෦෼ʹ1, 2Λ܁Γฦ͢ array 0

    1 2 3 4 5 6 7 2 1 4 3 5 8 7 6 array 0 1 2 3 2 1 4 3
  108. ΫΠοΫιʔτͷΞϧΰϦζϜ 1. ϐϘοτΛબͼ 2. ഑ྻΛϐϘοτҎԼͷ෦෼ͱϐϘοτΑΓେ͖͍෦෼ͱʹ෼͚Δ 3. ϐϘοτҎԼͷ෦෼ʹ1, 2Λ܁Γฦ͢ array 0

    1 2 3 4 5 6 7 2 1 4 3 5 8 7 6 array 0 1 2 3 2 1 4 3
  109. ΫΠοΫιʔτͷΞϧΰϦζϜ 1. ϐϘοτΛબͼ 2. ഑ྻΛϐϘοτҎԼͷ෦෼ͱϐϘοτΑΓେ͖͍෦෼ͱʹ෼͚Δ 3. ϐϘοτҎԼͷ෦෼ʹ1, 2Λ܁Γฦ͢ array 0

    1 2 3 4 5 6 7 2 1 4 3 5 8 7 6 array 0 1 2 3 2 1 4 3
  110. ΫΠοΫιʔτͷΞϧΰϦζϜ 1. ϐϘοτΛબͼ 2. ഑ྻΛϐϘοτҎԼͷ෦෼ͱϐϘοτΑΓେ͖͍෦෼ͱʹ෼͚Δ 3. ϐϘοτҎԼͷ෦෼ʹ1, 2Λ܁Γฦ͢ array 0

    1 2 3 4 5 6 7 2 1 4 3 5 8 7 6 array 0 1 2 3 1 2 4 3
  111. ΫΠοΫιʔτͷΞϧΰϦζϜ 1. ϐϘοτΛબͼ 2. ഑ྻΛϐϘοτҎԼͷ෦෼ͱϐϘοτΑΓେ͖͍෦෼ͱʹ෼͚Δ 3. ϐϘοτҎԼͷ෦෼ʹ1, 2Λ܁Γฦ͢ 4. ϐϘοτΑΓେ͖͍෦෼ʹ1,

    2Λ܁Γฦ͢ array 0 1 2 3 4 5 6 7 2 1 4 3 5 8 7 6 array 0 1 2 3 1 2 4 3 array 5 6 7 8 7 6
  112. ΫΠοΫιʔτͷΞϧΰϦζϜ 1. ϐϘοτΛબͼ 2. ഑ྻΛϐϘοτҎԼͷ෦෼ͱϐϘοτΑΓେ͖͍෦෼ͱʹ෼͚Δ 3. ϐϘοτҎԼͷ෦෼ʹ1, 2Λ܁Γฦ͢ 4. ϐϘοτΑΓେ͖͍෦෼ʹ1,

    2Λ܁Γฦ͢ array 0 1 2 3 4 5 6 7 2 1 4 3 5 8 7 6 array 0 1 2 3 1 2 4 3 array 5 6 7 8 7 6
  113. ΫΠοΫιʔτͷΞϧΰϦζϜ 1. ϐϘοτΛબͼ 2. ഑ྻΛϐϘοτҎԼͷ෦෼ͱϐϘοτΑΓେ͖͍෦෼ͱʹ෼͚Δ 3. ϐϘοτҎԼͷ෦෼ʹ1, 2Λ܁Γฦ͢ 4. ϐϘοτΑΓେ͖͍෦෼ʹ1,

    2Λ܁Γฦ͢ array 0 1 2 3 4 5 6 7 2 1 4 3 5 8 7 6 array 0 1 2 3 1 2 4 3 array 5 6 7 6 7 8
  114. ΫΠοΫιʔτͷΞϧΰϦζϜ 1. ϐϘοτΛબͼ 2. ഑ྻΛϐϘοτҎԼͷ෦෼ͱϐϘοτΑΓେ͖͍෦෼ͱʹ෼͚Δ 3. ϐϘοτҎԼͷ෦෼ʹ1, 2Λ܁Γฦ͢ 4. ϐϘοτΑΓେ͖͍෦෼ʹ1,

    2Λ܁Γฦ͢ 5. Ҏ্Λ࠶ىతʹ܁Γฦ͢ array 0 1 2 3 4 5 6 7 2 1 4 3 5 8 7 6 array 0 1 2 3 1 2 4 3 array 5 6 7 6 7 8
  115. ΫΠοΫιʔτΛฒߦԽ͢Δ

  116. ΫΠοΫιʔτΛฒߦԽ͢Δ

  117. ݴ͍׵͑Δͱɺ

  118. ΫΠοΫιʔτͷΞϧΰϦζϜͷத͔Βɺ ฒߦԽ͢Δ͜ͱ͕Ͱ͖ΔλεΫΛݟ͚ͭΔ

  119. ฒߦԽ͢Δ͜ͱ͕Ͱ͖ΔλεΫͱ͸

  120. ฒߦԽ͢Δ͜ͱ͕Ͱ͖ΔλεΫͱ͸ • ޓ͍ʹಠཱͨ͠ॲཧ͸ฒߦԽ͢Δ͜ͱ͕Ͱ͖Δ • ಠཱͨ͠ॲཧ͕શͯऴΘͬͨޙɺͦΕͧΕΛ·ͱΊͯҰͭͷ݁ՌΛಘΔ

  121. ฒߦԽ͢Δ͜ͱ͕Ͱ͖ΔλεΫͱ͸ • ޓ͍ʹಠཱͨ͠ॲཧ͸ฒߦԽ͢Δ͜ͱ͕Ͱ͖Δ • ಠཱͨ͠ॲཧ͕શͯऴΘͬͨޙɺͦΕͧΕΛ·ͱΊͯҰͭͷ݁ՌΛಘΔ

  122. ฒߦԽ͢Δ͜ͱ͕Ͱ͖ΔλεΫͱ͸ • ޓ͍ʹಠཱͨ͠ॲཧ͸ฒߦԽ͢Δ͜ͱ͕Ͱ͖Δ • ಠཱͨ͠ॲཧ͕શͯऴΘͬͨޙɺͦΕͧΕΛ·ͱΊͯҰͭͷ݁ՌΛಘΔ ΫΠοΫιʔτͷத͔Βಠཱͨ͠ ॲཧΛݟ͚ͭΑ͏

  123. 59 99 75 92 15 13 2 31 35 68

    83 67 51 70 77 a 0 14 1 2 3 4 5 6 7 8 9 10 11 12 13 15 2 13 31 35 51 59 99 35 75 83 67 51 70 77 a 0 14 1 2 3 4 5 6 7 8 9 10 11 12 13 2 15 13 31 35 51 a 0 1 2 3 4 5 68 75 67 70 92 83 77 99 14 7 8 9 10 11 12 13 a 2 13 a 0 1 31 35 51 3 4 5 35 51 4 5 51 5 13 1 68 67 70 7 8 9 68 70 8 9 70 9 83 77 92 99 14 11 12 13 99 14 77 83 11 12 83 12
  124. 59 99 75 92 15 13 2 31 35 68

    83 67 51 70 77 a 0 14 1 2 3 4 5 6 7 8 9 10 11 12 13 15 2 13 31 35 51 59 99 35 75 83 67 51 70 77 a 0 14 1 2 3 4 5 6 7 8 9 10 11 12 13 2 15 13 31 35 51 a 0 1 2 3 4 5 68 75 67 70 92 83 77 99 14 7 8 9 10 11 12 13 a 2 13 a 0 1 31 35 51 3 4 5 35 51 4 5 51 5 13 1 68 67 70 7 8 9 68 70 8 9 70 9 83 77 92 99 14 11 12 13 99 14 77 83 11 12 83 12
  125. 15 2 13 31 35 51 99 35 75 83

    67 51 70 77 a 0 14 1 2 3 4 5 7 8 9 10 11 12 13 2 15 13 31 35 51 a 0 1 2 3 4 5 68 75 67 70 92 83 77 99 14 7 8 9 10 11 12 13 a 2 13 a 0 1 31 35 51 3 4 5 35 51 4 5 51 5 13 1 68 67 70 7 8 9 68 70 8 9 70 9 83 77 92 99 14 11 12 13 99 14 77 83 11 12 83 12 2ͭͷಠཱͨ͠ΫΠοΫιʔτ
  126. 2 15 13 31 35 51 a 0 1 2

    3 4 5 68 75 67 70 92 83 77 99 14 7 8 9 10 11 12 13 a 2 13 a 0 1 31 35 51 3 4 5 35 51 4 5 51 5 13 1 68 67 70 7 8 9 68 70 8 9 70 9 83 77 92 99 14 11 12 13 99 14 77 83 11 12 83 12 4ͭͷಠཱͨ͠ΫΠοΫιʔτ
  127. ಠཱͨ͠λεΫ͸ฒߦʹॲཧͰ͖Δʂ

  128. SwiftͱGrand Central DispatchͰ ࣮૷ͯ͠ΈΔ

  129. ௚ྻॲཧ൛ͷΫΠοΫιʔτ ͭ·ΓɺฒߦԽ͢ΔલͷΫΠοΫιʔτ

  130. ௚ྻॲཧ൛ͷΫΠοΫιʔτ

  131. Array͕ࢀর͍ͯ͠Δ όοϑΝʔʹ௚઀ΞΫηε ௚ྻॲཧ൛ͷΫΠοΫιʔτ

  132. ௚ྻॲཧ൛ͷΫΠοΫιʔτ

  133. ௚ྻॲཧ൛ͷΫΠοΫιʔτ

  134. ௚ྻॲཧ൛ͷΫΠοΫιʔτ

  135. ύʔςΟγϣϯॲཧ ௚ྻॲཧ൛ͷΫΠοΫιʔτ

  136. ഑ྻͷϐϘοτҎԼͷ෦෼ ௚ྻॲཧ൛ͷΫΠοΫιʔτ

  137. ഑ྻͷϐϘοτΑΓେ͖͍෦෼ ௚ྻॲཧ൛ͷΫΠοΫιʔτ

  138. ௚ྻॲཧ൛ͷΫΠοΫιʔτ

  139. ͜͜ΛฒߦԽ͢Ε͹ྑ͍͸ͣʂ ௚ྻॲཧ൛ͷΫΠοΫιʔτ

  140. DispatchQueueΛ࢖ͬͯฒߦԽ

  141. DispatchQueueΛ࢖ͬͯฒߦԽ

  142. DispatchQueueΛ࢖ͬͯฒߦԽ

  143. DispatchQueueΛ࢖ͬͯฒߦԽ

  144. DispatchQueueΛ࢖ͬͯฒߦԽ

  145. DispatchQueueΛ࢖ͬͯฒߦԽ

  146. DispatchQueueΛ࢖ͬͯฒߦԽ `async` ͭ·ΓɺඇಉظॲཧͳͷͰɺ ιʔτॲཧ͕ऴΘΔલʹؔ਺Λൈ͚ͯ͠·͏ɻ

  147. DispatchQueueΛ࢖ͬͯฒߦԽ

  148. DispatchQueueΛ࢖ͬͯฒߦԽ

  149. DispatchQueueΛ࢖ͬͯฒߦԽ ཪͷεϨουͰιʔτ͕ߦΘΕ͍ͯΔ ࠷தʹΞΫηε

  150. DispatchQueueΛ࢖ͬͯฒߦԽ ☠

  151. ඇಉظॲཧ͕׬ྃ͢Δ·Ͱ଴ͭඞཁ͕͋Δ

  152. ඇಉظॲཧ͕׬ྃ͢Δ·Ͱ଴ͭඞཁ͕͋Δ • DispatchGroup͕࢖͑Δɻ • DispatchGroupΛDispatchQueue.async ͷҾ਺ʹ౉͢ͱɺඇಉظॲཧ͕׬ྃ͢ Δ·ͰεϨουΛϒϩοΫͯ͘͠ΕΔɻ

  153. DispatchGroupͰඇಉظॲཧͷ׬ྃΛ଴ͭ

  154. DispatchGroupͰඇಉظॲཧͷ׬ྃΛ଴ͭ

  155. DispatchGroupͰඇಉظॲཧͷ׬ྃΛ଴ͭ

  156. DispatchGroupͰඇಉظॲཧͷ׬ྃΛ଴ͭ

  157. DispatchGroupͰඇಉظॲཧͷ׬ྃΛ଴ͭ શͯͷඇಉظॲཧ͕ऴΘΔ·ͰϒϩοΫ

  158. DispatchGroupͰඇಉظॲཧͷ׬ྃΛ଴ͭ

  159. ϕϯνϚʔΫ • 1ԯݸͷϥϯμϜͳ੔਺ͷ഑ྻΛιʔτ

  160. ϕϯνϚʔΫ • 1ԯݸͷϥϯμϜͳ੔਺ͷ഑ྻΛιʔτ ඪ४ϥΠϒϥϦ
 Array.sort() ௚ྻ൛ ΫΠοΫιʔτ ฒߦ൛ ΫΠοΫιʔτ 10.3

    seconds 9.0 seconds 8.1 seconds - 5ճଌఆͨ͠ฏۉ஋Ͱ͢ɻ - εϥΠυʹ৐ͤͨίʔυ͸આ໌༻Ͱ͢ɻ࣮ࡍʹଌఆʹར༻ͨ͠ίʔυ͸Githubʹ͋Γ·͢ɻ - MacBook Pro 15-inch, 2018, Core i9 2.9 GHz. - macOS Catalina 10.15.6, Swift 5.2.4, ࠷దԽ͋Γ ( -O )
  161. ࣌ؒ 0 sec 2 sec 4 sec 6 sec 8

    sec 10 sec 12 sec Array.sort() ௚ྻΫΠοΫιʔτ ฒߦΫΠοΫιʔτ
  162. ࣌ؒ 0 sec 2 sec 4 sec 6 sec 8

    sec 10 sec 12 sec Array.sort() ௚ྻΫΠοΫιʔτ ฒߦΫΠοΫιʔτ ໿10ඵ ໿9ඵ ໿8ඵ
  163. None
  164. ͋Μ·Γ଎͘ͳ͍ɻ

  165. ͳͥ଎͘ͳ͍ͷ͔

  166. ͳͥ଎͘ͳ͍ͷ͔

  167. ͳͥ଎͘ͳ͍ͷ͔ ๲େͳ਺ͷΫϩʔδϟΛ enqueue ͍ͯ͠Δ

  168. Dispatch Queue

  169. Dispatch Queue { quickSort() quickSort() } { quickSort() quickSort() }

    { quickSort() quickSort() }
  170. Dispatch Queue { quickSort() quickSort() } { quickSort() quickSort() }

    { quickSort() quickSort() } CPU ίΞ CPU ίΞ CPU ίΞ
  171. Dispatch Queue { quickSort() quickSort() } { quickSort() quickSort() }

    { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } CPU ίΞ CPU ίΞ CPU ίΞ
  172. Dispatch Queue { quickSort() quickSort() } { quickSort() quickSort() }

    { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } CPU ίΞ CPU ίΞ CPU ίΞ ίΞͷୣ͍߹͍
  173. Dispatch Queue { quickSort() quickSort() } { quickSort() quickSort() }

    { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } CPU ίΞ CPU ίΞ CPU ίΞ ίΞͷୣ͍߹͍ Φʔόʔϔου
  174. SemaphoreΛ࢖ͬͯϦιʔεΛ؅ཧ

  175. SemaphoreΛ࢖ͬͯϦιʔεΛ؅ཧ • Semaphore͸தʹIntܕͷ਺஋Λ͍࣋ͬͯͯɺϦιʔεͷར༻Մ൱Λ؅ཧ • ࠓճ͸CPUͷίΞΛϦιʔεͱߟ͑Δ • `signal()` ͰϦιʔε(ίΞ)Λղ์͢Δ • `wait()`ͰϦιʔε(ίΞ)Λ࢖༻͢Δ

  176. 8

  177. 8

  178. ࢖༻ՄೳͳϦιʔεͷॳظ஋ 8

  179. Ϧιʔε(ίΞ)͕࢖༻Մೳ͔νΣοΫ 8

  180. ར༻ՄೳͳΒenqueue 8

  181. ΫϩʔδϟΛൈ͚Δͱ͖ʹ SignalΛૹͬͯར༻׬ྃΛ௨஌ 8

  182. ར༻ෆՄͳΒಉ͡εϨουͰॲཧΛܧଓ 8

  183. ݁Ռ • 1ԯݸͷϥϯμϜͳ੔਺ͷ഑ྻΛιʔτ ඪ४ϥΠϒϥϦ
 Array.sort() ௚ྻ൛ ΫΠοΫιʔτ ฒߦ൛ ΫΠοΫιʔτ ฒߦ൛

    ΫΠοΫιʔτ
 with Semaphore 10.3 seconds 9.0 seconds 8.1 seconds 4.5 seconds - 5ճଌఆͨ͠ฏۉ஋Ͱ͢ɻ - εϥΠυʹ৐ͤͨίʔυ͸આ໌༻Ͱ͢ɻ࣮ࡍʹଌఆʹར༻ͨ͠ίʔυ͸Githubʹ͋Γ·͢ɻ - MacBook Pro 15-inch, 2018, Core i9 2.9 GHz. - macOS Catalina 10.15.6, Swift 5.2.4, ࠷దԽ͋Γ ( -O )
  184. ࣌ؒ 0 sec 2 sec 4 sec 6 sec 8

    sec 10 sec 12 sec Array.sort() ௚ྻΫΠοΫιʔτ ฒߦΫΠοΫιʔτ ฒߦΫΠοΫιʔτվ ໿10ඵ ໿9ඵ ໿8ඵ ໿4.5ඵ
  185. 2ഒҎ্ͷߴ଎Խ Swiftͷඪ४ϥΠϒϥϦͱൺֱ

  186. 2ഒҎ্ͷߴ଎Խʂ

  187. 2ഒҎ্ͷߴ଎Խʂ • SemaphoreͰϦιʔε(ίΞ)ͷར༻Λ੍ޚ͢Δ͜ͱʹΑͬͯΦʔόʔϔουΛ ݮΒͨ͠ɻ

  188. 2ഒҎ্ͷߴ଎Խʂ • SemaphoreͰϦιʔε(ίΞ)ͷར༻Λ੍ޚ͢Δ͜ͱʹΑͬͯΦʔόʔϔουΛ ݮΒͨ͠ɻ • Semaphoreͷॳظ஋ -> ར༻Ͱ͖ΔϦιʔεͷ਺

  189. 8

  190. 8 ηϚϑΥͷॳظ஋: ಉ࣌ʹར༻͞ΕΔίΞͷ਺

  191. • SemaphoreͷΠχγϟϥΠβͷҾ਺͕ɺಉ࣌ʹར༻͞ΕΔίΞͷ਺

  192. • SemaphoreͷΠχγϟϥΠβͷҾ਺͕ɺಉ࣌ʹར༻͞ΕΔίΞͷ਺ • ಉ࣌ʹར༻͞ΕΔίΞͷ਺Λม͑ͨΒɺॲཧͷ଎͕͞มΘΔ͸ͣ

  193. Semaphoreͷॳظ஋Ͱ଎͞Λൺֱ 6ίΞ12εϨουͷMacBook ProͰܭଌ

  194. Semaphoreͷॳظ஋Ͱ଎͞Λൺֱ 6ίΞ12εϨουͷMacBook ProͰܭଌ 0 2.5 5 7.5 10 12.5 15

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 - 5ճଌఆͨ͠ฏۉ஋Ͱ͢ɻ - 1ԯݸͷϥϯμϜͳ੔਺Λιʔτɻ - εϥΠυʹ৐ͤͨίʔυ͸આ໌༻Ͱ͢ɻ࣮ࡍʹଌఆʹར༻ͨ͠ίʔυ͸Githubʹ͋Γ·͢ɻ - MacBook Pro 15-inch, 2018, Core i9 2.9 GHz. - macOS Catalina 10.15.6, Swift 5.2.4, ࠷దԽ͋Γ ( -O )
  195. Semaphoreͷॳظ஋Ͱ଎͞Λൺֱ 6ίΞ12εϨουͷMacBook ProͰܭଌ 0 2.5 5 7.5 10 12.5 15

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 - 5ճଌఆͨ͠ฏۉ஋Ͱ͢ɻ - 1ԯݸͷϥϯμϜͳ੔਺Λιʔτɻ - εϥΠυʹ৐ͤͨίʔυ͸આ໌༻Ͱ͢ɻ࣮ࡍʹଌఆʹར༻ͨ͠ίʔυ͸Githubʹ͋Γ·͢ɻ - MacBook Pro 15-inch, 2018, Core i9 2.9 GHz. - macOS Catalina 10.15.6, Swift 5.2.4, ࠷దԽ͋Γ ( -O ) ཧ૝
  196. 100%ཧ૝௨Γʹ͸͍͔ͳ͍

  197. 100%ཧ૝௨Γʹ͸͍͔ͳ͍ • ར༻͢ΔίΞ͕2ഒʹͳΕ͹ɺ଎͞΋2ഒʹͳΔͷ͕100%ͷཧ૝ • ͦͷͨΊʹ͸ΦʔόʔϔουΛθϩʹ͠ͳ͍ͱ͍͚ͳ͍

  198. 100%ཧ૝௨Γʹ͸͍͔ͳ͍ • ར༻͢ΔίΞ͕2ഒʹͳΕ͹ɺ଎͞΋2ഒʹͳΔͷ͕100%ͷཧ૝ • ͦͷͨΊʹ͸ΦʔόʔϔουΛηϩʹ͠ͳ͍ͱ͍͚ͳ͍ • ΦʔόʔϔουΛ׬શʹθϩʹ͢Δͷ͸ݱ࣮తʹ͸ແཧ

  199. 100%ཧ૝௨Γʹ͸͍͔ͳ͍ • ར༻͢ΔίΞ͕2ഒʹͳΕ͹ɺ଎͞΋2ഒʹͳΔͷ͕100%ͷཧ૝ • ͦͷͨΊʹ͸ΦʔόʔϔουΛηϩʹ͠ͳ͍ͱ͍͚ͳ͍ • ΦʔόʔϔουΛ׬શʹθϩʹ͢Δͷ͸ݱ࣮తʹ͸ແཧ • ཧ૝௨Γʹ͸͍͔ͳ͍

  200. ΞδΣϯμ • ࠷ۙͷiOSσόΠεͱϚϧνίΞԽʹؔ͢Δ՝୊ • for loopΛฒߦԽ͢Δ • ΫΠοΫιʔτΛฒߦԽ͢Δ • ͍Ζ͍ΖͳΦʔόʔϔου

  201. ͦ΋ͦ΋Φʔόʔϔουͱ͸

  202. ͦ΋ͦ΋Φʔόʔϔουͱ͸ • ΋ͱ΋ͱͷ௚ྻΞϧΰϦζϜʹ͸ͳ͔ͬͨ௥Ճͷॲཧɺ͓ΑͼͦΕʹ͔͔Δ ࣌ؒɻ

  203. ͦ΋ͦ΋Φʔόʔϔουͱ͸ • ΋ͱ΋ͱͷ௚ྻΞϧΰϦζϜʹ͸ͳ͔ͬͨ௥Ճͷॲཧɺ͓ΑͼͦΕʹ͔͔Δ ࣌ؒɻ • ෳ਺εϨου͔Βͷॻ͖ࠐΈͷอޢɺGCDϝιουͷݺͼग़͠ͳͲɺฒߦԽ ͷͨΊʹඞཁͳॲཧʹ͔͔Δ࣌ؒશͯ

  204. ͦ΋ͦ΋Φʔόʔϔουͱ͸ • ΋ͱ΋ͱͷ௚ྻΞϧΰϦζϜʹ͸ͳ͔ͬͨ௥Ճͷॲཧɺ͓ΑͼͦΕʹ͔͔Δ ࣌ؒɻ • ෳ਺εϨου͔Βͷॻ͖ࠐΈͷอޢɺGCDϝιουͷݺͼग़͠ͳͲɺฒߦԽ ͷͨΊʹඞཁͳॲཧʹ͔͔Δ࣌ؒશͯ • ͦͷଞɺؾ෇͖ʹ͍͘΋ͷͱͯ͠False Sharingͱݺ͹ΕΔ΋ͷ΋͋Δ

  205. False Sharing

  206. None
  207. None
  208. None
  209. ผͷεϨουͰ sum() ͱ inc() ΛݺͿ

  210. 0.002 ඵ

  211. 0.002 ඵ

  212. 0.002 ඵ

  213. 0.002 ඵ 0.2 ඵ

  214. 0.002 ඵ 0.2 ඵ ໿100ഒ

  215. 0.002 ඵ 0.2 ඵ ໿100ഒ - 5ճଌఆͨ͠ฏۉ஋Ͱ͢ɻ - εϥΠυʹ৐ͤͨίʔυ͸આ໌༻Ͱ͢ɻ࣮ࡍʹଌఆʹར༻ ͨ͠ίʔυ͸Githubʹ͋Γ·͢ɻ

    - MacBook Pro 15-inch, 2018, Core i9 2.9 GHz. - macOS Catalina 10.15.6, Swift 5.2.4, ࠷దԽ͋Γ ( -O )
  216. Core 0 Core 1

  217. Core 0 Core 1 sum() inc()

  218. x y ϝΠϯϝϞϦ Core 0 Core 1 sum() inc()

  219. x y ϝΠϯϝϞϦ Core 0 Core 1 sum() inc() Cache

    Cache x y
  220. x y ϝΠϯϝϞϦ Core 0 Core 1 sum() inc() Cache

    Cache x y ֤ίΞʹΩϟογϡ͕ උΘ͍ͬͯΔ
  221. x y ϝΠϯϝϞϦ Core 0 Core 1 sum() inc() Cache

    Cache x y
  222. x y ϝΠϯϝϞϦ Core 0 Core 1 sum() inc() Cache

    Cache x y ͦΕͧΕͷίΞͷ ΩϟογϡʹΞΫηε
  223. x y ϝΠϯϝϞϦ Core 0 Core 1 sum() inc()

  224. x y ϝΠϯϝϞϦ Core 0 Core 1 sum() inc() ϝϞϦ্ͰྡΓ߹ͬͨ

    Ґஔʹ͋Δ
  225. x y ϝΠϯϝϞϦ Core 0 Core 1 sum() inc() Cache

    Cache x y x y
  226. x y ϝΠϯϝϞϦ Core 0 Core 1 sum() inc() Cache

    Cache x y x y
  227. x y ϝΠϯϝϞϦ Core 0 Core 1 sum() inc() Cache

    Cache x y x y Yʹରͯ͠ॻ͖ࠐΈ
  228. x y ϝΠϯϝϞϦ Core 0 Core 1 sum() inc() Cache

    Cache x y x y'
  229. x y' ϝΠϯϝϞϦ Core 0 Core 1 sum() inc() Cache

    Cache x y' x y' ͪ͜ΒͷΩϟογϡ΋ ߋ৽ͯ͠͠·͏
  230. x y ϝΠϯϝϞϦ Core 0 Core 1 sum() inc() Cache

    Cache x y' x y' Φʔόʔϔου ͪ͜ΒͷΩϟογϡ΋ ߋ৽ͯ͠͠·͏
  231. False Sharing ೔ຊޠͰ͸ِڞ༗(͖͗ΐ͏Ώ͏)͋Δ͍͸ෆਖ਼ڞ༗

  232. False Sharing ೔ຊޠͰ͸ِڞ༗(͖͗ΐ͏Ώ͏)͋Δ͍͸ෆਖ਼ڞ༗ • xͱy͸ϓϩάϥϜͷҙຯ্͸ແؔ܎ͷ͸ͣ • ͔͠͠ɺxͱy͸ϋʔυ΢ΣΞతʹ͸ಉ͡ΩϟογϡϥΠϯ্ʹଘࡏ͢Δ • ϋʔυ΢ΣΞ͸ΩϟογϡϥΠϯΛ୯ҐʹΩϟογϡͷߋ৽Λߦ͏ͨΊɺ
 yͷߋ৽͝ͱʹଞͷίΞͷΩϟογϡ΋ߋ৽͠ͳ͚Ε͹ͳΒͳ͍

  233. False Sharing ೔ຊޠͰ͸ِڞ༗(͖͗ΐ͏Ώ͏)͋Δ͍͸ෆਖ਼ڞ༗ • xͱy͸ϓϩάϥϜͷҙຯ্͸ແؔ܎ͷ͸ͣ • ͔͠͠ɺxͱy͸ϋʔυ΢ΣΞతʹ͸ಉ͡ΩϟογϡϥΠϯ্ʹଘࡏ͢Δ • ϋʔυ΢ΣΞ͸ΩϟογϡϥΠϯΛ୯ҐʹΩϟογϡͷߋ৽Λߦ͏ͨΊɺ
 yͷߋ৽͝ͱʹଞͷίΞͷΩϟογϡ΋ߋ৽͠ͳ͚Ε͹ͳΒͳ͍

    Φʔόʔϔου
  234. ࠷ޙʹ

  235. ฒߦΞϧΰϦζϜͱͷ෇͖߹͍ํ • ͍͖ͳΓฒߦΞϧΰϦζϜ͔Β࢝ΊΔͷ͸͓͢͢ΊͰ͖ͳ͍ɻ • ·ͣ͸௚ྻͷΞϧΰϦζϜ͔ΒॳΊͯɺύϑΥʔϚϯεʹ໰୊͕͋Ε͹ฒߦΞ ϧΰϦζϜΛݕ౼͢Δͷ͕ྑ͍ɻ • ฒߦΞϧΰϦζϜ͸ɺਖ਼௚ʹݴͬͯ؆୯Ͱ͸ͳ͍ͷͰɺ͍͖ͳΓ࣮૷Λ࢝ΊΔ લʹઐ໳ͷॻ੶ΛಡΜͰΈΔͷ͕͓͢͢ΊͰ͢ɻ

  236. Acknowledgements ࢀߟࢿྉ

  237. Acknowledgements ฒߦΞϧΰϦζϜʹ͍ͭͯͷࢀߟॻ੶ • The Art of Concurrency • ࿨༁: ฒߦίϯϐϡʔςΟϯάٕ๏

  238. Acknowledgements Concurrent Mapʹ͍ͭͯ • objc.io, Concurrent Map • https://talk.objc.io/episodes/S01E90-concurrent-map

  239. Acknowledgements False Sharingʹ͍ͭͯ • https://www.isus.jp/products/vtune/pu38-02-detecting-and-mitigating-false- sharing/

  240. ·ͱΊ • ΞϧΰϦζϜͷத͔ΒฒߦͰॲཧ͢Δ͜ͱ͕Ͱ͖ΔλεΫΛݟ͚ͭΔ • ฒߦԽʹͱ΋ͳ͏ΦʔόʔϔουΛθϩʹ͢Δͷ͸ݱ࣮తʹ͸೉͍͠ • ฒߦԽͷԸܙ͕ΦʔόʔϔουʹΑΔϖφϧςΟΛ্ճΕ͹ྑ͍