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

GoとRust - 並行処理編

3f0a6648237e57a670f76b50d0b72bf1?s=47 nasa
April 27, 2022

GoとRust - 並行処理編

GoとRustの変更処理を比較することで両者を深く理解する試みです。

詳しい内容やスライドでは省いたところをブログに書きました
https://zenn.dev/nasa/articles/compare_rust_go_concurrency

3f0a6648237e57a670f76b50d0b72bf1?s=128

nasa

April 27, 2022
Tweet

More Decks by nasa

Other Decks in Technology

Transcript

  1. ©2022 Wantedly, Inc. GoͱRust - ฒߦॲཧฤ \ඇެࣜ/ Go Conferenceޙ໷ࡇ 2022/4/27

    nasa (Asan Kondo)
  2. ©2022 Wantedly, Inc. ࣗݾ঺հ • nasa Ͱ͢ • Backend engineer

    / Wantedly, in c • ਪનج൫ͷӡ༻։ൃ • DSͷੜ࢈ੑര্͛ • কདྷͷເ͸nasaͱ͍͏ϋϯυϧωʔϜΛ΋͗औΔ͜ͱ • GitHub: k-nas a • Twitter: nasa_desu • Zenn: nasa 🎉 🎉 🎉 🎉 🎉
  3. ©2022 Wantedly, Inc. ࿩͢͜ͱ • goroutineͱ͸ͳʹ͔ • Goͷฒߦॲཧͷ࢓૊Έ • goroutine(Go)ͱasync/await(Rust)ͷൺֱ

    • GoͷϥϯλΠϜɺRustͷϥϯλΠϜͷ࿩
  4. ©2022 Wantedly, Inc. ࿩͞ͳ͍͜ͱ • Ͳ͕ͬͪ༏Ε͍ͯΔͱ͍͏࿩Λ͢Δͭ΋Γ͸ͳ͍ • ߏจͷҧ͍ʹ͍ͭͯ͸ಛʹ৮Εͳ͍ • ϕʔεʹͳ͍ͬͯΔࢥ૝ʹ͍ͭͯ͸ಛʹ৮Εͳ͍

  5. ©2022 Wantedly, Inc. ໨࣍ 1. ͦΕͧΕͷฒߦϓϩάϥϛϯά 2. ͦΕͧΕͷλεΫ 3. ϥϯλΠϜ

    4. εέδϡʔϦϯά
  6. ©2021 Wantedly, Inc. ͦΕͧΕͷฒߦϓϩάϥϛϯά

  7. ©2022 Wantedly, Inc. • goΩʔϫʔυͰΒ͘Β͘ • ϥϯλΠϜ͕͍͍ײ͡ʹޮ཰త ʹॲཧͯ͘͠ΕΔ ͦΕͧΕͷฒߦϓϩάϥϛϯά (Pͷฒߦϓϩάϥϛϯά

  8. ©2022 Wantedly, Inc. • RustͰ͸ϥϯλΠϜ͕૊Έ ࠐΈͰೖͬͯͳ͍ͷͰϥϯ λΠϜϥΠϒϥϦΛ࢖͏ඞ ཁ͕͋Δ • ຊൃදͰ͸tokioͱ͍͏ϥΠ

    ϒϥϦΛ࢖͏ ͦΕͧΕͷฒߦϓϩάϥϛϯά 3VTUͷฒߦϓϩάϥϛϯά
  9. ©2022 Wantedly, Inc. • ͳΜ͔mainͷલʹมͳͷ͍ͭͯΔ • tokio::spawn?? ͦΕͧΕͷฒߦϓϩάϥϛϯά 3VTUͷฒߦϓϩάϥϛϯά

  10. ©2022 Wantedly, Inc. mainલͷσΟϨΫςΟϒ͸ϥϯλ ΠϜΛىಈ͢Δ + mainΛasyncؔ ਺ʹग़དྷΔ͓·͡ͳ͍ ͦΕͧΕͷฒߦϓϩάϥϛϯά 3VTUͷฒߦϓϩάϥϛϯά

  11. ©2022 Wantedly, Inc. tokio::spawn͸goΩʔϫʔυͱ ಉ͘͡λεΫΛىಈ͢Δؔ਺ ͦΕͧΕͷฒߦϓϩάϥϛϯά 3VTUͷฒߦϓϩάϥϛϯά

  12. ©2022 Wantedly, Inc. • ඇಉظؔ਺Λఆٛ͢Δ͔൱͔ • ϥϯλΠϜ͕ඪ४Ͱ͋Δ͔ • ޙʹݟΔҧ͍͸ඪ४͔൱͔ ʹΑΔ΋ͷͩͱࢥ͍ͬͯΔ

    ͦΕͧΕͷฒߦϓϩάϥϛϯά (Pͱ3VTUൺֱ
  13. ©2021 Wantedly, Inc. λεΫʹ͍ͭͯ ͦΕͧΕ͕ੜ੒ͨ͠λεΫʹ͍ͭͯݟ͍ͯ͘

  14. ©2022 Wantedly, Inc. • ͦΕͧΕ͕ੜ੒ͨ͠φχΧΛλε Ϋͱݺͼ·͢ • ͜ΕΒʹ͍ͭͯݟ͍͖ͯ·͠ΐ͏ λεΫʹ͍ͭͯ

  15. ©2022 Wantedly, Inc. • GoͰىಈͨ͠λεΫΛgrouting λεΫʹ͍ͭͯ • Tokio::spawnͰىಈͨ͠λεΫΛ ඇಉظλεΫ

  16. ©2021 Wantedly, Inc. ·ͣHPSPVUJOFʹ͍ͭͯݟ͍͖ͯ·͠ΐ͏

  17. ©2022 Wantedly, Inc. HPSPVUJOFͱ͸ ίϧʔνϯͷந৅Խ

  18. ©2022 Wantedly, Inc. HPSPVUJOFͱ͸ 🤔🤔🤔🤔

  19. ©2022 Wantedly, Inc. • ίϧʔνϯͷงғؾΛ௫Ή • ؔ਺͸ݺͼग़ͨ͠Β࠷ޙ·Ͱ࣮ߦ • ίϧʔνϯ͸ॲཧΛதஅग़དྷΔɺ ్ͦͯ͠த͔Β࠶։Ͱ͖Δ

    ίϧʔνϯͱ͸
  20. ©2022 Wantedly, Inc. • goroutine΋్தͰதஅͱ࠶։Λ܁ Γฦ͍ͯ͠Δ • ͜ΕΒͷ؅ཧ͸GoϥϯλΠϜ͕ ΍͍ͬͯΔ ίϧʔνϯͱ͸

  21. ©2022 Wantedly, Inc. • Ͳ͜Ͱதஅɾ࠶։͢Δͷ͔? HSPVUJOH ࠶։தஅͷ࿩

  22. ©2022 Wantedly, Inc. • ๻Β͸࠶։ɾதஅϙΠϯτ͸໌͍ࣔͯ͠ͳ͍ HSPVUJOH ࠶։தஅͷ࿩

  23. ©2022 Wantedly, Inc. • զʑϓϩάϥϚʔ͕໌ࣔͤͣͱ΋ϥϯλΠϜ͕ӨͰ͏·͍͜ ͱ΍ͬͯ͘Ε͍ͯΔ • ͏Ζ֮͑Ͱ͕͢Լهͷ࣌ʹ੾ΓସΘΔ͸ͣ • time.Slee

    p • I O • Memory allocation HSPVUJOH ࠶։தஅͷ࿩
  24. ©2022 Wantedly, Inc. HSPVUJOH ࠶։தஅͷ࿩

  25. ©2021 Wantedly, Inc. ඇಉظλεΫʹ͍ͭͯݟ͍͖ͯ·͠ΐ͏

  26. ©2022 Wantedly, Inc. • Ͳ͜Ͱதஅɾ࠶։͢Δͷ͔ ? • awaitͱॻ͍ͨͱ͖ • ϓϩάϥϚʔ͕໌ه͢Δ

    ඇಉظλεΫ ࠶։தஅͷ࿩
  27. ©2022 Wantedly, Inc. • ϒϩοΩϯάॲཧΛࠞೖͤ͞Δͱ ଞͷλεΫʹ੾ΓସΘΒͣ1ͭͷλ εΫ͕ઐ༗ͯ͠͠·͏ • ৻ॏ͕͞ඞཁ ඇಉظλεΫ

    ࠶։தஅͷ࿩
  28. ©2022 Wantedly, Inc. • ੑೳͷ੹೚͸ϥϯλΠϜ͕͔࣋ͭϓϩά ϥϚʔ͕͔࣋ͭ • Go͸ϓϩάϥϚʔϑϨϯυϦʔײ͕͋Δ • ҰํͰΦʔόʔϔου͕͋Δ(Β͍͠

    ) λεΫʹ͍ͭͯ
  29. ©2021 Wantedly, Inc. ϓϦΤϯϓςΟϒɾϊϯϓϦΤϯϓςΟϒ

  30. ©2022 Wantedly, Inc. w ·Ͱ (PͰ΋·Ͱ͸ແݶϧʔϓͳͲ͕͋ͬͨ৔߹ʹ ଞͷHPSPVUJOF͕࣮ߦ͞Εͳ͍ͱ͍͏໰୊͕͋ͬ ͨΑ͏ ϓϦΤϯϓςΟϒɾϊϯϓϦΤϯϓςΟϒ

  31. ©2022 Wantedly, Inc. w ͔Β λεΫ͕͠͹Β࣮͘ߦ͞Ε͍ͯΔ͜ͱΛݕ஌ͯ͠ϥ ϯλΠϜʹ໭ΔίʔυʯΛૠೖ͢Δ༻ʹͳͬͨΑ͏ Ͱ͢ɻศརʂ ϓϦΤϯϓςΟϒɾϊϯϓϦΤϯϓςΟϒ

  32. ©2022 Wantedly, Inc. w (PͷΑ͏ʹϥϯλΠϜ͕λεΫͷ࣮ߦΛதஅͯ͠ଞͷλεΫͷ࣮ߦʹҠΔΑ ͏ͳϚϧνλεΫํ๏ΛϓϦΤϯϓςΟϒϚϧνλεΫ w 3VTU ͱ͍͏͔BTZODBXBJUʁ ͷΑ͏ʹλεΫ͕ࣗൃతʹ࣮ߦݖΛ໌͚౉͢

    Α͏ͳϚϧνλεΫํ๏ΛϊϯϓϦΤϯϓςΟϒϚϧνλεΫ·ͨ͸ɺڠௐత ϚϧνλεΫ ϓϦΤϯϓςΟϒɾϊϯϓϦΤϯϓςΟϒ
  33. ©2022 Wantedly, Inc. w (P w ϓϦΤϯϓςΟϒϚϧνλεΫ w ࠶։ɾఀࢭϙΠϯτ͸ϥϯλΠϜ͕͍͍ײ͡ʹؤுΔ w

    3VTU w ϊϯϓϦΤϯϓςΟϒϚϧνλεΫ w ࠶։ఀࢭ͸ϓϩάϥϚʔ͕࣮૷ ϓϦΤϯϓςΟϒɾϊϯϓϦΤϯϓςΟϒ
  34. ©2021 Wantedly, Inc. λεΫεέδϡʔϦϯά

  35. ©2022 Wantedly, Inc. λεΫεέδϡʔϦϯά ͋·Γҧ͍͕ͳ͍ͷͰ(PϥϯλΠ ϜΛݟ͍ͯ͘ ͜͜Ͱઆ໌͍ͯ͠ΔϥϯλΠϜͷಈ࡞͸ݹ͍΋ͷ͔΋͠Ε·ͤΜ

  36. ©2022 Wantedly, Inc. λεΫεέδϡʔϦϯά w λεΫ͸ʹ.ݸͷ04εϨου্ʹ ߏங͞Εͨ/ݸͷάϦʔϯεϨου ্Ͱ࣮ߦ͞Ε·͢ɻ w ./Ϟσϧͱݺ͹ΕΔ΋ͷͰ͢

    ./Ϟσϧ
  37. ©2022 Wantedly, Inc. λεΫεέδϡʔϦϯά ϫʔΫεςΟʔϦϯάεέδϡʔϦϯά w HMPCBMUBTLRVFVFͦͷ໊ͷ௨Γάϩʔ όϧͳλεΫΩϡʔ w MPDBMUBTLRVFVFPTεϨουʹ͖ͭͭ

    ͷϩʔΧϧλεΫΩϡʔ͕͋Δ w QSPDFTTPS$16Λந৅Խͨ͠΋ͷɻλε Ϋͷ࣮ߦ͸͍͕ͭ͜ߦ͍ͬͯΔ w 04UISFBE
  38. ©2022 Wantedly, Inc. λεΫεέδϡʔϦϯά ϫʔΫεςΟʔϦϯάεέδϡʔϦϯά w λεΫΛফԽͨ͠ͱ͖ͷಈ͖

  39. ©2022 Wantedly, Inc. λεΫεέδϡʔϦϯά ϫʔΫεςΟʔϦϯά w λεΫͷ୳ࡧ͕࢝ΊΔ w ୳ࡧॱং͸ϥϯλΠϜʹΑͬͯҧ͏͔΋

  40. ©2022 Wantedly, Inc. λεΫεέδϡʔϦϯά ଞͷQSPDFTTPS͔Β౪Ή w ଞͷQSPDFTTPSͷϩʔΧϧΩϡʔ͔Β λεΫΛ౪Ή͜ͱ͔Β͜ͷΞϧΰϦζ Ϝ͸ϫʔΫεςΟʔϦϯάͱݺ͹Εͯ Δ

    w ͜ΕʹΑΓɺͭͷQSPDFTTPSʹλε Ϋ͕ूதͨ͠ͱͯ͠΋ଞͷQSPDFTTPS ʹॲཧΛ෼ࢄ͢Δ͜ͱ͕ग़དྷɺޮ཰త ʹλεΫΛॲཧ͍ͯ͘͜͠ͱ͕Ͱ͖Δ
  41. ©2021 Wantedly, Inc. νϟΠϧυεςΟʔϦϯάɾܧଓεςΟʔϦϯά

  42. ©2022 Wantedly, Inc. νϟΠϧυεςΟʔϦϯάɾܧଓεςΟʔϦϯά w ͜ͷΑ͏ͳΠϝʔδͷํ͕ଟ͍ͱ༧૝͍ͯ͠·͢

  43. ©2021 Wantedly, Inc. Goͩͱ͜Ε͸ؒҧ͍Ͱ͢ Page Title Page Subtitle

  44. ©2022 Wantedly, Inc. νϟΠϧυεςΟʔϦϯάɾܧଓεςΟʔϦϯά w QSPDFTTPSͰ͸AHPTBZ lIFMMPz A͕ಈ࡞͢Δ w λεΫੜ੒ޙͷॲཧ͕λεΫͱͯ͠ΤϯΩϡʔ͞ΕΔ

  45. ©2022 Wantedly, Inc. νϟΠϧυεςΟʔϦϯάɾܧଓεςΟʔϦϯά w ܧଓεςΟʔϦϯά͸ੑೳ্ͷར఺͕͋ΔͨΊ (PͰ࠾༻͞Ε͍ͯ·͢ w ܧଓεςΟʔϦϯά͸ίϯύΠϥͷαϙʔτ͕ ඞཁʹͳΔΒ͘͠ɺ3VTUͰ͸࠾༻Ͱ͖͍ͯͳ

    ͍ͷ͕ݱঢ়Ͱ͢ w 3VTUͰ͸εέδϡʔϦϯάͷͱ͖ʹઆ໌ͨ͠ λεΫ͕ΩϡʔΛग़ೖΓ͢ΔνϟΠϧυε ςΟʔϦϯάͱ͍͏ํࣜΛऔ͍ͬͯ·͢ɻ
  46. ©2021 Wantedly, Inc. ·ͱΊ

  47. ©2022 Wantedly, Inc. ·ͱΊ w (Pʹ͸ඪ४ͷϥϯλΠϜ͕͋ΔΑɺ3VTUͰ͸ϥϯλΠϜϥΠϒϥϦΛ࢖͏ඞཁ͕͋ΔΑ w (P͸ϥϯλΠϜ͕தஅ࠶։Λ؅ཧ͢ΔΑɺ3VTUͰ͸ϓϩάϥϚʔ͕؅ཧ͢ΔΑ w ϒϩοΩϯάॲཧΛࠞೖͪ͠Ό͏ͱةݥͩͶ

    w (P͸ϓϦΤϯϓςΟϒͩΑ 3VTUͩͱϊϯϓϦΤϯϓςΟϒͩΑ w Ͳͬͪ΋λεΫͷεέδϡʔϦϯά͸ϫʔΫεςΟʔϦϯάͱ͍͏ઓུͩΑ w (P͸λεΫຊମͰ͸ͳ͘ܧଓΛ౪Έ߹͍ͬͯΔΑ ܧଓϫʔΫεςΟʔϦϯά  w 3VTU͸λεΫຊମΛ౪Έ߹͍ͬͯΔΑ νϟΠϧυϫʔΫεςΟʔϦϯά
  48. ©2022 Wantedly, Inc. Wantedly Engineering Handbook ෺ཧຊΛϓϨθϯτʂ ࣾ಺ͷΤϯδχΞ͕஌Δ΂͖৘ใͷ͏ͪ֎෦ʹ΋ެ։Ͱ͖Δ৘ใΛ ମܥతʹ·ͱΊͨ)BOECPPLͰ͢ IUUQTEPDTXBOUFEMZEFW

    ٕज़తͳ࿩Λ͍ͨ͠ํ͸ΦϯϥΠϯ໘ஊ΋େ׻ܴʂ