[swift-dev] Reducing array abstraction

Chris Lattner clattner at nondot.org
Sat Oct 7 01:06:15 CDT 2017


This question is somewhere between swift-dev and swift-users, not sure where best to post this.  

I’m working on a project that wants to get very low-abstraction penalty array operations, particularly with varargs.  Given the currently language limitations (no fixed size arrays), the best I’ve been able to come up with is something like this, where “lowlevelAPI” contains the heavy lifting (and is assumed to be opaque), and the “safeAPI” wrappers exist merely to provide a convenient safe wrapper for clients:

-------------- next part --------------
A non-text attachment was scrubbed...
Name: array_abstraction.swift
Type: application/octet-stream
Size: 412 bytes
Desc: not available
URL: <https://lists.swift.org/pipermail/swift-dev/attachments/20171006/74bfa474/attachment.obj>
-------------- next part --------------


Given whole module optimization of the program, we should in principle, be able to optimize this down to the equivalent of an LLVM array alloca in the clients, a few stores to it, then passing the pointers to the LLVM alloca into lowlevelAPI.  However, Swift is not doing this, not even with:

$ swiftc array_abstraction.swift -emit-sil -o - -O 

In this trivial case (with constant initializers) it does do the “array outlining” optimization, but this is no where near the abstraction level it could be.

Is there a better way to achieve this, and if not, is there any planned work (perhaps aligned with the ABI stability efforts) to improve vararg array performance to be able to avoid heap abstractions?  Any individual call to a vararg array is a known length, so it is a perfect candidate for a stack allocation.

-Chris




More information about the swift-dev mailing list