> On Oct 3, 2016, at 10:20 AM, Jens Alfke via swift-users > <swift-users@swift.org> wrote: > > >> On Oct 2, 2016, at 5:14 PM, Mike Ferenduros via swift-users >> <swift-users@swift.org <mailto:swift-users@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. -Joe
_______________________________________________ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users