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
開発生産性を上げるための生成AI活用術
starfish719
1
170
iOS 17で追加されたSubscriptionStoreView を利用して5分でサブスク実装チャレンジ
natmark
0
590
2025年版 サーバーレス Web アプリケーションの作り方
hayatow
23
25k
ソフトウェア設計の実践的な考え方
masuda220
PRO
3
480
Let's Write a Train Tracking Algorithm
twocentstudios
0
220
開発者への寄付をアプリ内課金として実装する時の気の使いどころ
ski
0
350
iOSエンジニア向けの英語学習アプリを作る!
yukawashouhei
0
180
Playwrightはどのようにクロスブラウザをサポートしているのか
yotahada3
7
2.3k
Web Components で実現する Hotwire とフロントエンドフレームワークの橋渡し / Bridging with Web Components
da1chi
3
1.8k
育てるアーキテクチャ:戦い抜くPythonマイクロサービスの設計と進化戦略
fujidomoe
1
150
CSC509 Lecture 01
javiergs
PRO
1
430
After go func(): Goroutines Through a Beginner’s Eye
97vaibhav
0
230
Featured
See All Featured
A better future with KSS
kneath
239
17k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
960
The Power of CSS Pseudo Elements
geoffreycrofte
79
6k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.1k
Why You Should Never Use an ORM
jnunemaker
PRO
59
9.6k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
132
19k
Code Review Best Practice
trishagee
72
19k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
45
2.5k
Navigating Team Friction
lara
189
15k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
30
2.9k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.2k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
188
55k
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