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
510
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
Arm移行タイムアタック
qnighy
0
310
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
480
Content Security Policy入門 セキュリティ設定と 違反レポートのはじめ方 / Introduction to Content Security Policy Getting Started with Security Configuration and Violation Reporting
uskey512
1
530
Jakarta EE meets AI
ivargrimstad
0
530
型付き API リクエストを実現するいくつかの手法とその選択 / Typed API Request
euxn23
8
2.2k
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
1.4k
現場で役立つモデリング 超入門
masuda220
PRO
15
3.2k
Compose 1.7のTextFieldはPOBox Plusで日本語変換できない
tomoya0x00
0
190
TypeScript Graph でコードレビューの心理的障壁を乗り越える
ysk8hori
2
1.1k
詳細解説! ArrayListの仕組みと実装
yujisoftware
0
580
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
1
260
Nurturing OpenJDK distribution: Eclipse Temurin Success History and plan
ivargrimstad
0
880
Featured
See All Featured
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
126
18k
KATA
mclloyd
29
14k
The Cost Of JavaScript in 2023
addyosmani
45
6.7k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Building a Scalable Design System with Sketch
lauravandoore
459
33k
Producing Creativity
orderedlist
PRO
341
39k
[RailsConf 2023] Rails as a piece of cake
palkan
52
4.9k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
93
16k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
410
Faster Mobile Websites
deanohume
305
30k
Embracing the Ebb and Flow
colly
84
4.5k
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