[swift-users] UnsafeMutablePointer on the stack?
jgroff at apple.com
Mon Oct 3 12:44:18 CDT 2016
> On Oct 3, 2016, at 10:20 AM, Jens Alfke via swift-users <swift-users at swift.org> wrote:
>> On Oct 2, 2016, at 5:14 PM, Mike Ferenduros via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
>> Personally I would be surprised if the malloc caused an actual measurable performance hit tbh.
> It won’t be noticeable against a call to SecRandom, as noted, but there are other circumstances where it’s a big performance hit to put a temporary buffer on the heap. (Right now I’m working on some rather performance-sensitive database code in C++ where avoiding a few heap allocations per iteration has proven to be a big win.)
> Does Swift have any solution for allocating stack-based array buffers?
There has been some work in the optimizer toward optimizing arrays onto the stack when they don't escape. In general, I would recommend sticking with the pointer allocate/deallocate methods if you need to directly control the destiny of the memory. If the allocate and deallocate occur unconditionally in the same function, it is possible to optimize the pair into a stack allocation. Using a local variable isn't a guarantee that you'll get stack memory, since closures may force the variable onto the heap, and furthermore subjects you to more aggressive semantic constraints on the underlying memory than you may want.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the swift-users