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
520
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.5k
メモリー管理の嬉しいバイキング料理
rayfix
7
6.3k
Other Decks in Programming
See All in Programming
[JAWS-UG横浜 #76] イケてるアップデートを宇宙いち早く紹介するよ!
maroon1st
0
480
責務を分離するための例外設計 - PHPカンファレンス 2024
kajitack
6
1.4k
短期間での新規プロダクト開発における「コスパの良い」Goのテスト戦略」 / kamakura.go
n3xem
2
170
ゆるやかにgolangci-lintのルールを強くする / Kyoto.go #56
utgwkk
2
390
クリエイティブコーディングとRuby学習 / Creative Coding and Learning Ruby
chobishiba
0
3.9k
PHPで学ぶプログラミングの教訓 / Lessons in Programming Learned through PHP
nrslib
3
300
開発者とQAの越境で自動テストが増える開発プロセスを実現する
92thunder
1
190
数十万行のプロジェクトを Scala 2から3に完全移行した
xuwei_k
0
280
nekko cloudにおけるProxmox VE利用事例
irumaru
3
440
Kaigi on Railsに初参加したら、その日にLT登壇が決定した件について
tama50505
0
100
今年一番支援させていただいたのは認証系サービスでした
satoshi256kbyte
1
260
テスト自動化失敗から再挑戦しチームにオーナーシップを委譲した話/STAC2024 macho
ma_cho29
1
1.3k
Featured
See All Featured
How GitHub (no longer) Works
holman
311
140k
Agile that works and the tools we love
rasmusluckow
328
21k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.1k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
28
4.4k
Navigating Team Friction
lara
183
15k
Designing for humans not robots
tammielis
250
25k
Documentation Writing (for coders)
carmenintech
66
4.5k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.6k
Building Adaptive Systems
keathley
38
2.3k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
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