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
610
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.7k
メモリー管理の嬉しいバイキング料理
rayfix
7
6.8k
Other Decks in Programming
See All in Programming
[AI Engineering Summit Tokyo 2025] LLMは計画業務のゲームチェンジャーか? 最適化業務における活⽤の可能性と限界
terryu16
1
190
Kotlin Multiplatform Meetup - Compose Multiplatform 외부 의존성 아키텍처 설계부터 운영까지
wisemuji
0
150
Developing static sites with Ruby
okuramasafumi
0
340
Graviton と Nitro と私
maroon1st
0
160
モデル駆動設計をやってみようワークショップ開催報告(Modeling Forum2025) / model driven design workshop report
haru860
0
300
從冷知識到漏洞,你不懂的 Web,駭客懂 - Huli @ WebConf Taiwan 2025
aszx87410
2
3.2k
まだ間に合う!Claude Code元年をふりかえる
nogu66
5
920
【卒業研究】会話ログ分析によるユーザーごとの関心に応じた話題提案手法
momok47
0
150
フルサイクルエンジニアリングをAI Agentで全自動化したい 〜構想と現在地〜
kamina_zzz
0
340
DevFest Android in Korea 2025 - 개발자 커뮤니티를 통해 얻는 가치
wisemuji
0
180
Canon EOS R50 V と R5 Mark II 購入でみえてきた最近のデジイチ VR180 事情、そして VR180 静止画に活路を見出すまで
karad
0
140
AIエンジニアリングのご紹介 / Introduction to AI Engineering
rkaga
8
3.5k
Featured
See All Featured
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
2.2k
Building the Perfect Custom Keyboard
takai
2
670
Imperfection Machines: The Place of Print at Facebook
scottboms
269
13k
Rails Girls Zürich Keynote
gr2m
95
14k
Documentation Writing (for coders)
carmenintech
77
5.2k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
130
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
80
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
61
51k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
How to Ace a Technical Interview
jacobian
281
24k
Producing Creativity
orderedlist
PRO
348
40k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
9.2k
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