21.5 · The costs that show up in profiles: collections, strings, formatters, JSON, images and disk

Most slow code is ordinary code doing a lot of an expensive thing: a linear search in a loop, a formatter built a thousand times, a disk write per item. The costs are predictable once you know which operations hide them.

Mental model. Ask of every hot operation: how many times, how big is the input, and does it touch disk, network or the main thread?

In memory

Data-structure, string, formatter, JSON and image costs.

  1. Which everyday operations are surprisingly expensive in Swift and iOS code, and how do you find and fix them? 21.5.1 · L3
    The same few costs account for most “it is slow” reports. Linear search in a loop: array.contains is O(n), so calling it for every element of another array is…

On disk

Batching, transactions and write volume.

  1. How do you reduce disk and database cost: batching writes, transactions, atomic writes, memory mapping and the system’s disk-write limit? 21.5.2 · L5
    Disk cost is dominated by how many separate operations you issue and whether each one waits for the storage to confirm. Writing one record at a time is far…

More in Performance & energy