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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Ray Fix
March 02, 2017
Programming
3
620
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.8k
メモリー管理の嬉しいバイキング料理
rayfix
7
6.8k
Other Decks in Programming
See All in Programming
new(1.26) ← これすき / kamakura.go #8
utgwkk
0
1.9k
AI主導でFastAPIのWebサービスを作るときに 人間が構造化すべき境界線
okajun35
0
630
Ruby x Terminal
a_matsuda
7
590
PJのドキュメントを全部Git管理にしたら、一番喜んだのはAIだった
nanaism
0
240
2026年は Rust 置き換えが流行る! / 20260220-niigata-5min-tech
girigiribauer
0
230
コードレビューをしない選択 #でぃーぷらすトウキョウ
kajitack
0
190
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
320
CopilotKit + AG-UIを学ぶ
nearme_tech
PRO
2
150
AWS Infrastructure as Code の新機能 2025 総まとめ 〜SA 4人による怒涛のデモ祭り〜
konokenj
10
3.3k
LangChain4jとは一味違うLangChain4j-CDI
kazumura
1
160
社内規程RAGの精度を73.3% → 100%に改善した話
oharu121
13
7.8k
猫の手も借りたい!ので AIエージェント猫を作って社内に放した話 Claude Code × Container Lambda の Slack Bot "DevNeko"
naramomi7
0
260
Featured
See All Featured
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
310
Building a Modern Day E-commerce SEO Strategy
aleyda
45
8.8k
Fireside Chat
paigeccino
42
3.8k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
54k
Ethics towards AI in product and experience design
skipperchong
2
220
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
400
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
2
150
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
190
The World Runs on Bad Software
bkeepers
PRO
72
12k
Balancing Empowerment & Direction
lara
5
930
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
1
320
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.1k
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