[swift-evolution] [Pitch] Swift needs fixed-size array
davesweeris at mac.com
Mon Apr 17 13:37:47 CDT 2017
> On Apr 17, 2017, at 10:52 AM, Anders Kierulf via swift-evolution <swift-evolution at swift.org> wrote:
> Swift needs a datatype that contains a fixed number of a given type; basically a simple fixed-size array.
> Motivation: I’ve been porting code for Monte Carlo Tree Search in my Go-playing program from C++ to Swift. Performance is crucial for this code, as more simulations lead to better play. After the initial port, the Swift code was more than 10x slower than my C++ code. After several weeks of optimizing, profiling, and digging through disassembly, I’ve gotten to within a factor of 2. Most of that gain came from using the ugly workaround of importing fixed-size arrays from C.
> My app is designed for a 19x19 (or smaller) Go board, not an arbitrary N x N board, so I don’t want to pay the high cost of variable-size data structures in the lowest levels of my app. Most apps are not like this, and most of my app is not, but this kernel of my app needs to be fast. Heap allocations, reference counting, and indirections all slow down the code. I just need a fixed size of memory that I can access like an array, and Swift doesn’t let me do that.
> Workaround: By importing an array from C, I can allocate a blob of memory on the stack or include it in a struct. I can then use UnsafeRawPointer to access that blob like an array (see details in SR-4548). This is ugly, but it works, and it is much faster than using a Swift array. However, I’m stymied by SR-4542, which causes mutability to spread like a plague through client code.
> (SR-4542: Calling a function taking an UnsafeRawPointer forces the parameter to be passed as inout, which means the method must be mutating. UnsafeMutableRawPointer should require inout, UnsafeRawPointer should not.)
> Proposal: UnsafeMutablePointer almost provides what I need. However, it can only allocate memory on the heap, or it can take a given blob of memory and interpret it as something else. What’s missing is a way to allocate typed memory of a certain size on the stack or in a struct. For example, something like this, with support for subscripts, limited to value types:
> var foo = UnsafeMemory<Int64>(count: 6)
> var bar = FixedSizeArray<UInt32>(repeating: 0, count: 380)
> (1) C arrays are currently imported as tuples, so extending tuples with subscripts and adding a way to create tuples with a specific count of the same type could address this need. However, I don’t think this fits well with the concept of tuples.
> (2) Changing the Array type to allow a fixed size could be considered in the future. ‘count’ and ‘capacity’ would be fixed and only known to the compiler, not stored with the data. However, I suspect the consequences would be far-reaching, and thus I don’t see this happening soon.
> An UnsafeMemory type would be a limited addition that fits in well with the existing low-level Pointer module, and fills a gap in the capabilities of Swift. The Pointer module helps implement low-level, performance-critical code, and not being able to create data on the stack is a serious omission. Jumping through C hoops is not a solution.
Up until 30 seconds ago, I'd thought there was a way to initialize an array with a pre-existing buffer. Apparently not, though.
There’s currently no way to restrict a generic parameter to be a value type. I suspect this is at least partly because `struct` doesn’t make any promises about whether its properties have value or reference semantics (but unless I’m forgetting something, that’s just a guess on my part). The topic of an exclusively value-semantics type, “simplestruct” for lack of a better term, has come up at least once. IIRC it was deemed to not provide enough of a benefit to justify the added language complexity (although I have a vague memory that it was only brought in connection with another topic, as it is here, so maybe if it was its own proposal the results would been different). Personally, I don’t think it adds all that much complexity (from the developer's PoV… dunno about the compiler), and barring some other reason would be in favor of it. That’s just one opinion, though (and one of someone who’s not all that experienced, as well).
Both of your alternatives have been brought up before. My recollection is we tentatively* decided that it’d be better to add fixed-size arrays and change how C arrays are imported, but that conversation was a *long* time ago, before we were as worried about backwards compatibility. Neither alternative has come to a formal proposal, though, because they were deemed out-of-scope** before things got that far. I don’t think either has come up since phase 2 started, though... Maybe it’d be worth revisiting the ideas?
- Dave Sweeris
* because of **
More information about the swift-evolution