In a Swift JSON tokenizer's Release build, Time Profiler shows swift_retain and swift_release dominating the hot loop: what causes that traffic, and how do you cut it?
answer
- copying a strong reference costs
- walking class nodes one by one
- references hidden inside value types
- trivial structs in contiguous storage
basics
~20 sEvery copy of a strong reference the optimizer cannot prove redundant costs a retain and a release: stepping through linked class tokens, or copying structs that hold String or Array buffers. Store tokens as trivial structs in one contiguous array.
solid answer
~50 sARC retains when a strong reference is copied and releases when a copy's lifetime ends; `-O` removes only the pairs it can prove redundant. A tokenizer that walks `final class Token` nodes with `current = token.next` retains the next node and releases the previous one on every step, and a struct whose fields are `String` or `Array` still retains their buffers when copied. Fixes, cheapest first: store tokens as **trivial structs** (kind, offset and length into the input bytes) in a contiguous array, so the loop touches no references; keep the hot code where the optimizer can see it, in one module or `@inlinable` across a framework boundary, which gives ARC optimization more room; and treat `Unmanaged`, which holds a reference without retaining it, as a measured last resort. Re-profile the Release build after each change.
code
swift · 31 linesfinal class Token {
let kind: UInt8
let next: Token?
init(kind: UInt8, next: Token?) { self.kind = kind; self.next = next }
}
func countStrings(from head: Token?) -> Int {
var count = 0
var current = head
while let token = current { // walks references node by node
if token.kind == 0x22 { count += 1 }
current = token.next
}
return count
}
struct TokenSpan { // trivial: no references inside
var kind: UInt8
var start: Int32
var length: Int32
}
func countStrings(in spans: [TokenSpan]) -> Int {
var count = 0
for span in spans where span.kind == 0x22 { count += 1 }
return count
}
var head: Token? = nil
for i in 0..<4 { head = Token(kind: i % 2 == 0 ? 0x22 : 0x7B, next: head) }
print(countStrings(from: head), countStrings(in: [TokenSpan(kind: 0x22, start: 0, length: 5)]))go deeper
Recall that ARC counts strong references to class instances and that copying a reference changes the count, which costs time.
Explain where retains come from in a loop: moving a reference along a chain, copying structs with reference fields, and calls the optimizer cannot see into.
Diagnose from a Release Time Profiler trace, then fix by representation first: trivial structs in contiguous storage, visible hot code, Unmanaged only with a proven lifetime. Confirm each change in the profile or the SIL.
Decide where a flat, index-based representation is worth its API cost, keeping it inside the hot module behind a convenient interface, and require profiles before anyone adopts Unmanaged.
## Where retain and release come from Swift manages class instances with **automatic reference counting (ARC)**: the compiler inserts a call to the runtime's `swift_retain` when a strong reference is copied and `swift_release` when a copy is no longer needed. Each is an update to the object's reference count, and in a loop that runs millions of times those updates show up as their own frames in Time Profiler. The optimizer removes retain/release pairs it can prove redundant, so `-O` executes far fewer of them than `-Onone`, but it must keep any pair it cannot prove safe to drop. Common sources in a parser's hot loop: - **Moving a reference along a chain.** `current = token.next` copies a reference into `current` (retain) and ends the old one (release), on every iteration. - **Copying values that contain references.** A struct is reference-counted when it contains a reference anywhere inside it, so copying a struct with a `String` field can retain the string's heap storage; a short string stored inline needs no count. - **Passing references into code the optimizer cannot see.** A call into a non-inlinable function in another module is opaque, so the optimizer has less freedom to remove retains and releases around it. - **Protocol-typed values.** A value of an unconstrained existential type is copied through its type's witness functions; a protocol constrained to `AnyObject` lets the compiler treat the value as a plain class reference. ## The linked-token trap The code example shows two ways to count string tokens. In the `-O` SIL for the linked version, the loop's back edge still contains a `retain_value` of the next node and a `release_value` of the previous one; the version over an array of `TokenSpan` structs has no reference counting in its loop at all, because `TokenSpan` contains only integers. Making `Token` a `final` class does not help: `final` changes how methods are dispatched, not how references are counted. | Representation | Per-step ARC in the loop | Memory layout | |---|---|---| | Linked `final class` tokens | Retain next, release previous | One heap object per token | | Array of structs with `String` fields | Retain when an element is copied | Contiguous elements, separate string buffers | | Array of trivial structs (kind, offset, length) | None | One contiguous buffer | ## Reducing the traffic Work from the representation outwards, re-profiling a Release build after each step: 1. **Make the hot data trivial.** Store each token as a struct of integers that point into the input bytes, and materialize a `String` only when a caller asks for one. This removes both the per-token allocation and the per-step reference counting. 2. **Keep it contiguous.** An `Array` of trivial structs is one buffer the loop reads sequentially; reading an element copies plain bytes. 3. **Let the optimizer see the code.** Keep the tokenizer's hot functions in the module that runs the loop, or mark the framework's small entry points `@inlinable`, so ARC optimization and inlining work across what used to be opaque calls. 4. **Constrain class-only protocols with `AnyObject`** when only classes conform, so values of that protocol type are single references. 5. **Avoid needless copies in the API.** Parameters are normally borrowed for the duration of a call, but storing an argument, capturing it in an escaping closure or returning it copies the reference; the lesson on noncopyable types and ownership covers the `borrowing` and `consuming` modifiers that make such transfers explicit. 6. **Last resort: `Unmanaged`.** `Unmanaged.passUnretained(_:)` wraps a reference without retaining it, so storing or passing the wrapper costs no count, but something else must keep the object alive for the whole use, for example `withExtendedLifetime` on the list head. `takeUnretainedValue()` turns it back into an ordinary strong reference, so the saving is limited to where the wrapper travels. A mistake reads freed memory, which ARC would have prevented. ## Confirming the fix - In Time Profiler, the `swift_retain` and `swift_release` frames under the tokenizer should shrink or disappear; profile with realistic input, because tiny samples hide per-element costs. - For a precise count, compile the file with `swiftc -O -emit-sil` and look for `retain_value`, `release_value`, `strong_retain` or `strong_release` inside the loop's blocks, or with `-S` and count calls to `swift_retain`. - Do not chase ARC in a Debug build: `-Onone` keeps many pairs that `-O` removes, so the numbers are not the ones users pay. The general rules of reference counting, such as when an object is freed and how lifetimes end, belong to the lesson on retain and release mechanics; this question is about what that bookkeeping costs in a loop and how to avoid paying it.
- Why doesn't turning Token into a struct with a String text field remove the traffic?A struct needs reference counting whenever it contains a reference, and a `String` longer than the small-string limit (15 UTF-8 bytes on 64-bit Apple platforms) holds a reference to a heap buffer. Copying the struct retains that buffer. Storing an offset and length into the input instead gives a trivial struct that copies as plain bytes.
- When is Unmanaged justified in a hot loop?Only after a Release profile shows the remaining ARC traffic matters and you can prove the lifetime. `Unmanaged.passUnretained(_:)` wraps a reference without retaining it, so another strong reference must keep the object alive throughout, for example `withExtendedLifetime` on the list head; `takeUnretainedValue()` hands back an ordinary strong reference again. Get the lifetime wrong and the loop reads freed memory.
- In a Release build the optimizer removes all ARC overhead.
- Structs never need reference counting, whatever their fields.
- Retain and release happen only when an object is created or destroyed.
- Marking the token class final removes its retains and releases.
- ARC runs on a background collector thread, so the hot loop does not pay for it.