Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

The Dark Secrets Of Faster Compilation With Delphi

the dark secrets of faster compilation with delphi

In this post, we’ll look at how to use a Windows compiler to improve Delphi applications and shorten development time. Furthermore, we learn how to debug more quickly and intelligently by utilizing lesser-known but valuable debugger features such as groups, thread support, data breakpoints, and others.

How to write fast parallel programs in Delphi?

If you have been following CodeRage Online Developer Conferences, you know about FastMM4 and FastMM5. FastMM5 is a fast replacement memory manager for Embarcadero Delphi applications that scales well across multiple threads and CPU cores, is not prone to memory fragmentation, and supports shared memory without the use of external files.

How can I achieve fast programs in Delphi using FastMM?

With the newest update of FastMM that is FastMM5, you can achieve 15% higher performance on the single-threaded, and 30% higher on the multithreaded benchmarks.

Reasons for slowdown

If your Delphi application does not scale on a new multiprocessor, multicore, or multithreaded hardware, the obstacle might be lock contention in the memory allocator. Because threads are fighting for allocators.

The obvious solution when the memory manager is slowing you down is to utilize fewer memory allocations that are either preload the memory and then reuse it without releasing it back to the system or change the algorithm in your code.

You can find different methods to improve your application. Now it is demonstration time. We have an application here that do some calculations and we do that calculation with several workers to achieve higher performance

procedure TForm1.Button1Click(Sender: TObject);
begin
  SpeedTest := TSpeedTest.create(100);
  SpeedTest.NumWorkers := inpNumWorkers.Value;

  Button1.Enabled := false;
  Screen.Cursor := crHourGlass;

  Form1.Caption := 'Time = ' + SpeedTest.Execute.ToString;

  SpeedTest.Free;
  Button1.Enabled := true;
  Screen.Cursor := crDefault;

end;

As you can see, this is the driver code. It creates a test with some parameters, then sets the number of workers and executes the test with workers.

function TSpeedTest.Execute: integer;
var
  i: Integer;
  StopWatch: TStopwatch;
begin

  Stopwatch := TStopwatch.Create;
  Stopwatch.Reset;
  Stopwatch.Start;

  if fNumWorkers > 1 then
    Parallel.For(0, fList.Count - 1)
    .NumTasks(fNumWorkers)
    .Execute(
      procedure(i: integer)
      begin
        fList[i].Execute;
      end)

  else
    for i := 0 to fList.Count - 1 do
      fList[i].Execute;

  Stopwatch.Stop;
  result := Stopwatch.ElapsedMilliseconds;
end;

In the execute function, it just goes over the list. fList contains 100 items that we created in the constructor. As you can see, it goes over using parallel for loop if there is the worker number given.

It is like we are doing some stress test on memory manager to benchmark.

Here are the results with 1 and 2 workers:

Now with 8 workers, you can see a little better result:

If you would like to learn more about this, you can watch our CodeRage session here on FastMM and Delphi: [https://www.youtube.com/watch?v=p-5mJyXvmrc]

How to effectively debug Delphi programs?

To effectively debug a program you should set few project options which can be found in the project options dialog. Here are some important settings are stored in the Delphi compiling tab. Follow the picture here:

Besides, there are other configurations you should know about. Go to the Tools menu and click Options. Then at the bottom, click the Debugger tab.

Most experienced Delphi developers prefer to check some options to make debugging processes easier. For instance, you can check the Rearrange editor local menu on the run option. So that you can have debug menus available at the top of the rick click menu. Furthermore, allowing the Allow side effects and function calls in new watches, so that your watch is fully functional.

How to use Breakpoints in RAD Studio Delphi?

Each breakpoint which you can set by pressing F5 or with the mouse has a bunch of properties associated with it. To access properties for breakpoints you can right-click the breakpoint or breakpoint list window.

Overall, breakpoints pause program execution at a certain location or when a particular condition occurs. You can set source breakpoints and module load breakpoints in the Code Editor before and during a debugging session. 

Besides, there are several options available in the IDE, for instance:

Be sure to read the official documentation on Embarcadero DocWiki.

How to use the pass count breakpoint option in Delphi?

By default, the pass count is set to 1. So, if the pass count equals 1 when the breakpoint line is encountered, the breakpoint is activated and pauses the program execution on that line of code.

Pass count – provides you to specify a particular number of times that a breakpoint needs to be passed for the breakpoint to be activated. So the pass counter tells the debugger to pause the program execution the nth time that the breakpoint is encountered during the program run.

How to debug multithreaded Delphi programs?

When debugging multithreaded applications there will be some limitations on debugging sessions/threads. And it works with only one thread.

For instance, when some code is called from more than one thread, and we put a breakpoint that is only activated from one thread and not from other available threads.

One of the best practices in naming threads. By setting a name for threads you can evaluate the process easily. Here is a sample code on naming threads:

procedure ThreadProc;
var
  I: Integer;
begin
  TThread.NameThreadForDebugging('Demo Thread');
  for I := 1 to 100 do 
  begin
    Sleep(100);
    DoSomething(55);
  end;
end;

How to use Hardware Breakpoints?

In addition to software breakpoints which always slow down the execution of a program, RAD Studio provides hardware-level breakpoints.

Hardware breakpoints – make use of dedicated registers and hence are limited in number. These can be set on both volatile and nonvolatile memory.

They are triggered when a value in memory is modified this is incredibly useful when you want to find out why some variable is changing its value when it shouldn’t have to downsides of hardware breakpoints out.

Learn more about breakpoints on Embarcadero DocWiki.

Where to learn even more about the secrets of super-fast Delphi

To finish off, you can follow this webinar to dive even deeper into the secrets of Delphi!


RAD Studio Delphi is the fastest way to create applications which can run on Windows, macOS, Linux, iOS and Android. Download a free trial copy today and turn your ideas into applications your users will love.

Exit mobile version