Optimising Hot Paths: COW, Inlining and Specialisation
Applying @inline, @_specialize and value-type optimisations based on profiler data.
Identify Before Optimising
Always profile first. Optimise only code that the Time Profiler identifies as a hot path — premature optimisation wastes time.
// Measure → Identify → Optimise → Re-measure
// A 10ms function called once is less important than
// a 0.1ms function called 10,000 times per frameCopy-on-Write (COW)
Swift value types (Array, Dictionary) use COW: storage is shared until a mutation occurs, avoiding unnecessary copies.
var a = [1, 2, 3, 4, 5]
var b = a // no copy yet, shared buffer
b.append(6) // copy happens here (b mutates)
print(a.count) // 5 — a unchangedAll lessons in this course
- Instruments Basics: Time Profiler
- Allocations and Leaks Instruments
- Main Thread Checker and Hangs
- Optimising Hot Paths: COW, Inlining and Specialisation