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
610
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.8k
Other Decks in Programming
See All in Programming
JETLS.jl ─ A New Language Server for Julia
abap34
2
480
AI前提で考えるiOSアプリのモダナイズ設計
yuukiw00w
0
210
これならできる!個人開発のすゝめ
tinykitten
PRO
0
150
Denoのセキュリティに関する仕組みの紹介 (toranoana.deno #23)
uki00a
0
230
AIの誤りが許されない業務システムにおいて“信頼されるAI” を目指す / building-trusted-ai-systems
yuya4
7
4.3k
re:Invent 2025 トレンドからみる製品開発への AI Agent 活用
yoskoh
0
640
生成AIを利用するだけでなく、投資できる組織へ
pospome
2
440
2年のAppleウォレットパス開発の振り返り
muno92
PRO
0
180
クラウドに依存しないS3を使った開発術
simesaba80
0
220
Findy AI+の開発、運用におけるMCP活用事例
starfish719
0
2.2k
AI Agent の開発と運用を支える Durable Execution #AgentsInProd
izumin5210
7
1.9k
ELYZA_Findy AI Engineering Summit登壇資料_AIコーディング時代に「ちゃんと」やること_toB LLMプロダクト開発舞台裏_20251216
elyza
2
1.1k
Featured
See All Featured
WCS-LA-2024
lcolladotor
0
410
AI: The stuff that nobody shows you
jnunemaker
PRO
2
170
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
61
51k
A Tale of Four Properties
chriscoyier
162
24k
Fashionably flexible responsive web design (full day workshop)
malarkey
408
66k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
115
100k
Java REST API Framework Comparison - PWX 2021
mraible
34
9.1k
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Learning to Love Humans: Emotional Interface Design
aarron
274
41k
How to Ace a Technical Interview
jacobian
281
24k
Context Engineering - Making Every Token Count
addyosmani
9
600
We Have a Design System, Now What?
morganepeng
54
8k
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