[Pitch] RigidArray and UniqueArray

It's mitigated somewhat by the fact that it's a property, which would be exceedingly unusual a spelling for something if it had the side effect of freeing (all) capacity. That'd be an actively malicious API :angry_face_with_horns:

That said, since the type introduces the terminology isFull, this could be described as unfilledCapacity.


On the topic of specific APIs, though, I worry about this one and similar ones:

  public mutating func insert(
    moving items: inout OutputSpan<Element>,
    at index: Int
  )

To me, insert(moving:at:) reads as though the at refers to the index in items, the container elements are being moved from, rather than the container elements are being moved to. Perhaps insert(moving:to:)? (Yes, there's something non-ideal that we're losing the idiomatic "at" meaning an index.)

1 Like

OutputSpan also has an isfull property, so I think that ship has sailed.

The following isn’t a comment about the exact shape of this proposal, but a related note about enabling developers to use these types without frequent pain + interface mismatches:

I think a crucial part of being able to effectively use these new noncopyable variants of things within the broader ecosystem is the larger adoption of Span/MutableSpan (and perhaps BorrowingSequence + future inout variant) as the definitive currencies for “I need temporary [immutable/mutable] access to some [contiguous/maybe-noncontiguous] sequence of elements”.

My understanding is that historically the guidance has been “if you need access to some ordered data, do the friendly thing for your callers and take an Array (or maybe Sequence)". There are surely huge amounts of existing APIs that take Array, where if re-written today, they would/should use [Mutable]Span.

I hope that these new types come along with some new developer guidance in the form of documentation/talks recommending that existing APIs, especially ones that may be used in performance sensitive contexts, should strongly consider defining new overloads of their interfaces which take spans instead of arrays where applicable.

I think this would also help alleviate some of the concern expressed in this thread around it being a massive issue to make these be new types instead of adding them into Array. Much like normal protocol-oriented design, encouraging interfaces to take spans will free up callers to use whatever owned representation makes the most sense for their use case.

5 Likes

While many codebases have been updated to Swift 6, they often still follow idioms from Swift 3/4. One such example is the use of inout Array, which was the primary method for mutating a bunch of ordered elements at those days.

I agree that we need more clear guidance for developers on how to use [Mutable]Span types effectively, as well as these upcoming collections.

There is a great paper Swift for C++ Practitioners, Part 1: Intro & Value Types | Doug's Compiler Corner .
I strongly feel something similar for Swift developers could be incredibly valuable.

With the introduction of Rigid/Unique variants for collections like Array , Set , and Dictionary , alongside Span types and Slices, it’s important that we provide clear and consistent guidance. This is especially relevant when we consider types like Array<~Copyable> and UniqueArray<~Copyable>, which appear the same at first glance but have subtle, important differences.

I do hope that we never had any guidance suggesting to just take an Array. The friendly thing for callers is for functions to be generic over a protocol that expresses the abstraction they need.

To me it feels rude to define a function that simply wants to reorder some items as taking an inout Array: it would force clients to materialize an Array just to be able to call it. Such function definitions are fine as incidental implementation details, but they do not make for a great public interface.

There will always be room to define some functions to take individual concrete types like Array or RigidDeque, of course -- especially if they want to consume the argument to store it in their own representation. But it is a basic engineering principle to define as flexible interfaces as possible. In many cases, the concrete definitions should just be overloads of generic functions that define the real interface, as an optimization measure.

I do hope that taking inout some MutableCollection<Foo> is how we primarily allow a function to mutate a bunch of ordered elements in Swift.

extension Foo {
  func jiggle(_ items: inout some MutableCollection<Int>) { ... }
}

Array is just one embodiment of a mutable collection type; ContiguousArray, ArraySlice, UnsafeMutableBufferPointer in the stdlib, and Deque and BitArray from swift-collections are prominent examples of others.

Note that MutableCollection does not just express the mutation of a single contiguous memory region -- it also allows conforming types (1) to have discontiguous storage (as is the case with Deque), or (2) to materialize their contents on demand (as is the case with BitArray).

Taking an inout MutableSpan only allows mutating a single contiguous memory region; this is the modern equivalent of taking an UnsafeMutableBufferPointer. Crucially, MutableSpan is memory safe, and that turns this classic pattern from an awkwardly discouraged risky/unsafe shortcut into a fully viable, first-class API design pattern:

extension Foo {
  // BAD, unsafe interface
  func unsafeShuffle(_ items: UnsafeMutableBufferPointer<Int>) { ... }

  // GOOD, safe interface, expressing the same
  func shuffle(_ items: inout MutableSpan<Int>) { ... }
}

Taking a MutableSpan is not replacing the idea of taking a MutableCollection. It "just" allows a crucial high-performance design pattern (taking a reference to a single, mutable memory region) to be expressed with a safe interface that we can wholeheartedly recommend to people.

(The ownership-aware analogue of MutableCollection is probably going to be something like the MutableContainer prototype in swift-collections. That prototype allows discontiguous storage (roughly along the lines of BorrowingSequence), but it doesn't allow on-demand generation (a container has to own its contents; it isn't a factory). Sadly it is too early to talk about details here; they are too much in flux.)

1 Like

Separate from the other discussion, what happens to the versions of these collections in the swift-collections package? Will they be kept? Will they be synced with any changes to the standard library version? Since these types won't back deploy, keeping them somewhere to extend their support would be very appreciated. Otherwise they'll fall into the same bucket as InlineArray and other standard library additions: used by Apple internally but not by the community because few people can afford to only deploy to the latest OS versions.

2 Likes

Agreed for non-resilient libraries/code, but for evolution-enabled libraries where there’s no specialization this can give pretty awful performance. I know there’s the withContiguousStorageIfAvailable thing which mitigates this, but that can be easy to forget and also can fallback to really bad performance when not available/used.

Though maybe this won’t be nearly as much of a problem with the future sequence types given the design direction of having them vend the concrete [Mutable]Span types as their currency as noted here SE-0516: Borrowing Sequence - #9 by ben-cohen