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
480
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.4k
メモリー管理の嬉しいバイキング料理
rayfix
7
6.1k
Other Decks in Programming
See All in Programming
CSC307 Lecture 06
javiergs
PRO
0
360
なぜ宣言的 UI は壊れにくいのか / Why declarative UI is less fragile
uenitty
29
13k
Modern Angular: Renovation for Your Applications
manfredsteyer
PRO
0
140
Javaの現状2024夏 / Java current status 2024 summer
kishida
4
1.4k
Exploring the Gradually Lost Technical Skills in the Cloud Native Era
hwchiu
2
3.9k
【Go言語】ジェネリクス
tomo1227
0
170
CSC307 Lecture 07
javiergs
PRO
0
220
Play Billing Library 7.0.0 変更点まとめ@potatotips#88
kako351
0
160
Android開発者のための Kotlin Multiplatform入門
ntaro
0
190
APIのない大学ログインWebサービスをWKWebViewとJavaScriptでアプリ化した話
akidon0000
1
330
feature環境をGitHub ActionsとCloudFormationでいい感じに管理する
nealle
2
310
DynamoDB コスト最適化っぽいことの基本 with Terraform
kuro_kurorrr
2
250
Featured
See All Featured
Six Lessons from altMBA
skipperchong
24
3.2k
Speed Design
sergeychernyshev
9
270
StorybookのUI Testing Handbookを読んだ
zakiyama
15
4.9k
Testing 201, or: Great Expectations
jmmastey
33
6.9k
Fantastic passwords and where to find them - at NoRuKo
philnash
42
2.7k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
353
29k
Visualization
eitanlees
139
14k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
13
430
Code Reviewing Like a Champion
maltzj
517
39k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
189
16k
Imperfection Machines: The Place of Print at Facebook
scottboms
262
13k
KATA
mclloyd
20
13k
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