# Vector, a fixed-size array

**URL:** <https://forums.swift.org/t/vector-a-fixed-size-array/75264>\
**Category:** Pitches\
**Tags:** array\
**Created:** [October 10, 2024, 1:18am UTC](https://forums.swift.org/t/vector-a-fixed-size-array/75264 "2024-10-10T01:18:30Z")\
**Posts on this page:** 20\
**Page:** 8

<div class="post-metadata">

**Author:** ![scanon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/scanon/32/2445_2.png) [@scanon](https://forums.swift.org/u/scanon)\
**Post date:** [October 17, 2024, 3:01pm UTC](https://forums.swift.org/t/vector-a-fixed-size-array/75264/143 "2024-10-17T15:01:23Z")

</div>

Sorry, that was the result of a sloppy edit while writing in the browser; it should say "`dot(a, a) == 0` with `a` non-zero" (Consider `a = [1, i]`).

(Originally I had something like "dot(a, b) == 0 with a and b non-orthogonal" but it's hard to make that objection formal because people commonly _define_ "orthogonal" as "the dot product is zero")

---

<div class="post-metadata">

**Author:** ![GreatApe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/greatape/32/2640_2.png) [@GreatApe](https://forums.swift.org/u/GreatApe)\
**Post date:** [October 17, 2024, 6:41pm UTC](https://forums.swift.org/t/vector-a-fixed-size-array/75264/144 "2024-10-17T18:41:50Z")

</div>

Yeah afterwards it hit me that you probably meant exactly the `||(1,i)||` case

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [October 18, 2024, 1:05am UTC](https://forums.swift.org/t/vector-a-fixed-size-array/75264/145 "2024-10-18T01:05:25Z")

</div>

While it might never happen but it would be amazing if through some compiler magic the type was simply an evolution of `Array` itself.

```swift
- Array<T> or Array<T, 0…> dynamic array
- Array<T, 1…> non empty array
- Array<T, 4> or Array<T, 4 … 4> fixed size array
- Array<T, 0 ..< N> an array with min and max amount of values

```

Basically the secondary parameter becomes a range that covers all cases.

---

<div class="post-metadata">

**Author:** ![RandomHashTags](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/randomhashtags/32/24259_2.png) [@RandomHashTags](https://forums.swift.org/u/RandomHashTags)\
**Post date:** [October 18, 2024, 7:23am UTC](https://forums.swift.org/t/vector-a-fixed-size-array/75264/146 "2024-10-18T07:23:25Z")

</div>

+1. If it were up to me this is the way I'd implement it, at the compiler level.

---

<div class="post-metadata">

**Author:** ![intswift](https://avatars.discourse-cdn.com/v4/letter/i/7cd45c/32.png) [@intswift](https://forums.swift.org/u/intswift)\
**Post date:** [October 18, 2024, 10:29am UTC](https://forums.swift.org/t/vector-a-fixed-size-array/75264/147 "2024-10-18T10:29:31Z")

</div>

I really like this idea. It makes me think, though, whether instead of giving a size of the array it could be expressed as a range of indices. So:

```swift
Array<T, 0..<5> // Array with 5 elements from [0] to [4].
Array<T, 7...10> // Array with 4 elements from [7] to [10].

```

which then brings us onto the question of slices. 😀 If a fixed array were expressed in terms of its indices, then would we need a separate fixed-array slice type, or could multiple fixed arrays with differing but overlapping indices not (as an option) share a single underlying array?

As for the bike-shedding of names, "RigidArray" as mentioned earlier by Diggory gets +1 from me. I think it gives a good mental impression of what it is, and the term "rigid" could be a useful term of art to extend to other types too (e.g. "RigidSet", "RigidDictionary").

---

<div class="post-metadata">

**Author:** ![ibex](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ibex/32/30439_2.png) [@ibex](https://forums.swift.org/u/ibex)\
**Post date:** [October 18, 2024, 12:33pm UTC](https://forums.swift.org/t/vector-a-fixed-size-array/75264/148 "2024-10-18T12:33:55Z")

</div>

> [@intswift](#):
>
> ```swift
> Array<T, 0..<5> // Array with 5 elements from [0] to [4].
> Array<T, 7...10> // Array with 4 elements from [7] to [10].
> 
> ```

That will become harder to read in real life. 🙂

Compare this:

```swift
// 1 D
Array<T, 0..<5>

// 2 D
Array<Array<T, 0..<5>, 0..<5>

// 3 D
Array<Array<Array<T, 0..<5>, 0..<5>, 0..<5>

```

and this:

```swift
// 1 D
Array<5, T>

// 2 D
Array<5, Array<5, T>>

// 3 D
Array<5, Array<5, Array<5, T>>>

```

Which one is easier to grasp?

---

<div class="post-metadata">

**Author:** ![duan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/duan/32/40252_2.png) [@duan](https://forums.swift.org/u/duan)\
**Post date:** [October 18, 2024, 6:27pm UTC](https://forums.swift.org/t/vector-a-fixed-size-array/75264/149 "2024-10-18T18:27:14Z")

</div>

Since I didn't find this on my reading: as a property of a class on the heap, would `Vector` incur reference counting?

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [October 18, 2024, 6:33pm UTC](https://forums.swift.org/t/vector-a-fixed-size-array/75264/150 "2024-10-18T18:33:26Z")

</div>

Vector itself would not, since its elements are stored in-line. The class may incur reference counting.

`Vector<5, T>` is like if you wrote 5 Ts inline as stored properties of the class.

---

<div class="post-metadata">

**Author:** ![duan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/duan/32/40252_2.png) [@duan](https://forums.swift.org/u/duan)\
**Post date:** [October 18, 2024, 6:36pm UTC](https://forums.swift.org/t/vector-a-fixed-size-array/75264/151 "2024-10-18T18:36:00Z")

</div>

Since this came up in the thread, imagining `Vector` as the backing storage for tensors, a few more questions:

1. Is there any way, other than making `Vector` a property of a class, to force `Vector` to exist on the heap? Heap is usually the better place to load in 1s-100s gigabytes of weights.
2. It's common to use mmap to load large weights from disk, and use pointer arithmetic to assign the interesting stored locations in the weights to the buffer. This can all be done with the `UnsafePointer` & friends pointing at any sort of contagious memory today. Seems like it's not possible to use `Vector` to load weights from disk this way?

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [October 18, 2024, 6:41pm UTC](https://forums.swift.org/t/vector-a-fixed-size-array/75264/152 "2024-10-18T18:41:01Z")

</div>

It sounds like you probably want a `Span` view over mapped memory, or even just a standard `Array`, rather than a `Vector`.

For very large vectors, you’ll have to be careful to manage copying.

For a matrix type you could still use generic integer parameters to enforce shape compatibility at compile-time, even if they use one of these other storage types.

---

<div class="post-metadata">

**Author:** ![Alejandro](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alejandro/32/46197_2.png) [@Alejandro](https://forums.swift.org/u/Alejandro)\
**Post date:** [October 18, 2024, 6:44pm UTC](https://forums.swift.org/t/vector-a-fixed-size-array/75264/153 "2024-10-18T18:44:57Z")

</div>

> [@duan](#):
>
> Is there any way, other than making `Vector` a property of a class, to force `Vector` to exist on the heap? Heap is usually the better place to load in 1s-100s gigabytes of weights.

Yeah, just manually allocate it on the heap.

```swift
let ptr = UnsafeMutablePointer<Vector<2, Int>>.allocate(capacity: 1)

ptr.initialize(to: Vector(repeating: 0))

```

This is also equivalent to just allocating a pointer of `Int` where the capacity is 2.

It would be nice to have some `Box`/`Unique` type that does this unsafety for you and exposes a safer interface.

> [@duan](#):
>
> Seems like it's not possible to use `Vector` to load weights from disk this way?

Well `mmap` returns a pointer to raw memory anyway. Your best bet would be to convert it to a `Span` which provides the safe interface of interacting with this memory. If you need to mutate this memory, then a hypothetical `MutableSpan` would be your go to. `Vector` is an instance of contiguous `Element`s, not a view into them.

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [October 18, 2024, 6:58pm UTC](https://forums.swift.org/t/vector-a-fixed-size-array/75264/154 "2024-10-18T18:58:23Z")

</div>

Fixed-size arrays are really far, far less generally useful than I think some people here are imagining. They're an important tool for certain specific kinds of low-level programming, and they'll give us a much more natural solution for importing array fields from C, and that's about it. If you have even a few thousand elements in an array — and it could be much fewer depending on the element — the heap allocation costs of `Array` are going to be dominated by the cost of initializing the elements.

If you're concerned about heap-management costs of `Array`, like retains and releases, you need to understand that those are triggered by copying or destroying the array value. So the retains and releases will go away with a fixed-size array, but they'll be replaced by doing separate copies and destructions of all the array elements, which is almost strictly more expensive than a single retain or release. Avoiding array copies is important sometimes, but the critical tool we're working on for that is `Span`, not fixed-size arrays.

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [October 18, 2024, 8:01pm UTC](https://forums.swift.org/t/vector-a-fixed-size-array/75264/155 "2024-10-18T20:01:51Z")

</div>

> [@Alejandro](#):
>
> It would be nice to have some `Box`/`Unique` type that does this unsafety for you and exposes a safer interface.

If we also had a reference counted Box for copyable payloads, we could re-imagine `indirect` enum cases as just syntax sugar for the appropriate kind of wrapper, depending on whether the payload is copyable or not.

---

<div class="post-metadata">

**Author:** ![duan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/duan/32/40252_2.png) [@duan](https://forums.swift.org/u/duan)\
**Post date:** [October 18, 2024, 8:07pm UTC](https://forums.swift.org/t/vector-a-fixed-size-array/75264/156 "2024-10-18T20:07:42Z")

</div>

To put some of these previous conversations about matrices, dot products etc in perspective: `Vector` as backing storage for these things are fine as supporting evidences for the naming/shape discussions. In real world, fixed-sized array serves, at best, as a partial solution to this problems, in the sense that it can be part of something like `llvm:SmallVector` for a CPU backend of a tensor library.  
(I'm not interested in the naming discussion at the slightest, but I suppose this amounts to pouring some cold water on all the matrix talk 😄)

Overall I'm very excited to no longer have to deal with C buffers imported as tuples!

---

<div class="post-metadata">

**Author:** ![Alejandro](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alejandro/32/46197_2.png) [@Alejandro](https://forums.swift.org/u/Alejandro)\
**Post date:** [October 18, 2024, 10:33pm UTC](https://forums.swift.org/t/vector-a-fixed-size-array/75264/157 "2024-10-18T22:33:19Z")

</div>

@lorentey has been prototyping a `Shared` variant of this that does just that. Then we'd have `std::shared_ptr` and `std::unique_ptr` 🙈

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [October 18, 2024, 10:38pm UTC](https://forums.swift.org/t/vector-a-fixed-size-array/75264/158 "2024-10-18T22:38:04Z")

</div>

> [@John\_McCall](#):
>
> Avoiding array copies is important sometimes, but the critical tool we're working on for that is `Span`, not fixed-size arrays.

Just to add to this - you can already avoid array copies by using `borrow` parameters to suppress implicit copying.

Unfortunately, lots of operations require copies when they really shouldn't (like `for` loops):

```swift
// Why do I have to copy an Array to iterate over its contents?

func foo(_ input: borrowing [Float]) {
           `- error: 'input' is borrowed and cannot be consumed

  for _ in input {
           `- note: consumed here
  }
}

```

For a `Vector`, this implicit copy (made visible by the explicit `borrowing` modifier) would create an entirely new buffer and initialise it from the original `Vector`. So if the vector is very large, the very act of writing a `for` loop could have a significant implicit performance cost, regardless of what you do in the loop. This is an artefact of the `Collection` design that will hopefully be remedied by a new protocol. (Actually, this is also a good argument against having Vector conform to Sequence/Collection at all)

The ability to have `borrow var` variables which we can store and write abstractions around (using lifetime dependencies) will also help avoid copies.

`Vector` and `Array` can both benefit from these improvements.

* * *

**EDIT:** To phrase the point on copying another way - currently `for` loops use _consuming iteration_, via Sequence's `consuming func makeIterator()` method.

That works for single-pass sequences, and until now we've been using it for multi-pass collections because we can just copy them and consume the copy. For copy-on-write collections, this is cheap (just a retain/release).

We already know this is insufficient for non-copyable collections, and so we will need to introduce _borrowing iteration_ for them (all collections can already do this via their indexes). The part that is interesting with regards to `Vector` is that even though it is copyable, because its elements are stored inline the implicit copies necessary for consuming iteration may be surprisingly expensive.

It means every `for` loop involving a vector will have linear complexity with respect to the length of the vector from that initial copy, even if they `break` out of the loop body early.

---

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [October 19, 2024, 6:14pm UTC](https://forums.swift.org/t/vector-a-fixed-size-array/75264/159 "2024-10-19T18:14:31Z")

</div>

The Language Steering Group discussed this pitch briefly and wanted to provide some guidance for the pitch discussion. This has been a very active thread with a lot of good feedback provided. Much of the back and forth has centered on naming, and, while the name that we ultimately pick for this type is important, we do want to make sure that traffic about the name does not overwhelm other topics. The name can be decided on largely independently of the fundamental functionality this type provides.

Regarding the naming discussion, the LSG strongly encourages community members to strive to make their commentary amount to more than just a +1 or a -1 for particular names (as many are doing already). Most important are the _reasons_ for selecting a particular name rather than the number of people speaking for or against any of the options. Additionally, for those who argue that `Vector` is not the appropriate name, it remains very helpful to suggest or argue for concrete alternatives rather than simply speak against `Vector`.

---

<div class="post-metadata">

**Author:** ![saagarjha](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@saagarjha](https://forums.swift.org/u/saagarjha)\
**Post date:** [October 20, 2024, 3:17am UTC](https://forums.swift.org/t/vector-a-fixed-size-array/75264/160 "2024-10-20T03:17:41Z")

</div>

> [@Alejandro](#):
>
> This pitch isn't making any current behavior better or worse with regards to imported C array initialization.

The specific concern I have is that you can iterate over one of these in a for loop and easily run into unitialized elements. Which is the case for, say, `UnsafeBufferPointer` but that is pretty clearly annotated as such.

---

<div class="post-metadata">

**Author:** ![lorentey](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lorentey/32/1299_2.png) [@lorentey](https://forums.swift.org/u/lorentey)\
**Post date:** [October 21, 2024, 10:49pm UTC](https://forums.swift.org/t/vector-a-fixed-size-array/75264/161 "2024-10-21T22:49:50Z")

</div>

> [@dimi](#):
>
> I wholehartedly agree here, which is which I proposed `HomogenousTuple`

I have the same objection as before: we are already using the term "homogeneous tuple" to mean, well, a homogeneous tuple. `Vector` is like that, but it is distinct from it.

This is in addition to the (to me, quite obvious) problem that `HomogeneousTuple` is not a name -- it's a dictionary definition masquerading as a name. `Vector` is a core construct, along the likes of `Array`, `Bool`, `String`, `Slice` or `Set`; it deserves to have a name that matches its importance.

> [@saagarjha](#):
>
> The specific concern I have is that you can iterate over one of these in a for loop and easily run into unitialized elements. Which is the case for, say, `UnsafeBufferPointer` but that is pretty clearly annotated as such.

Can you provide a concrete example of this kind of unsafety to illustrate this concern?

(In general, I personally expect C interop to be inherently unsafe, _especially_ any code that touches C pointer types. To me, it seems self evident that Swift code that wants restricts itself to safe interfaces should not be allowed to directly call C functions. Importing C's vector types as `Vector` instead of homogeneous tuples does not seem to move the needle on this.)

---

<div class="post-metadata">

**Author:** ![phoneyDev](https://avatars.discourse-cdn.com/v4/letter/p/49beb7/32.png) [@phoneyDev](https://forums.swift.org/u/phoneyDev)\
**Post date:** [October 21, 2024, 11:34pm UTC](https://forums.swift.org/t/vector-a-fixed-size-array/75264/162 "2024-10-21T23:34:22Z")

</div>

I would also like to see this become a fixed size array:  
`let fixedArray = [1, 2, 3, 4]`  
with whatever performance benefits can accrue to a stack-allocated or ivar `Array`.

For me this is a common idiom where a constant array contains the single source of truth for something. In fact when I read fixed-size array this is what I was expecting to see.

[Previous page](https://forums.swift.org/t/vector-a-fixed-size-array/75264.md?page=7)

[Next page](https://forums.swift.org/t/vector-a-fixed-size-array/75264.md?page=9)
