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
440
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.3k
メモリー管理の嬉しいバイキング料理
rayfix
7
5.9k
Other Decks in Programming
See All in Programming
プログラミングを楽しもう! / Enjoy Programming
chobishiba
1
680
まっちすいっち戦争 / match vs switch
takuyakatsusa
1
630
Laravel標準バリデーションでできること
hmb_ok
1
330
Sementic Kernelのネイティブ関数について
tomokusaba
0
780
before_rails_girls_after_rails_girls
maimux2x
0
300
Cloud RunとCloud PubSubでサーバレスなデータ基盤2024 with Terraform / Cloud Run and PubSub with Terraform
shinyorke
7
1.7k
Kotlinを用いたDSL的な設計手法と使用上の注意
kohii00
2
490
Catch Up with Swift 5.10
ojun9
1
450
20240301_cocone_EMゆるミートアップvol6_LT資料
cocone
0
250
ここ1~2年くらいで 使えるようになった(主要ブラウザーの最新版 がすべて対応した ) ウェブの新機能について ランダムに喋る!
myzkyy
7
5.9k
Не учите алгоритмы
hellsquirrel
1
700
CSRF対策のやり方、そろそろアップデートしませんか / Update your knowledge of CSRF protection
hiro_y
22
12k
Featured
See All Featured
StorybookのUI Testing Handbookを読んだ
zakiyama
10
4.4k
Product Roadmaps are Hard
iamctodd
43
9.6k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
113
18k
The Brand Is Dead. Long Live the Brand.
mthomps
48
19k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
67
38k
Fantastic passwords and where to find them - at NoRuKo
philnash
35
2.4k
Scaling GitHub
holman
456
140k
Six Lessons from altMBA
skipperchong
19
2.9k
Visualization
eitanlees
135
14k
Web Components: a chance to create the future
zenorocha
304
41k
Fontdeck: Realign not Redesign
paulrobertlloyd
75
4.8k
Ruby is Unlike a Banana
tanoku
95
10k
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