Insydneydoessetting[weak]infrontofanobjectmakeapenalty?

In delphi sydney, does setting [weak] in front of an object (not an interface) make a penalty ?
Exemple :

  TMyObject = class(Tobject)
  Private
    [weak] FOwner: TMyObject;
    ....
  end;

I ask because I know that internally [weak] references are stored in a list and thus it's have some drawbacks (regarding speed). As now with Sydney ARC is gone there is no need anymore to put [weak] in front of an object (as far as I know), but as I want to keep my code compatible with Rio and less I ask if I can safely leave the [weak] reference without suffering of useless performance lost (in sydney)

there is a teeny-weeny problem with the [weak] attribute. It denotes a
zeroing weak reference that will be zeroed (niled) when the object it
points to is no longer valid. In order to do that, the compiler has to
track such objects at runtime and that introduces some overhead. If
you are tracking many such references, that can introduce a
significant performance loss.

回答

[weak]对象引用的属性仅在 ARC 编译器上实现。在经典的非 ARC 编译器[weak]属性上,在对象引用上使用时没有任何作用并且没有惩罚。

由于 10.4 Sydney 不再有 ARC 编译器,[weak]因此不再需要属性,但可以使用它来保持向后兼容性。它不会影响使用非 ARC 编译器编译的代码。

[weak]对非 ARC 编译器没有影响,可以通过 CPU 视图轻松检查。

var
  Obj: TObject;
  [weak] WObj: TObject;
begin
  Obj := TObject.Create;
  WObj := Obj;

  Obj.Free;
end;

在 Android ARC 编译器 10.3 上分配Obj给弱WObj将调用_InstWeakCopy过程来跟踪弱引用:

使用 10.4 Android 编译器编译时相同的代码不再调用 _InstWeakCopy


注意:此答案严格涵盖[weak]在对象引用上使用时属性的行为。用于接口引用的[weak]工作方式与以前相同,因为它是在 10.1 Berlin 的非 ARC 编译器中引入的。

  • Of course, `[weak]` still has an effect on interface references. (I think some readers of this A might not see that if they read it out of context.)
  • @AndreasRejbrand That is why I put __object references__ in bold, right on the top.

以上是Insydneydoessetting[weak]infrontofanobjectmakeapenalty?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>