justyy
justyy

Reputation: 6041

The sequence of use unit in Delphi

I am wondering if the following using unit order is correct.

uses
  FastMM4 in 'Fast\FastMM4\FastMM4.pas',
  VCLFixPack in 'VCLFixPack.pas',  
  FastMove in 'Fast\FastMove\FastMove.pas',
  FastCode in 'Fast\FastCode\FastCode.pas',
  FastMM4Messages in 'Fast\FastMM4\FastMM4Messages.pas',

Why should I bother with the order?

Upvotes: 3

Views: 668

Answers (2)

David Heffernan
David Heffernan

Reputation: 613242

You have three separate pieces of code here:

  1. FastMM
  2. VCLFixPack
  3. FastCode

It is important to install the replacement memory manager, FastMM, before any heap allocations are performed. So I think it is clear that must be the first unit to include. And you may as well keep the two FastMM units together.

The other units make changes to the code in memory, to fix bugs (VCLFixPack) or to improve performance (FastCode). Although you have separately identified FastMove and FastCode, in reality, the FastCode unit actually installs a suite of improved functions, defined over a dozen separate separate units.

It seems quite likely that it would be important to install the VCL fixes before the VCL units are included. So VCLFixPack should appear before any VCL units.

As for FastCode, since it only influences performance, you could perfectly well use it at any point in the .dpr file. It probably does not matter if the initialization code runs with the vanilla RTL code. That said, for sake of consistency it makes sense to keep together all these units which change the behaviour of the runtime. So I would write your .dpr uses clause like this:

uses
  FastMM4 in 'Fast\FastMM4\FastMM4.pas',
  FastMM4Messages in 'Fast\FastMM4\FastMM4Messages.pas',
  FastCode in 'Fast\FastCode\FastCode.pas',
  VCLFixPack in 'VCLFixPack.pas',  
  ....

I put FastCode before VCLFixPack because logically the RTL is at a lower level than the VCL. However, it does not matter either way around. But you've got to pick one, and that was my reasoning.

I also omitted an explicit reference to the FastMove unit. That is used by FastCode, along with a host of other units, and if you are going to omit the others, you may as well omit FastMove.

Upvotes: 5

Rudy Velthuis
Rudy Velthuis

Reputation: 28826

There may be a few reasons:

  • Some units perform actions at startup. The order of such actions may be important, like FastMM4, which replaces the default memory manager with its own. This should be done before any memory is allocated.
  • If several units contain duplicate identifiers, the "last" one overrides the previous one. This may be on purpose, or it may be by accident. The other identifiers are still accessible, but not without qualification with the unit name.
  • The same as for actions at startup, actions at the end of life my be important too, and their order.

Note that it is not guaranteed that the order is preserved. If one of the units uses another, that one must be loaded first. To make a unit really the very first, make it the first in the .dpr or .dpk file.

Upvotes: 7

Related Questions