加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 在多个单元中进行通用实例化可膨胀可执行文件?

发布时间:2020-12-15 04:27:11 所属栏目:大数据 来源:网络整理
导读:本 Embarcadero article讨论XE7 IDE的内存问题包含以下内容: Be aware of the “Growth by Generics” Another scenario that might depend on your application code and cause an increase of the memory used by the compiler and the debugger relates
本 Embarcadero article讨论XE7 IDE的内存问题包含以下内容:

Be aware of the “Growth by Generics”

Another scenario that might depend on your application code and cause an increase of the memory used by the compiler and the debugger relates with the way generic data types are used. The way the Object Pascal compiler works can cause the generation of many different types based on the same generic definition,at times even totally identical types that are compiled in different modules. While we won’t certainly suggest removing generics,quite the contrary,there are a few options to consider:

  • Try to avoid circular unit references for units defining core generic types
  • Define and use the same concrete type definitions when possible
  • If possible,refactor generics to share code in base classes,from which a generic class inherits

我理解的最后一个项目前两个我不太清楚.

这些问题是否仅影响IDE性能,还是影响编译代码的大小?

例如,考虑第二项,如果我声明TList< Integer>在两个单独的单元中,我可以在我的可执行文件中的每个单元中获得两个独立的代码块?我当然希望不要!

解决方法

点2.这是指在可能的情况下实例化相同的通用类型.例如使用TList< Integer>在所有地方,而不是具有两种通用类型TList< Integer>和TList 在几个单元中将仅包括TList< Integer>的单个副本.在exe文件.另外,声明TIntegerList = TList< Integer>会导致相同的.

泛型人群指的是具有完整的TList< T>即使底层生成的代码相同,您也可以使用每种特定类型的副本.

例如:TList< TObject>和TList< TPersistent>将包括TList< T>的两个单独副本.即使生成的代码可以折叠成单个代码.

这将我们移动到第3点,其中使用基类为普通类代码,然后在其上使用泛型类来获取类型安全性,可以在编译期间和在exe文件中节省内存.

例如,在非通用TObjectList之上构建泛型类只会包含每个特定类型的精简通用层,而不是完整的TObjectList功能.报告为QC 108966

TXObjectList<T: class,constructor> = class(TObjectList)
  protected
    function GetItem(index: Integer): T;
    procedure SetItem(index: Integer; const Value: T);
  public
    function Add: T;
    property Items[index: Integer]: T read GetItem write SetItem; default;
  end;

function TXObjectList<T>.GetItem(index: Integer): T;
begin
  Result := T( inherited GetItem(index));
end;

procedure TXObjectList<T>.SetItem(index: Integer; const Value: T);
begin
  inherited SetItem(index,Value);
end;

function TXObjectList<T>.Add: T;
begin
  Result := T.Create;
  inherited Add(Result);
end;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读