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
590
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.7k
Other Decks in Programming
See All in Programming
全員アーキテクトで挑む、 巨大で高密度なドメインの紐解き方
agatan
5
6.4k
Patterns of Patterns (and why we need them)
denyspoltorak
0
110
GeistFabrik and AI-augmented software development
adewale
PRO
0
120
チーム開発の “地ならし"
konifar
8
5.7k
Flutterアプリ運用の現場で役立った監視Tips 5選
ostk0069
1
490
AIを駆使して新しい技術を効率的に理解する方法
nogu66
1
650
目的で駆動する、AI時代のアーキテクチャ設計 / purpose-driven-architecture
minodriven
9
3k
JEP 496 と JEP 497 から学ぶ耐量子計算機暗号入門 / Learning Post-Quantum Crypto Basics from JEP 496 & 497
mackey0225
2
450
Promise.tryで実現する新しいエラーハンドリング New error handling with Promise try
bicstone
3
520
モビリティSaaSにおけるデータ利活用の発展
nealle
0
550
Agentに至る道 〜なぜLLMは自動でコードを書けるようになったのか〜
mackee
5
1.9k
Herb to ReActionView: A New Foundation for the View Layer @ San Francisco Ruby Conference 2025
marcoroth
0
160
Featured
See All Featured
Large-scale JavaScript Application Architecture
addyosmani
514
110k
jQuery: Nuts, Bolts and Bling
dougneiner
65
8k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
61k
Typedesign – Prime Four
hannesfritz
42
2.9k
The Power of CSS Pseudo Elements
geoffreycrofte
80
6.1k
Context Engineering - Making Every Token Count
addyosmani
9
410
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
1.8k
Rails Girls Zürich Keynote
gr2m
95
14k
Making Projects Easy
brettharned
120
6.5k
Scaling GitHub
holman
464
140k
Why Our Code Smells
bkeepers
PRO
340
57k
Stop Working from a Prison Cell
hatefulcrawdad
272
21k
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