skip to content

When would you use Swift's ContiguousArray or Swift 6.2's InlineArray instead of Array, and what does each give up?

level: seniorimportance: nice to knowfreq 18%

answer

  1. NSArray-backed storage on Apple platforms
  2. class elements and bridging
  3. fixed count in the type
  4. no heap buffer, eager copies

basics

~20 s

ContiguousArray always uses contiguous native storage and never bridges to NSArray, which can help when Element is a class. InlineArray (Swift 6.2) stores a fixed count of elements inline, with no heap buffer, but copies eagerly and is not a Collection.

solid answer

~40 s

On Apple platforms an `Array` whose `Element` is a class or `@objc` protocol may be backed by a bridged `NSArray`, so the standard library says `ContiguousArray` can be more efficient and more predictable when you never pass the array to Objective-C; for structs and enums the two should perform alike. `InlineArray<count, Element>`, spelled `[4 of UInt8]` since Swift 6.2, puts its fixed number of elements directly in the enclosing value, avoiding a separate heap allocation. It gives up growth, copy-on-write (a copy copies every element, like a tuple) and `Sequence`/`Collection` conformance, so there is no `map`. It also needs a Swift 6.2 runtime, which on Apple platforms means macOS 26 or iOS 26.

code

swift · 19 lines
swift
@available(macOS 26.0, iOS 26.0, *)
struct ColorSwatch {
    var rgba: [4 of UInt8] = [0, 0, 0, 255]   // InlineArray<4, UInt8>
}

if #available(macOS 26.0, iOS 26.0, *) {
    var original = ColorSwatch()
    var edited = original                     // all four bytes copied now
    edited.rgba[0] = 255
    original.rgba[2] = 128
    print(original.rgba[0], edited.rgba[0])   // 0 255
    print(MemoryLayout<ColorSwatch>.size)     // 4: no pointer, no heap buffer
    print(MemoryLayout<[UInt8]>.size)         // 8: an Array is one buffer reference

    for i in edited.rgba.indices {
        print(edited.rgba[i], terminator: " ")
    }
    print()
}

go deeper

for a junior

Recall that Array is the default and that ContiguousArray and InlineArray are specialized: one avoids NSArray bridging, the other has a fixed size.

for a middle

Explain why Array may be NSArray-backed for class elements on Apple platforms and how InlineArray stores elements inline and copies them eagerly.

for a senior

Justify either type only with a measured hot path, and account for InlineArray's OS 26 availability and missing Collection conformance.

for a principal

Set a team default of Array and require profiling evidence before specialized array types enter shared code, given their API and availability costs.

## Three array types, one default `Array` is the right default almost everywhere. The standard library offers two specialized alternatives, and knowing when they help is a differentiator rather than a screening question. | | `Array` | `ContiguousArray` | `InlineArray<count, Element>` | |---|---|---|---| | Size | grows | grows | fixed by the type | | Storage | heap buffer, or bridged `NSArray` for class elements on Apple platforms | heap buffer, always contiguous | inside the enclosing value | | Copy | copy-on-write | copy-on-write | copies every element eagerly | | `Collection` conformance | yes | yes | no | | Since | always | always | Swift 6.2 | ## ContiguousArray: no bridging On platforms with the Objective-C runtime, `Array` is built on a buffer type that can wrap an `NSArray`. The source's own header comment says `Array<Element>` behaves like `ContiguousArray<Element>` when the element is not a reference type or an Objective-C existential; otherwise it may use an `NSArray` bridged from Cocoa for storage. That flexibility makes conversion to and from Cocoa cheap but costs the compiler certainty about representation. The `ContiguousArray` documentation states the trade-off: - It **always** stores elements in one contiguous region of memory. - If `Element` is a class or `@objc` protocol and you do not bridge to `NSArray` or pass the array to Objective-C APIs, `ContiguousArray` **may be** more efficient and have more predictable performance than `Array`. - If `Element` is a struct or enum, the two **should have similar efficiency**. Note the strength of those words: "may be" and "should", not a guarantee. On Linux and other platforms without the Objective-C runtime, `Array` is defined directly on the contiguous buffer, so the difference largely disappears. Reach for `ContiguousArray` in a measured hot loop over class instances, not by default; it is not bridged to `NSArray` at all, so code that passes it to Objective-C must convert. ## InlineArray: fixed size, no heap buffer Swift 6.2 added `InlineArray` (SE-0453) and the type sugar `[count of Element]` (SE-0483). Its declaration is `struct InlineArray<let count: Int, Element: ~Copyable>`, so the element count is part of the type, using integer generic parameters. The documentation summarises it: a fixed-size array that doesn't use a separate memory allocation just to store its elements, and whose copies copy all elements eagerly, like a tuple. What that means in practice: 1. **Where the bytes live.** As a stored property of a class it lives on the heap with the object's other properties; elsewhere it is, in general, on the stack. `MemoryLayout<[4 of UInt8]>.size` is 4, where an `Array` is a single 8-byte buffer reference on a 64-bit platform. 2. **Initialization.** Every element must be initialized, typically from an array literal: `var rgba: [4 of UInt8] = [0, 0, 0, 255]`. 3. **Access.** Subscripts are bounds-checked, and an out-of-range index is a run-time error; iterate with `indices`. 4. **No Collection.** The proposal deliberately does not conform `InlineArray` to `Sequence` or `Collection`, so `map`, `filter` and slicing are unavailable; calling `map` reports that the type `has no member 'map'`. 5. **Noncopyable elements.** It can hold `~Copyable` elements, in which case it is noncopyable itself; that ownership model is covered in the lesson on noncopyable types. Availability matters on Apple platforms: compiling it for an older deployment target reports `'InlineArray' is only available in macOS 26.0 or newer` (or `iOS 26.0`), so app code needs `@available` or `if #available` guards. ## Common misreadings - "`ContiguousArray` is always faster": the documentation only says it may be, and only for class or `@objc` protocol elements. - "`InlineArray` always lives on the stack": it is stored inline in whatever holds it, so as a stored property of a class it is on the heap. - "`InlineArray` is a cheaper `Array`": copies are eager, so a large one passed around by value costs more than an `Array`, whose copies share a buffer. ## Choosing - Use `Array` unless profiling says otherwise. - Use `ContiguousArray` for large arrays of class instances in hot paths that never touch Objective-C. - Use `InlineArray` for small, fixed-size data that is created and copied often, such as a pixel's four channels or a 3x3 matrix, where avoiding a heap allocation per value is measurable. A large `InlineArray` makes every copy expensive, the opposite of copy-on-write.

  • Why can't you call `map` on an `InlineArray`?
    `InlineArray` does not conform to `Sequence` or `Collection`; SE-0453 left those conformances out on purpose: because an `InlineArray` copies eagerly, generic collection algorithms and slicing would make many hidden full copies. It offers `indices`, `count` and subscripts instead, so you loop over `indices` or convert to an `Array` when you need the algorithms.
  • Is `ContiguousArray` faster than `Array` for an array of structs?
    Not meaningfully. The documentation says that when `Element` is a struct or enum the two should have similar efficiency, because `Array` never needs `NSArray` storage for such elements. The benefit is for class or `@objc` protocol elements on Apple platforms.

  • Recommending ContiguousArray everywhere as a free speed-up
  • Thinking InlineArray uses copy-on-write like Array
  • Expecting map and filter to work on an InlineArray
  • Using a large InlineArray as a value passed around and copied often
  • Shipping InlineArray in an app targeting iOS 18 without availability checks

Assumes Swift 6.4; InlineArray and its [count of Element] type sugar shipped in Swift 6.2 (SE-0453, SE-0483) and need macOS 26 or iOS 26 at run time on Apple platforms.