[SR-7286] non-escaping closures aren't stack allocated #49834
Labels
bug
A deviation from expected or documented behavior. Also: expected but undesirable behavior.
compiler
The Swift compiler in itself
performance
SILOptimizer
Area → compiler: SIL optimization passes
Attachment: Download
Additional Detail from JIRA
md5: e7067f5ef2bdbe07d6a03ec02a586cdf
duplicates:
Issue Description:
Even non-escaping closures aren't stack allocated which kind of defeats their purpose. Take the following SwiftPM project:
(also attached as .tar.gz)
when run with
I'd expect it to first print
100000
(the counter value) and then O(1) allocations which should be very few. In reality it looks like this though:which are 100027 heap allocations! Which is unexpected.
We hit this problem in the real world in a NIO PR: https://github.com/apple/swift-nio/pull/242/files
We needed to add
@_inlineable
to thewithLock
functions in order for them to not heap allocate 🙁. In this case just making it inlineable fortunately works as the methods are small enough so that the whole thing is inlined and the allocation therefore elided.Tested under
With the latest 4.1 dev snapshot it's only one allocation less:
The text was updated successfully, but these errors were encountered: