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
370
"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
Amplify で SPA をホスティングする際の注意点
andpad
0
13
マルチプロダクト開発の現場でAWS Security Hubを1年以上運用して得た教訓
andpad
0
56
rails stats で紐解く ANDPAD のイマを支える技術たち
andpad
1
390
本編では話さない Zig の話
andpad
2
240
ANDPAD黒板のオフラインモード機能 リリースまでの軌跡
andpad
0
250
アンドパッドのマルチプロダクト戦略を支える SRE
andpad
1
240
Introduction of Cybersecurity with OSS (RDRC2024)
andpad
1
69
開発チームとともに進めるインフラセキュリティの継続的な改善
andpad
2
110
ANDPAD and Ruby
andpad
1
790
Other Decks in Programming
See All in Programming
Honoをフロントエンドで使う 3つのやり方
yusukebe
7
3.2k
GitHub Actions × RAGでコードレビューの検証の結果
sho_000
0
260
法律の脱レガシーに学ぶフロントエンド刷新
oguemon
5
740
責務と認知負荷を整える! 抽象レベルを意識した関心の分離
yahiru
1
210
Open source software: how to live long and go far
gaelvaroquaux
0
630
Immutable ActiveRecord
megane42
0
140
Spring gRPC について / About Spring gRPC
mackey0225
0
220
昭和の職場からアジャイルの世界へ
kumagoro95
1
370
ファインディの テックブログ爆誕までの軌跡
starfish719
2
1.1k
Introduction to kotlinx.rpc
arawn
0
690
Lottieアニメーションをカスタマイズしてみた
tahia910
0
130
データベースのオペレーターであるCloudNativePGがStatefulSetを使わない理由に迫る
nnaka2992
0
140
Featured
See All Featured
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.3k
The Art of Programming - Codeland 2020
erikaheidi
53
13k
Scaling GitHub
holman
459
140k
It's Worth the Effort
3n
184
28k
What's in a price? How to price your products and services
michaelherold
244
12k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
2.1k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
120k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
6
550
Visualization
eitanlees
146
15k
Facilitating Awesome Meetings
lara
52
6.2k
Making the Leap to Tech Lead
cromwellryan
133
9.1k
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
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 カジュアル⾯談 応募フォーム