swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.39k stars 10.34k forks source link

[SR-9701] Stack allocated Objective-C no escape closures #52141

Open aschwaighofer opened 5 years ago

aschwaighofer commented 5 years ago
Previous ID SR-9701
Radar None
Original Reporter @aschwaighofer
Type Improvement
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Improvement | |Assignee | None | |Priority | Medium | md5: fd60c5edf6ddc3d03a27ac1f2a14393b

Issue Description:

When we pass closures to @noescape objective c block arguments the closures are allocated on the heap. We should also stack allocate them.

belkadan commented 5 years ago

The Objective-C convention is actually fine with you stack-allocating all block arguments; it's considered the callee's responsibility to Block_copy them. cc @rjmccall

https://clang.llvm.org/docs/BlockLanguageSpec.html
https://clang.llvm.org/docs/AutomaticReferenceCounting.html#blocks

rjmccall commented 5 years ago

Correct. We don't do this only because we're being cautious about lifetimes. If we had a reliable way of detecting that the copy is unnecessary, it would be a significant win.

Clang has actually implemented an optimization where it eliminates the copying logic from `noescape` blocks entirely, essentially generating them as global blocks that happen to be allocated on the stack.