Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Go beyond the Actor Boundary ~ Swift 6へのConcurr...
Search
giginet
PRO
March 24, 2023
Programming
4.8k
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
510
15年メンテしてきたdotfilesから開発トレンドを振り返る 2011 - 2026
giginet
PRO
2
400
Claude Codeログ基盤の構築
giginet
PRO
7
4.4k
Inside of Swift Export
giginet
PRO
1
2.8k
Swiftビルド弾丸ツアー - Swift Buildが作る新しいエコシステム
giginet
PRO
0
2.8k
SwiftでMCPサーバーを作ろう!
giginet
PRO
3
440
Creating Intuitive Developer Tool in Swift
giginet
PRO
0
1.6k
Mergeable Libraryで 高速なアプリ起動を実現しよう!
giginet
PRO
2
8k
5分でわかるExplicitly Built Modules
giginet
PRO
2
1.8k
Other Decks in Programming
See All in Programming
並列実装の現場、2ヶ月間実務でAIを使い倒したAIもPCも私も限界が近い
ming_ayami
0
130
Dataformのリポジトリを立ち上げるときにまずやること / dataform-day0-2026
snhryt
0
170
Composerを使ったサプライチェーン攻撃の様子を眺めてみる #phpstudy
o0h
PRO
2
250
AI 輔助遺留系統現代化的經驗分享
jame2408
1
680
IBM Bobを活用したレガシーアプリの最新化
oniak3ibm
PRO
1
200
セキュリティの専門家じゃなくてもできる。「セキュリティ意識」をアップデートして サプライチェーン攻撃への耐性を高めよう。
tk3fftk
5
880
ローカルLLMを使ってB2Bサービスを作っていての学び
yaotti
0
200
「AIで開発し、AIを届ける」をEvalでつなぐ 〜AIネイティブに始めるプロダクト開発の実践〜 / Connecting "Develop with AI, deliver AI" with Eval
rkaga
4
5.3k
例外の正しい扱い方 そのエラー try-catchして大丈夫?
jinwatanabe
0
260
エージェンティックRAGにAWSで入門しよう!
har1101
8
1.6k
ADKを使って簡単にAIエージェントを作ってみよう
k1mu21
0
270
AIとASP.NET Coreで雑Webアプリを作った話
mayuki
0
660
Featured
See All Featured
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
3.5k
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
66
55k
Writing Fast Ruby
sferik
630
63k
HDC tutorial
michielstock
2
720
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.5k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
52k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
250
1.3M
Docker and Python
trallard
47
3.9k
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.5k
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
330
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ͳܯࠂΛղফ͍ͯ͘͠ඞཁ ͕͋Δ