Vector
is not the same thing as InlineArray
.
Vector<3, Int>
consists of precisely 3 integers, like a homogeneous tuple, or the int3
vector type in a shader language, or int[3]
in C. This is not an Array
type in Swift, because it does not provide the operations we expect a Swift Array
type to provide.
InlineArray<3, Int>
is an actual Swift array type. It can contain 0, 1, 2, or 3 integer values. You can insert new ones at arbitrary offsets, or remove existing ones from it.
(The C programming language does not provide a built-in implementation of such an abstraction -- out of necessity, people build it from scratch, out of a base pointer, a capacity and a current count. InlineArray
packages these up into a proper type.)
Internally, a Vector
holds enough room to store the necessary amount of fully initialized elements, and nothing else.
Internally, an InlineArray
instance holds partially initialized storage of a certain capacity, along with metadata that describes which of its slots are initialized. The nature of this metadata varies between Inline*
types; in the case of InlineArray
, it consists of a single count
value. In InlineDeque
, it consists of a count
and startSlot
. In InlineSet
, it would consist of a full bitmap of occupied slots.
It is fair to argue that you do not care about this distinction, and you've never ever wanted to reach for either Vector
or InlineArray
. This is a valid point: the existing Array
is great, and provides an awesome, uniform model. If you are happy with its performance, please feel fully empowered to continue using it, indefinitely.
Unfortunately though, we need Swift to become usable in contexts where these details matter.
I believe that Vector
belongs in the core Standard Library, because it is destined to become a prominent currency type. It is intended that it will play the same role that is currently being played by homogeneous tuples, badly.
InlineArray
may or may not make sense to add to the core stdlib; I think it may make more sense to ship it in a module that needs to be explicitly imported. (InlineDeque
, InlineDictionary
etc. are in the same category.) Some people will want to frequently use it (and SmallArray
, SmallDeque
etc.) to avoid Array
's allocation overhead, but we don't need to shove these down everyone's throats -- there are many contexts where the performance benefits don't matter, or Array
is already the optimal solution.
The Swift project is making a concerted effort to push the language into areas it did not adequately serve before. We are working hard to fulfill the aspirations of the first sentence of its README
, that "Swift is a high-performance system programming language".
For the language to become a serious contender for use in low-level, high-performance, memory-constrained and/or realtime environments, we need to start making distinctions we intentionally avoided making before. This includes fracturing our existing, comfortable, universal constructs into a myriad highly specialized pieces. My drawing board includes InlineArray
, RigidArray
, SmallArray
and DynamicArray
as potential spawns of Array
; we will most definitely end up having to eventually productize some or all of these. These types enable exciting new use cases, but they are inherently a major break from our classic guiding principles, so for many existing uses of Swift, they will be nonsensical at best, or a major complication at worst. (For example, think what it will take to enable a noncopyable array variant like RigidArray
to serve as the underlying model for a SwiftUI list view.)
We are fundamentally changing the nature of Swift; the challenge is to do it in a way that doesn't overly harm our existing use cases. It must remain possible for a Swift programmer to ignore that InlineArray
exists.
Note that Vector
is not in this scary category, because it isn't a spinoff of Array
; rather, it is a rethinking of homogeneous tuples. Vector
is not an array type in the sense that Swift is using this term.
We have plenty of Array
types, and lots more are coming; but Vector
is not one of them.
Vector
is not an Array
.