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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Ray Fix
March 02, 2017
Programming
3
630
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.8k
メモリー管理の嬉しいバイキング料理
rayfix
7
6.8k
Other Decks in Programming
See All in Programming
車輪の再発明をしよう!PHP で実装して学ぶ、Web サーバーの仕組みと HTTP の正体
h1r0
2
300
Ruby and LLM Ecosystem 2nd
koic
1
1.3k
Nostalgia Meets Technology: Super Mario with TypeScript
manfredsteyer
PRO
0
100
Claude Codeログ基盤の構築
giginet
PRO
7
3.6k
今年もTECHSCOREブログを書き続けます!
hiraoku101
0
120
PHP でエミュレータを自作して Ubuntu を動かそう
m3m0r7
PRO
2
130
CSC307 Lecture 15
javiergs
PRO
0
260
Rethinking API Platform Filters
vinceamstoutz
0
150
実践ハーネスエンジニアリング #MOSHTech
kajitack
1
280
20260320登壇資料
pharct
0
110
Reactive ❤️ Loom: A Forbidden Love Story
franz1981
2
130
エンジニアの「手元の自動化」を加速するn8n 2026.02.27
symy2co
0
180
Featured
See All Featured
Testing 201, or: Great Expectations
jmmastey
46
8.1k
Utilizing Notion as your number one productivity tool
mfonobong
4
270
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.1k
Paper Plane (Part 1)
katiecoart
PRO
0
5.8k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
68
38k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
1.8k
Mobile First: as difficult as doing things right
swwweet
225
10k
SEO for Brand Visibility & Recognition
aleyda
0
4.4k
Six Lessons from altMBA
skipperchong
29
4.2k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
240
The Language of Interfaces
destraynor
162
26k
Building AI with AI
inesmontani
PRO
1
820
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