Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Go beyond the Actor Boundary ~ Swift 6へのConcurr...
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
giginet
PRO
March 24, 2023
Programming
5k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Go beyond the Actor Boundary ~ Swift 6へのConcurrency対応 ~
giginet
PRO
March 24, 2023
More Decks by giginet
See All by giginet
🀄️ on swiftc
giginet
PRO
0
590
15年メンテしてきたdotfilesから開発トレンドを振り返る 2011 - 2026
giginet
PRO
2
490
Claude Codeログ基盤の構築
giginet
PRO
7
4.7k
Inside of Swift Export
giginet
PRO
1
3.5k
Swiftビルド弾丸ツアー - Swift Buildが作る新しいエコシステム
giginet
PRO
0
3k
SwiftでMCPサーバーを作ろう!
giginet
PRO
3
490
Creating Intuitive Developer Tool in Swift
giginet
PRO
0
1.7k
Mergeable Libraryで 高速なアプリ起動を実現しよう!
giginet
PRO
2
8.6k
5分でわかるExplicitly Built Modules
giginet
PRO
2
1.9k
Other Decks in Programming
See All in Programming
設計次第でAIコードの読む量は減らせる / designing-for-code-reading
minodriven
23
9k
Heart of Swift Concurrency
koher
0
1.1k
難しいけど、読めた。- OSSの入口に立った話。
sts11142
0
130
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
410
【高い買い物LT会】初任給で話題の国産フィジカルAIを買った話
akagami
PRO
0
170
re:Inventに行く前に知っておきたい現地参加のノウハウ
nokomoro3
0
250
AI が書く Go コードの品質を劇的に向上させる Linter: “declscope”
mpyw
0
440
標準パッケージに uuid が追加された 背景から見る Go らしい意思決定 / go_127_uuid_decision
convto
5
9k
技術的負債を組織課題として解く-増えすぎたマイクロサービスとの戦い-
reimaru
1
2.5k
thread_parallel_with_free-threaded_Python_and_NumPy.pdf
riku_sakamoto
0
360
wkhtmltopdfの次どうするか問題2026
willnet
2
1.8k
Apple Intelligence を用いた個人情報誤送信防止、及びユーザーリクエスト体験の改善について
yukiny
0
220
Featured
See All Featured
Done Done
chrislema
187
17k
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
550
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
2
930
The Curious Case for Waylosing
cassininazir
1
540
The untapped power of vector embeddings
frankvandijk
2
1.9k
Speed Design
sergeychernyshev
33
2.1k
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
2
700
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6.1k
How STYLIGHT went responsive
nonsquared
100
6.3k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
1k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.6k
Transcript
Go beyond the Actor Boundary 2023/3/22 Swift 6ͷConcurrencyରԠ @giginet
୭ʁ • @giginet • LINEגࣜձࣾ Developer Experience։ൃνʔϜ • ٕज़ސ •
ϚωʔϑΥϫʔυ, peep, ϢϏϨδ • OSSϝϯςφ • Carthage, XcodeGen, fastlaneͳͲ • ϙέϞϯϥϯΫϚον
ࠓ͢͜ͱ • Swift Concurrencyʹ͍͓ͭͯ͞Β͍ • Race Conditions, Actor Boundary, Sendable
• Swift 6.0࣌ʹ͚ͯԿΛ͢Εྑ͍͔ • ݱঢ়ઃఆͰ͖ΔޓϞʔυʹ͍ͭͯڍಈݱঢ়Λௐͨ
Swift 6 • Swift 6͔ΒRace ConditionsͷνΣοΫ͕ݫ֨Խ • εϨουηʔϑͰͳ͍ίʔυίϯύΠϧΤϥʔʹͳͬͯ͠·͏ • Sendableʹద߹͍ͯ͠ͳ͍ܕΛϓϩηεΛ͑ͯڞ༗͍͚ͯ͠ͳ
͍ʢActor boundaryʣ
Race conditions? • ෳͷϓϩηεʢॲཧʣ͕ڞ༗ϦιʔεʹΞΫηεͯ͠༧ظ͠ͳ͍ڍ ಈΛҾ͖ى͜͢͜ͱ value = 1 value =
2 value = 2? 💥 ϓϩηε1 ϓϩηε2 ڞ༗Ϧιʔε let value = Shared.value Shared.value = value + 1 let value = Shared.value // Do long task await doLongTask() Shared.value = value + 1
Sendable • Race ConditionsΛ͙ͨΊʹSendableͱ͍͏protocol͕ಋೖ • ෳͷϓϩηε͔ΒΛڞ༗ͯ҆͠શͩͱ͍͏͜ͱΛࣔ͢ protocol • ҎԼͷUserෆมͰ͋Γɺෳ͔Βࢀরͯ͠Race Conditions
͕ى͖ͳ͍ • = Sendableʹద߹Ͱ͖Δ struct User: Sendable { let name: String let age: Int64 }
ࢀߟࢿྉ • Protect mutable state with Swift actors • https://developer.apple.com/videos/play/wwdc2021/10133/
• Eliminate data races using Swift Concurrency • https://developer.apple.com/videos/play/wwdc2022/110351/
Swift 6 • ͜ͷΑ͏ͳةݥͳίʔυΛίϯύΠϧ࣌ʹνΣοΫͯ͘͠ΕΔΑ͏ʹ ͳΔ • Sendable͕ඞཁͳՕॴͰඞਢʹͳΔ(Sendable Constraints) • ಛఆͷεϨουͰͷಈ࡞Λڧ੍
(Actor Isolation Checking) • →ࠓ·Ͱಈ͍͍ͯͨίʔυ͕ಈ͔ͳ͘ͳΔՄೳੑ • →Swift 6͕ग़ͨͱ͖ʹ߄ͯͳ͍Α͏ʹɺࠓͷ͏ͪʹ४උ͓ͯ͘͠ඞ ཁ͕͋Δ
Ͳ͏ͬͯҠߦ͍͚ͯ͠ྑ͍ͷʁ • Incremental migration to concurrency checking • https://github.com/apple/swift-evolution/blob/main/proposals/0337- support-incremental-migration-to-concurrency-checking.md
• ҆શͳඇಉظॲཧνΣοΫͷͨΊͷϚΠάϨʔγϣϯख๏ΛఏҊͨ͠ proposal
None
কདྷతʹյΕΔͱ͜ΖΛΔʹʁ • -warn-concurrency • Swift 6͔ΒΤϥʔʹͳΔ߲Λܯࠂͯ͘͠ΕΔϏϧυΦϓγϣϯ • ͜ΕΛ͚ͭΔ͜ͱͰɺSwift 6͔ΒΤϥʔʹͳΔ෦Λܯࠂͯ͘͠ΕΔΑ͏ ʹͳΔ
• Swift 5.7͔ΒมΘͬͨͷͰ࠷ۙਪ͞Εͳ͍ʢ·ͩ͑Δʣ
-strict-concurrency= • Swift 5.7(Xcode 14.0)͔ΒมΘͬͨ • https://github.com/apple/swift/pull/42523 • ΘΓʹ-strict-concurrency=LEVELͱ͍͏ΦϓγϣϯͰܯࠂϨϕϧΛબ ΔΑ͏ʹ
• minimal, targeted, completeͷ3ஈ֊ • completeϞʔυ͕-warn-concurrencyͱಉ
Xcode Projectͷઃఆ • ͳ͔ͥXcode 14.3βͰͣͬͱͿͬյΕ͍͕ͯͨɺ14.3RCͰͬͱͬͨ
// swift-tools-version: 5.8 // The swift-tools-version declares the minimum version
of Swift required to build this package. import PackageDescription let package = Package( name: "MyPackage", platforms: [ .macOS(.v11), ], targets: [ .executableTarget( name: "MyPackage", path: "Sources", swiftSettings: [ .unsafeFlags([ "-Xfrontend", "-strict-concurrency=complete", ]), ] ), ] ) Swift Packageͷઃఆ
֤ϨϕϧԿ͕ҧ͏ͷ͔ʁ • Proposalʹॻ͔Ε͍ͯͳ͍ • ͜ͷΦϓγϣϯ͕ಋೖ͞ΕͨPRswiftcͷ࣮ͰΔ͜ͱ͕Ͱ͖Δ • ͜ΕΛಡΜ͚ͩͩ͡ΌΑ͘Θ͔Βͳ͍ https://github.com/apple/swift/blob/main/include/swift/Basic/LangOptions.h https://github.com/apple/swift/pull/42523
Sendable Constraints Actor Isolation Checking Minimal ໌ࣔతʹSendableʹద߹͍ͯ͠ Δ߹ ConcurrencyΛ༻͍ͯ͠Δ߹ Targeted
ConcurrencyΛ༻͍ͯ͠Δ߹ ConcurrencyΛ༻͍ͯ͠Δ߹ Completed શͯͷίʔυ શͯͷίʔυ
Minimal • Xcodeͷॳظઃఆ • SendableΛ໌࣮ࣔͨ͠ʹؔͯ͠SendableͷνΣοΫ͕Δ ⚠Sendableʹద߹ग़དྷͳ͍߹ʹܯࠂʹͳΔ • ڞ༗͞ΕΔՄมͳ͕͋Δ߹ͳͲ
Targeted • ConcurrencyΛར༻͍ͯ͠ΔՕॴͰSendableͷνΣοΫ͕༗ޮʹͳΔ ⚠SendableͳclosureΛٻΊ͍ͯΔՕॴͰnon-sendableͳclosureΛ͢ ⚠SendableͰͳ͍ΛSendableͳclosureͰΩϟϓνϟ͢Δ
• non-sendableͳΛSendableͳclosureͰ͓͏ͱ͢Δ(Sendable Constraints) • actor͕ࢦఆ͞Ε͍ͯͳ͍asyncϝιουΛผͷactor͔Βݺ΅͏ͱ͢Δ(Actor isolation Checking)
Complete ɾSwift 6ޓϞʔυ ɾwarn-concurrencyͱಉ ⚠ConcurrencyΛ͍ͬͯͳ͍෦ʹؔͯ͠Sendable੍/Actor IsolationΛ νΣοΫ͢Δ • `MainActor(unsafe)`͕༗ޮʹͳΔ 🚨UIViewControllerͳͲɺϝΠϯεϨουͰͷಈ࡞Λఆ͍ͯ͠Δ࣮͕
MainActorҎ֎͔Βݺͳ͘ͳΔ
• ಉظؔʹ͍ͭͯSendable Constraints͕νΣοΫ͞ΕΔ • MainActor(unsafe)ͳ࣮͕MainActorʹͳΔͨΊɺ͜Ε·Ͱͱҧ͍ಉظతʹݺͳ͘ͳΔ • UIViewControllerͳͲ
ࢀߟ • Swift Concurrency ʹରԠ͍ͯͨ͘͠Ίͷ Strict Concurrency Check ͱ `@preconcurrency
import` • https://qiita.com/maiyama18/items/7f2a7c0ed4cce9d3af72
Ͳ͏͍ͬͯͯ͘͠ͷʁ • ࠷ऴతʹSwift 6·ͰʹCompleteͷܯࠂ͕ղফ͞Εͳ͍ͱϏϧυ͕௨Βͳ͘ ͳΔ • ࠓޙஈ֊తϚΠάϨʔγϣϯΛਐΊ͍ͯ͘͜ͱͰɺରԠͷݟ͕ཷ·͍͖ͬͯ ͦ͏ • ͪ͜ΒͷهࣄͰҠߦϓϥϯʹ͍ͭͯ৮ΕΒΕ͍ͯͨ
• Introducing Swift Concurrency to Merpay code • https://engineering.mercari.com/en/blog/entry/20221223-introducing- swift-concurrency-to-merpay-code/
Ͳ͏͍ͬͯͯ͘͠ͷʁ • Concurrencyͷཧղʹ5͕࣌ؒΓͳ͍ • ༷ʑͳݴޠ༷ඇಉظॲཧͷૉཆ͕ඞཁ • actor/@MainActor/Sendable/@preconcurrency • ଓ͖࠙ձͰ 🍻🍻
·ͱΊ • ܯࠂΛઃఆͯ͠Swift 6ʹ͚ͯিܸʹඋ͑Δ • -warn-concurrencyͰͳ͘ɺ-strict-concurrency=completeΛ͏ • ϚΠάϨʔγϣϯϓϥϯʹ͍ͭͯߟ͍͑ͯ͘ඞཁ͕͋Δ • ଟ͘ͷΞϓϦέʔγϣϯͰMain
Actor IsolatedͳܯࠂΛղফ͍ͯ͘͠ඞཁ ͕͋Δ