Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi project with many units takes a lot to run

I have a dpr with 290+ units.

The compiled exe is 50MB.

The dpr code is now like this:

begin
  ShowMessage('Before Initialize');
  Application.Initialize;

When I double click on the built exe I notice that 8 seconds pass before I see "Before Initialize". Is this because of big exe size? Or is there a way to minimize this time?

like image 815
LaBracca Avatar asked Jan 20 '11 12:01

LaBracca


3 Answers

Before Application.Initialize every initialization section of every unit is executed. You might have some code in there that takes time.

The number of units is not the problem. I have a project with 1100+ units, exe is 35 MB and it starts instantaneously.

If you are starting from network drive or really slow disk you might experience a slowdown.

like image 54
Mikael Eriksson Avatar answered Oct 05 '22 05:10

Mikael Eriksson


Based on your question, it can be anything.

The only advice I can give you is to measure:
Log the timestamps of every entry/exit in all your unit initialization sections.

Based on one of your comments (which you should add to your question as it describes more detail):

WindowsCodecs.dll is initialized by one of your units, probably converting one or more images from one format into another.
You should delay the conversion until the result of that conversion is needed.

--jeroen

like image 39
Jeroen Wiert Pluimers Avatar answered Oct 05 '22 03:10

Jeroen Wiert Pluimers


The initialization section of the units is normally not a speed problem (unless you have some database-related stuff in there).

What could be slow is the TForm loading from resources.

It's always better to have the TForm created on the fly, only when it's necessary: - Go to "Project" menu, then select "Options", then the "Forms" tab. - Put all not mandatory forms from left list to the right "available" list. - Create the forms on request, by some code.

The unit remains the same:

type
  TOneForm = class(TForm)
  ....
  end;

var
  OneForm: TOneForm;

But you can use the following code to create the form on request:

Instead of your former

  OneForm.ShowModal;

uses this kind of code

  if OneForm=nil then
    OneForm := TOneForm.Create(Application);
  OneForm.ShowModal;

You'll find the loading of the application much faster.

Note: I just read out that the problem was before form loading. So the above trick won't work for this particular problem. I keep the answer because it could be useful to others. I'll read better next time. :(

In all cases, having a lot of code run from the initialization is not a good design. It sounds like a lot of global objects or variables... refactoring could make sense here... :)

like image 35
Arnaud Bouchez Avatar answered Oct 05 '22 04:10

Arnaud Bouchez