[swift-evolution] Proposal for Passing Arrays to Variadic Functions
clattner at apple.com
Tue Dec 15 00:00:49 CST 2015
On Dec 14, 2015, at 1:13 PM, Kevin Ballard <kevin at sb.org> wrote:
>> A similar optimization could be done when passing down an array literal, but the calling convention would have to support passing down an array value obtained in other ways (e.g. passed down to the caller or loaded from memory). In that case, it is hard to beat the performance of a single retain: no copy of the elements are implied. Doing the optimization for general unknown number of elements also isn’t profitable in general because the stack is a very finite resource, so you’d have to handle the “large” case somehow.
> Oh sure, when I said doing it in the general case, I just meant for arrays created from array literals (or possibly even arrays constructed using .append(), if the optimizer really wants to go ahead and figure out how many elements the resulting array has). And I was thinking that the calling convention is just pass an array, and instead Array will have the flag internally in its native storage that says "my buffer is on the stack" (as well as having a special method to construct an array from a stack buffer that the compiler will use for array literals / varargs). The biggest complication is having the compiler insert calls to array.moveToHeapIfCurrentlyOnStack() whenever an array parameter escapes the function (arrays returned from functions can't be on the stack, and arrays created in the function can skip the stack if it's known to escape), and the question there is whether this call will have meaningful performance implications.
Ah, I understand the model you’re envisioning.
We’re *extremely* reticent to make the hot paths of Array any slow (things like subscript for example), particularly when you may be talking about [Float] or [Int], because those are types that are important to get vectorized. Right now, the only slowdown vs C that you get on those hot paths on arrays of structs are the array bounds check, and that check is often hoisted out of loops.
More information about the swift-evolution