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
410
"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
読もう! Android build ドキュメント
andpad
1
170
アンドパッドにおける CocoaPods ライブラリ群の SwiftPackageManager への移行戦略
andpad
0
35
Flutter は DCM が 9 割
andpad
1
49
Amplify で SPA をホスティングする際の注意点
andpad
0
41
マルチプロダクト開発の現場でAWS Security Hubを1年以上運用して得た教訓
andpad
0
71
rails stats で紐解く ANDPAD のイマを支える技術たち
andpad
1
420
本編では話さない Zig の話
andpad
2
270
ANDPAD黒板のオフラインモード機能 リリースまでの軌跡
andpad
0
300
アンドパッドのマルチプロダクト戦略を支える SRE
andpad
1
270
Other Decks in Programming
See All in Programming
RailsでCQRS/ESをやってみたきづき
suzukimar
2
1.2k
ステートソーシング型イベント駆動の視点で捉えるCQRS+ES
shinnosuke0522
0
140
kintone開発を効率化するためにチームで試した施策とその結果を大放出!
oguemon
1
440
Devin , 正しい付き合い方と使い方 / Living and Working with Devin
yukinagae
1
280
Generative AI for Beginners .NETの紹介
tomokusaba
1
230
TCAを用いたAmebaのリアーキテクチャ
dazy
0
260
[JAWS DAYS 2025] 最近の DB の競合解決の仕組みが分かった気になってみた
maroon1st
0
230
PEPCは何を変えようとしていたのか
ken7253
3
340
AWS Step Functions は CDK で書こう!
konokenj
5
970
The Price of Micro Frontends… and Your Alternatives @bastacon 2025 in Frankfurt
manfredsteyer
PRO
0
360
CIBMTR振り返り+敗北から学ぶコンペの取り組み方反省
takanao
1
390
CloudRun, Spanner に対する負荷試験の反省と オブザーバビリティによるアプローチ
oyasumipants
1
240
Featured
See All Featured
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
233
17k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
50
2.3k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.1k
Fashionably flexible responsive web design (full day workshop)
malarkey
406
66k
Large-scale JavaScript Application Architecture
addyosmani
511
110k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
30
2.3k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.3k
How to train your dragon (web standard)
notwaldorf
91
5.9k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
45
9.5k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
Statistics for Hackers
jakevdp
797
220k
Bash Introduction
62gerente
611
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 カジュアル⾯談 応募フォーム