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
Unsafe Swift
Search
Ray Fix
March 02, 2017
Programming
3
570
Unsafe Swift
Case study of using unsafe to implement hashable type. Presented at try! Swift 2017 Tokyo, Japan.
Ray Fix
March 02, 2017
Tweet
Share
More Decks by Ray Fix
See All by Ray Fix
アルゴリズムを通じて よりよいアプリを
rayfix
6
2.6k
メモリー管理の嬉しいバイキング料理
rayfix
7
6.6k
Other Decks in Programming
See All in Programming
関数型まつりレポート for JuliaTokai #22
antimon2
0
140
データベースコネクションプール(DBCP)の変遷と理解
fujikawa8
1
270
Cursor AI Agentと伴走する アプリケーションの高速リプレイス
daisuketakeda
1
120
git worktree × Claude Code × MCP ~生成AI時代の並列開発フロー~
hisuzuya
0
280
PHPで始める振る舞い駆動開発(Behaviour-Driven Development)
ohmori_yusuke
2
160
Kotlin エンジニアへ送る:Swift 案件に参加させられる日に備えて~似てるけど色々違う Swift の仕様 / from Kotlin to Swift
lovee
1
250
Webからモバイルへ Vue.js × Capacitor 活用事例
naokihaba
0
750
AWS CDKの推しポイント 〜CloudFormationと比較してみた〜
akihisaikeda
3
300
「Cursor/Devin全社導入の理想と現実」のその後
saitoryc
0
120
Railsアプリケーションと パフォーマンスチューニング ー 秒間5万リクエストの モバイルオーダーシステムを支える事例 ー Rubyセミナー 大阪
falcon8823
4
850
データの民主化を支える、透明性のあるデータ利活用への挑戦 2025-06-25 Database Engineering Meetup#7
y_ken
0
290
Webの外へ飛び出せ NativePHPが切り拓くPHPの未来
takuyakatsusa
1
230
Featured
See All Featured
Designing Experiences People Love
moore
142
24k
Facilitating Awesome Meetings
lara
54
6.4k
Why Our Code Smells
bkeepers
PRO
337
57k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
44
2.4k
Scaling GitHub
holman
459
140k
Designing for humans not robots
tammielis
253
25k
Java REST API Framework Comparison - PWX 2021
mraible
31
8.6k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
357
30k
How to Ace a Technical Interview
jacobian
277
23k
Balancing Empowerment & Direction
lara
1
350
Git: the NoSQL Database
bkeepers
PRO
430
65k
Being A Developer After 40
akosma
90
590k
Transcript
THE SAFETY OF UNSAFE SWIFT @RAYFIX ⚡ try! Swift Japan
2017 1
UB UNDEFINED BEHAVIOR 2
UNDEFINED SCHEDULE = 3
SWIFT SAFETY 4
WORKING WITH C PERFORMANCE LOW LEVEL 5
SWIFT POINTERS UnsafeMutableRawBufferPointer<Pointee> Mutable Raw Buffer <Pointee> 6
7
O(1) CONSTANT TIME LOOKUP DICTIONARIES AND SETS8
O(N) LINEAR TIME LOOKUP BAD HASH O(1) 9
struct Angle: Hashable { var radians: Double … var hashValue:
Int { return radians.hashValue } } ANGLE 10
struct Point: Hashable { var x, y: Double var hashValue:
Int { return x.hashValue ^ y.hashValue } } ^ COMPOSITION 11
struct Point: Hashable { var x, y: Double var hashValue:
Int { return "\(x),\(y)".hashValue } } FAKE IT Heap Allocations are Expensive! 12
protocol HashAlgorithm { init() // 1 mutating func consume(bytes:) //
2 var finalValue: Int // 3 } ROBUST COMPOSITION13
struct FVN1AHash: HashAlgorithm { private var hash: UInt64 = 0xcbf29ce484222325
private let prime: UInt64 = 0x100000001b3 mutating func consume<S: Sequence>(bytes: S) where S.Iterator.Element == UInt8 { for byte in bytes { hash = (hash ^ UInt64(byte)) &* prime } } var finalValue: Int { return Int(truncatingBitPattern: hash) } } HASH ALGO AUTHORS14
var hashValue: Int { var hash = FVN1AHash() hash.consume(x) hash.consume(y)
return hash.finalValue } SAFE EASY CLIENT CODE15
UNSAFE CODE SAFELY HIDDEN AWAY extension HashAlgorithm { mutating func
consume<I: Integer>(_ value: I) { var temp = value withUnsafeBytes(of: &temp) { rawBufferPointer in consume(bytes: rawBufferPointer) } } } 16
Safe, Swifty API for Users Safe Customization Points for Library
Developers Well Tested Unsafe Code UNSAFE CODE SAFELY HIDDEN AWAY 17
18