ARC 在哪里写发布指令?
Where does ARC write the release instructions?
我知道每次按下键盘上的 CMD + B 时:
- Xcode 确实唤醒了 ARC
- ARC 分析我的代码并编写所有
retain/release/autorelease
调用
- 最终代码被LLVM编译
在此过程中还发生了一些事情,但是我要问的是...
ARC在哪里写发布指令?
- 就在引用 class 实例的变量 超出范围
之前
- 就在上次使用该变量
之后
例子
class HugeObject {
func doVeryImportantStuff() { print("The answer is \((10*4)+2)") }
}
func foo() {
let a = HugeObject()
a.doVeryImportantStuff()
// <-- Point A
let b = HugeObject()
b.doVeryImportantStuff()
// <-- Point B
}
ARC 将把 a.release()
行写到哪里?
- 在
Point B
?
- 或者,更好的是,能够理解
a.release
可以安全地移动到 Point A
?
我怀疑在足迹方面可能会有重要影响,但我找不到任何相关信息。
首先:ARC是编译器的一部分,它的名字是clang,不是LLWM。边编译边做。
给你的问题:
简短回答:这取决于您给编译器的注释。默认不算太早也不算太晚。
长答案:
从语义上讲,当局部变量的范围丢失时,将发送局部变量(局部范围,自动)的释放。但这是优化的。因此,从技术上讲,发布可能会提前发送,这意味着在本地 var 的最后一次可见使用和范围丢失之间。
如果你有理由保留和 extent 一样长的 retain,你必须注释 objc_precise_lifetime
。
In general, ARC maintains an invariant that a retainable object pointer held in a __strong object will be retained for the full formal lifetime of the object. Objects subject to this invariant have precise lifetime semantics.
By default, local variables of automatic storage duration do not have precise lifetime semantics. Such objects are simply strong references which hold values of retainable object pointer type, and these values are still fully subject to the optimizations on values under local control.
[…]
A local variable of retainable object owner type and automatic storage duration may be annotated with the objc_precise_lifetime attribute to indicate that it should be considered to be an object with precise lifetime semantics.
http://clang.llvm.org/docs/AutomaticReferenceCounting.html#precise-lifetime-semantics
我知道每次按下键盘上的 CMD + B 时:
- Xcode 确实唤醒了 ARC
- ARC 分析我的代码并编写所有
retain/release/autorelease
调用 - 最终代码被LLVM编译
在此过程中还发生了一些事情,但是我要问的是...
ARC在哪里写发布指令?
- 就在引用 class 实例的变量 超出范围 之前
- 就在上次使用该变量 之后
例子
class HugeObject {
func doVeryImportantStuff() { print("The answer is \((10*4)+2)") }
}
func foo() {
let a = HugeObject()
a.doVeryImportantStuff()
// <-- Point A
let b = HugeObject()
b.doVeryImportantStuff()
// <-- Point B
}
ARC 将把 a.release()
行写到哪里?
- 在
Point B
? - 或者,更好的是,能够理解
a.release
可以安全地移动到Point A
?
我怀疑在足迹方面可能会有重要影响,但我找不到任何相关信息。
首先:ARC是编译器的一部分,它的名字是clang,不是LLWM。边编译边做。
给你的问题:
简短回答:这取决于您给编译器的注释。默认不算太早也不算太晚。
长答案:
从语义上讲,当局部变量的范围丢失时,将发送局部变量(局部范围,自动)的释放。但这是优化的。因此,从技术上讲,发布可能会提前发送,这意味着在本地 var 的最后一次可见使用和范围丢失之间。
如果你有理由保留和 extent 一样长的 retain,你必须注释 objc_precise_lifetime
。
In general, ARC maintains an invariant that a retainable object pointer held in a __strong object will be retained for the full formal lifetime of the object. Objects subject to this invariant have precise lifetime semantics.
By default, local variables of automatic storage duration do not have precise lifetime semantics. Such objects are simply strong references which hold values of retainable object pointer type, and these values are still fully subject to the optimizations on values under local control.
[…]
A local variable of retainable object owner type and automatic storage duration may be annotated with the objc_precise_lifetime attribute to indicate that it should be considered to be an object with precise lifetime semantics.
http://clang.llvm.org/docs/AutomaticReferenceCounting.html#precise-lifetime-semantics