除了“包装”外,别无其他记录-我应该解决它吗? [英] Nothing but "packed" records -- should I fix it?

查看:79
本文介绍了除了“包装”外,别无其他记录-我应该解决它吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在查看旧版Delphi 7程序中的某些代码时,我注意到到处都有记录,其中记录着 packed 。当然,这意味着该记录是逐字节存储的,并且未对齐以便更快地访问CPU。打包似乎是盲目地完成的,目的是要使编译器或其他产品胜过智能-基本上是评估几个字节的内存而不是更快地访问

While reviewing some code in our legacy Delphi 7 program, I noticed that everywhere there is a record it is marked with packed. This of course means that the record is stored byte-for-byte and not aligned to be faster for the CPU to access. The packing seems to have been done blindly as an attempt to outsmart the compiler or something -- basically valuing a few bytes of memory instead of faster access

示例记录:

TFooTypeRec = packed record
    RID                 : Integer;
    Description         : String;
    CalcInTotalIncome   : Boolean;
    RequireAddress      : Boolean;
end;

我应该解决这个问题并使每条记录正常还是不打包?还是使用现代的CPU和内存,这可以忽略不计,并且可能浪费时间?开箱会导致任何问题吗?

Should I fix this and make every record normal or "not" packed? Or with modern CPUs and memory is this negligible and probably a waste of time? Are there any problems that can result from unpacking?

推荐答案

如果不完全了解每个打包记录在应用程序代码中的使用方式,就无法回答此问题。就像问我是否应该将此变量声明从Int64更改为Byte吗?一样。

There is no way to answer this question without a full understanding of how each of those packed records are used in your application code. It is the same as asking "Should I change this variable declaration from Int64 to Byte ?"

不知道该变量将是什么值以及维持答案所需的值可以是的。

Without knowing what values that variable will be expected and required to maintain the answer could be yes. Or it could be no.

类似地,在您的情况下。如果记录需要要打包,则应该打包。如果不需要包装,那么不包装也没有任何危害。如果您不确定或无法确定,那么最安全的方法就是将它们保持原样。

Similarly in your case. If a record needs to be packed then it should be left packed. If it does not need to be packed then there is no harm in not packing it. If you are not sure or cannot tell, then the safest course is to leave them as they are.

作为进行此确定的指南(您应决定继续进行) ,需要或建议进行记录打包的情况包括:

As a guide to making this determination (should you decide to proceed), situations where record packing is required or recommended include:


  • 记录值的持久性

  • 共享记录记录具有[潜在地]不同编译代码的值

  • 与外部定义结构的严格兼容性

  • 故意在不同结构的内存上覆盖类型布局

  • persistence of record values
  • sharing of record values with [potentially] differently compiled code
  • strict compatibility with externally defined structures
  • deliberately overlaying a type layout over differently structured memory

这不一定是详尽无遗的清单,它们的共同点是:

This isn't necessarily an exhaustive list, and what these all have in common is:


  • 记录包含相邻字节中的一系列值,该记录的任何潜在生产者或使用者都必须并且可以依赖该值,而不会受到编译器或其他因素的干扰

我建议您(如果可能且可行)确定e在每种情况下打包的目的是什么,并为此目的将文档添加到记录声明本身,以便将来任何有相同问题的人都不必经过该发现过程,例如:

What I would recommend is that (if possible and practical) you determine what purpose packing serves in each case and add documentation to that effect to the record declaration itself so that anyone in the future with the same question doesn't have to go through that discovery process, e.g.:

  type
    TSomeRecordType = packed record
      // This record must be packed as it is used for persistence
      ..
    end;

    TSomeExternType = packed record
      // This record must be packed as it is required to be compatible
      //  in memory with an externally defined struct (ref: extern code docs)
      ..
    end;

这篇关于除了“包装”外,别无其他记录-我应该解决它吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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