迅速.无主相对于弱性能的(绝对)唯一的特定优势是什么? [英] Swift. Is the (absolutely) sole specific advantage of unowned over weak, performance?

查看:39
本文介绍了迅速.无主相对于弱性能的(绝对)唯一的特定优势是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Swift 中,我们有正常的默认输入

In Swift, we have normal default typing

  • 对象根本不能变为nil.

我们有弱类型

  • 对象可以变为 nil.如果对象变为nil,你的指针自动变为nil,所以你知道对象变为nil

我们有无主打字

  • 对象可以变为 nil.如果对象变为 nil,则您的指针不会发生任何变化 - 如果您尝试使用它,您就搞砸了

(因此:推论:唯一可以使用无主"的情况是,如果您绝对知道"该对象将永远不会变为 nil.)

(So: by corollary: the one and only time you can use "unowned" is if you "absolutely know" the object will never become nil.)

现在:

在我看来,下面这句话,

It seems to me that the following sentence,

绝对是真的...

绝对我的意思是,真的,真的,绝对,直到最深层次的哲学问题真的......

and by absolutely I mean, really, truly, absolutely, down to the deepest possible philosophical concerns true...

因此逻辑推论:

{另外 - 我能想到的唯一其他区别是在自我记录的意义上.如果我使用 unowned,它会提示我的开发者同事做某些事情;让我们暂时搁置这个问题.)

{Aside - the only other difference I can think of is in the self-documenting sense. If I use unowned, it cues my fellow developers to certain things; let us set aside that issue for now.)

所以我的问题很简单,非常准确,非常具体:上面的粗体句子是true"吗?(在完全、非常、非常"真实的真实意义上).

So my question is straightforward, very exact, very specific: are the bold sentences above "true" (in the "utterly, very, spectacularly" true sense of true).

推荐答案

我同意 Yannick 的观点.你的大胆陈述是不正确的.无主引用必须在其生命周期内有效.在 -Ounchecked 程序中,未能保持此先决条件是未定义的行为.我的意思不是它崩溃了".我的意思是它不是一个结构良好的程序;它的作用是不确定的.即使在 -Ounchecked 下,弱引用也不会由于其发布而产生未定义的行为.

I agree with Yannick. Your bold statements are not correct. An unowned reference must be valid for its lifetime. In an -Ounchecked program, failure to maintain this precondition is undefined behavior. I don't mean "it crashes." I mean it is not a well-formed program; it is undefined what it does. A weak reference cannot generate undefined behavior due to its release, even under -Ounchecked.

使用 unowned 是程序员声明引用将在其整个生命周期内有效的声明.这甚至不是 Type! 断言的东西.! 类型只是断言引用在访问时有效.这就是为什么你不能在 unowned 上测试 x == nil .它不是可选的.它不是变相可选"(如 Type!).它必须始终有效.

Using unowned is a statement by the programmer that the reference will be valid over its entire lifetime. That's not even something Type! asserts. ! types just assert that the reference will be valid at the point that it is accessed. That's why you can't test x == nil on an unowned. It is not optional. It's not "optional in disguise" (like Type!). It must always be valid.

然而,与弱引用不同的是,当另一个实例具有相同的生命周期或更长的生命周期时,将使用无主引用.... 无主引用应该总是有价值.—— 【Swift 编程语言】

Unlike a weak reference, however, an unowned reference is used when the other instance has the same lifetime or a longer lifetime. ... An unowned reference is expected to always have a value. —— [The Swift Programming Language]

所以对于你的最深可能的哲学"来说,unowned 包括一个在弱中不存在的先决条件.这个前提存在于程序之外,必须由程序员证明,而不是编译器,以确保程序的格式良好.

So to your "deepest possible philosophical," unowned includes a precondition that does not exist in weak. This precondition exists outside the program, and must be proven by programmer, not the compiler, in order to ensure a well-formed program.

对于是否有理由使用 unowned,如果我们采取绝对的立场(如您的问题),肯定是有理由的.在已知前提条件为真的情况下,它是最严格的类型.weak 是比 unowned 更弱的类型;它表达的先决条件较少.好的类型理论鼓励我们尽可能使用最强的(最严格的;最少的合法值)类型,并且 unowned 是比 weak 更强的类型.

To whether there is a reason to use unowned, there certainly is if we're taking an absolutest stance (as in your question). It is the tightest type in cases where the precondition is known to be true. weak is a weaker type than unowned; it expresses fewer preconditions. Good type theory encourages us to use the strongest (most restrictive; fewest legal values) types we can, and unowned is a stronger type than weak.

在非绝对主义(实用")意义上,选择更强类型的结果是更简单的代码.当你使用weak时,你每次使用它时都要不断地重新断言它不是nil的前提并处理它的情况(可能插入nil>fatalError 只是用更多的工作重新发明了 unowned).使用 unowned 可以让您一次断言这个前提条件.这将创建更简单、更正确的代码.我从来没有使用 unowned 来提高速度.我一直用它来避免一遍又一遍地回答但如果它是零呢?"在它永远不能为零的代码中.

In a non-absolutist ("practical") sense, the result of picking a stronger type is simpler code. When you use weak, you have to constantly re-assert the precondition that it is not nil every time you use it and handle the cases where it is (possibly inserting fatalError which just reinvents unowned with more work). Using unowned lets you assert this precondition one time. This creates simpler, more correct code. I've never used unowned for speed. I've always used it to avoid answering over and over again "but what if it's nil?" in code where it must never be nil.

这篇关于迅速.无主相对于弱性能的(绝对)唯一的特定优势是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆