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
"noncopyable types" の使いどころについて考えてみた
Search
ANDPAD inc
September 26, 2024
Programming
0
550
"noncopyable types" の使いどころについて考えてみた
栗山 徹
@kotetu
2024 年 9 月 6 日
iOSDC 2024 Effect presented by Sansan & ANDPAD
ANDPAD inc
September 26, 2024
Tweet
Share
More Decks by ANDPAD inc
See All by ANDPAD inc
プロダクト開発を支えるデータ利活用:中央集権から「民主化」までの軌跡
andpad
0
120
アンドパッドの Go 勉強会「 gopher 会」とその内容の紹介
andpad
0
350
読もう! Android build ドキュメント
andpad
1
430
アンドパッドにおける CocoaPods ライブラリ群の SwiftPackageManager への移行戦略
andpad
0
200
Flutter は DCM が 9 割
andpad
1
340
Amplify で SPA をホスティングする際の注意点
andpad
1
280
マルチプロダクト開発の現場でAWS Security Hubを1年以上運用して得た教訓
andpad
0
140
rails stats で紐解く ANDPAD のイマを支える技術たち
andpad
1
590
本編では話さない Zig の話
andpad
2
390
Other Decks in Programming
See All in Programming
Comparing decimals in Swift Testing
417_72ki
0
170
画像コンペでのベースラインモデルの育て方
tattaka
3
1.7k
kiroでゲームを作ってみた
iriikeita
0
170
書き捨てではなく継続開発可能なコードをAIコーディングエージェントで書くために意識していること
shuyakinjo
1
280
Bedrock AgentCore ObservabilityによるAIエージェントの運用
licux
9
700
エンジニアのための”最低限いい感じ”デザイン入門
shunshobon
0
110
Understanding Kotlin Multiplatform
l2hyunwoo
0
260
React 使いじゃなくても知っておきたい教養としての React
oukayuka
18
5.7k
What's new in Adaptive Android development
fornewid
0
140
CEDEC2025 長期運営ゲームをあと10年続けるための0から始める自動テスト ~4000項目を50%自動化し、月1→毎日実行にした3年間~
akatsukigames_tech
0
140
A Gopher's Guide to Vibe Coding
danicat
0
160
ライブ配信サービスの インフラのジレンマ -マルチクラウドに至ったワケ-
mirrativ
1
240
Featured
See All Featured
ReactJS: Keep Simple. Everything can be a component!
pedronauck
667
120k
Visualization
eitanlees
146
16k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
139
34k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
48
9.6k
Gamification - CAS2011
davidbonilla
81
5.4k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
Being A Developer After 40
akosma
90
590k
GraphQLの誤解/rethinking-graphql
sonatard
71
11k
KATA
mclloyd
32
14k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
Bash Introduction
62gerente
614
210k
Transcript
© 2024 ANDPAD All Rights Reserved. "noncopyable types" の使いどころについて考えてみた iOSDC
2024 Effect presented by Sansan & ANDPAD 1
© 2024 ANDPAD All Rights Reserved. Confidential Swift 5.9 で導入された
“noncopyable types” を紹介しつつ、 どのような場面で使えるのかについて考えてみた。 セッションのテーマ 2
© 2024 ANDPAD All Rights Reserved. Confidential 2022年6月入社。 2023年6月より黒板機能 (iOS)
の開発を担当。 iOSDC Japan 2024 「ANDPAD黒板のオフラインモード機能 リリースまでの軌跡」 iOSDC Japan 2023 「認証体験向上のために passkeys (パスキー) に対応する」 「Swift Markdownを使ったMarkdownアプリ開発」 栗山 徹 開発本部 Tech Lead Profile | 経 歴 自己紹介 3
© 2024 ANDPAD All Rights Reserved. Confidential noncopyable types とは何か?
利用すると良さそうなケースについて考えてみた まとめ 1 2 3 アジェンダ 4
© 2024 ANDPAD All Rights Reserved. Confidential 現場の効率化から経営改善まで一元管理できる クラウド型建設プロジェクト管理サービス 社 内
社 外 営業 / 監督 / 設計 事務 / 管理職 職人 / 業者 メーカー / 流通 案件管理 資料 工程表 写真 報告 チャット 黒板 図面 受発注 • • • ANDPAD とは 5
© 2024 ANDPAD All Rights Reserved. noncopyable types とは何か? 01
6
© 2024 ANDPAD All Rights Reserved. Confidential 7 (無理矢理1行で説明すると) 値型のコピーに関する制約の追加
noncopyable types •WWDC 2024 - Consume noncopyable types in Swift https://developer.apple.com/videos/play/wwdc2024/10170/ •swift-evolution - SE-0390 (Noncopyable structs and enums) https://github.com/swiftlang/swift-evolution/blob/main/proposals/0390-noncopyable-structs-and-enums.md •Apple Developer の Copyable のドキュメント https://developer.apple.com/documentation/Swift/Copyable 一次情報
© 2024 ANDPAD All Rights Reserved. Confidential 8 代入時の挙動やメモリ管理方式が異なる Swift
の値型と参照型|値型 •struct, enum •代入時は別なインスタンスを生成 (値はコピー) •参照カウントが無い •スタック領域に格納される Swift の値型 file2 file1
© 2024 ANDPAD All Rights Reserved. Confidential 9 代入時の挙動やメモリ管理方式が異なる Swift
の値型と参照型|参照型 •class •代入時はインスタンスの参照がコピーされる •参照カウント (カウント0になったら解放される) •ヒープ領域に格納される Swift の参照型 File インスタンス file1 (参照) file2 (参照)
© 2024 ANDPAD All Rights Reserved. Confidential 10 参照型のように一意のデータを表現するのが難しい Swift
の値型の課題 File インスタンス file1 (参照) file2 (参照) file2 file1 値型 参照型 •値型の代入は全てコピー •参照型を使うと参照カウントの管理が煩雑 •ヒープ領域に格納されるので参照カウント管理と併せてオーバーヘッドが生じる
© 2024 ANDPAD All Rights Reserved. Confidential 11 値型のコピーを Copyable
と ~Copyable で制御 noncopyable types の概要 Copyable と ~Copyable (noncopyable) •これまでの値型 (コピー自由) は Copyable に準拠する型と再定義 •通常の値型は暗黙的に Copyable に準拠する (明示的につけなくても準拠) •Copyable に適合しない型を ~Copyable に準拠した値型として定義 •Copyable と ~Copyable は包含関係にある
© 2024 ANDPAD All Rights Reserved. Confidential 12 noncopyable types
の概要|Copyable https://developer.apple.com/videos/play/wwdc2024/10170/ より引用
© 2024 ANDPAD All Rights Reserved. Confidential 13 noncopyable types
の概要|~Copyable https://developer.apple.com/videos/play/wwdc2024/10170/ より引用
© 2024 ANDPAD All Rights Reserved. Confidential 14 代入時の挙動が変わる ~Copyable
に準拠するとどうなるのか|Copyable Copyable に準拠した値型
© 2024 ANDPAD All Rights Reserved. Confidential 15 代入時の挙動が変わる ~Copyable
に準拠するとどうなるのか|~Copyable ~Copyable に準拠した値型
© 2024 ANDPAD All Rights Reserved. Confidential 16 所有権 (ownership)
が移動したため (所有権が無くなった後の参照・代入はできない) なぜエラーとなるのか? user1 代入時点 user1 所有権 user2 user2 代入時点 user1 所有権 user2 user1 は参照できない
© 2024 ANDPAD All Rights Reserved. Confidential 17 別な変数への代入に使用した変数は使用できなくなる →所有権を通じて一意性が生じている
~Copyable に準拠するとどうなるのか ~Copyable に準拠した値型 user1 user2
© 2024 ANDPAD All Rights Reserved. Confidential 18 メソッドの引数に下記3つのいずれかを指定する必要がある 3
つの ownership 指定 ownership 指定 •consuming : 所有権の移動が発生する、参照・変更可能 •borrowing : 所有権の移動が発生しない、参照のみ •inout : 所有権の移動が発生しない、参照・変更可能 (パラメータの再初期化が必要)
© 2024 ANDPAD All Rights Reserved. 利用すると良さそうなケースについて考えてみた 02 19
© 2024 ANDPAD All Rights Reserved. Confidential 20 ファイル関係など、低レイヤでの利用例が多い noncopyable
types のドキュメント・発表における利用事例 WWDC の場合 •取引情報の処理 •フロッピーディスクの処理 swift-evolution の場合 •ファイルディスクリプタといったファイル関連処理
© 2024 ANDPAD All Rights Reserved. Confidential 21 参照型で一意性を担保する必要がある箇所については 今後値型で実現できるようになる?
今後活用が見込まれそうな用途 class の方が実装しやすい (?) 箇所 •API Client ? •ファイル読み書き? ~Copyable がアプリ開発で必須と言われるためには もう少し活用方法の模索が必要
© 2024 ANDPAD All Rights Reserved. まとめ 03 22
© 2024 ANDPAD All Rights Reserved. Confidential noncopyable types は、値型のコピーに制約をつけることで
値型に新たな使い方をもたらした 既存のプロダクトコードに導入するには、まだ試行錯誤が必要 1 2 セッションのまとめ 23
© 2024 ANDPAD All Rights Reserved. We are Hiring! 開発部公式
X アカウント @andpad_dev カジュアル⾯談 応募フォーム