Closed weissi closed 5 years ago
@swift-ci create
Fixed with: https://github.com/apple/swift/pull/21933
$ sudo dtrace -n 'pid$target::malloc:entry { @malloc_calls = count(); } :::END { printa(@malloc_calls); } ' -c ./.build/x86_64-apple-macosx/release/nectc
dtrace: system integrity protection is on, some features will not be available
dtrace: description 'pid$target::malloc:entry ' matched 5 probes
100000
dtrace: pid 62508 has exited
CPU ID FUNCTION:NAME
6 2 :END
48

You made my day 🙂
Attachment: Download
Additional Detail from JIRA
| | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, Optimizer, Performance | |Assignee | None | |Priority | Medium | md5: e7067f5ef2bdbe07d6a03ec02a586cdfduplicates:
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: