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
490
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.2k
Other Decks in Programming
See All in Programming
AndroidアプリのUIバリエーションをあの手この手で確認する / Check UI variations of Android apps by various means
tkmnzm
1
140
rbs-inlineを導入してYARDからRBSに移行する
euglena1215
1
270
Debugging: All you need to know (for simultaneous interpreting)
jmatsu
2
760
僕が思い描くTypeScriptの未来を勝手に先取りする
yukukotani
9
2.4k
Go Code Generation at newmo / 2024-08-27 #newmo_layerx_go
genkey6
0
560
Kotlin 2.0 and Beyond
antonarhipov
2
150
Rustではじめる負荷試験
skanehira
5
1.2k
長期運用プロダクトの開発速度を維持し続けるためのリファクタリング実践例
wataruss
8
2.7k
私のEbitengineの第一歩
qt_luigi
0
450
Jakarta EE meets AI
ivargrimstad
0
380
Method Swizzlingを行うライブラリにおけるマルチモジュール設計
yoshikma
0
120
仮想ファイルシステムを導入して開発環境のストレージ課題を解消する
segadevtech
2
540
Featured
See All Featured
The Invisible Side of Design
smashingmag
295
50k
Mobile First: as difficult as doing things right
swwweet
221
8.8k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2k
Faster Mobile Websites
deanohume
304
30k
We Have a Design System, Now What?
morganepeng
48
7.1k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
230
17k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
36
2.1k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
363
22k
Gamification - CAS2011
davidbonilla
79
5k
Making the Leap to Tech Lead
cromwellryan
128
8.8k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
5
480
The Pragmatic Product Professional
lauravandoore
31
6.2k
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