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
460
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.4k
メモリー管理の嬉しいバイキング料理
rayfix
7
6k
Other Decks in Programming
See All in Programming
Enjoy Creative Coding with Ruby (RubyKaigi2024)
chobishiba
0
890
TypeScriptで使いやすいOpenAPIの書き方
yukimochi_dwango
1
950
RaaP
ksss
0
160
Ruby on Fails - effective error handling with Rails conventions
talyssonoc
0
300
ペパボOpenTelemetry革命
pyama86
2
1.2k
The Design of Everyday APIs - PyCon 2024
roguelynn
1
210
[RubyKaigi 2024] Ruby Mixology 101: adding shots of PHP, Elixir, and more
palkan
0
140
TypeScriptコードの漸進的改善 / Progressive Improvement of TypeScript Code
medley
1
450
RubyGems on ruby.wasm
kateinoigakukun
0
140
Three ways to use AI on Android: The Good, the Bad and the Ugly
marxallski
0
120
TypeScriptの型とパフォーマンス (TSKaigi 2024)
ypresto
14
5.1k
FoodGram
iseruuuuu
0
230
Featured
See All Featured
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
26
2.3k
Stop Working from a Prison Cell
hatefulcrawdad
266
19k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
155
14k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
323
20k
How To Stay Up To Date on Web Technology
chriscoyier
782
250k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
245
20k
Embracing the Ebb and Flow
colly
80
4.2k
5 minutes of I Can Smell Your CMS
philhawksworth
199
19k
The Brand Is Dead. Long Live the Brand.
mthomps
49
30k
The Cost Of JavaScript in 2023
addyosmani
21
4k
Rebuilding a faster, lazier Slack
samanthasiow
74
8.3k
Navigating Team Friction
lara
179
13k
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