The Dark Secrets Of Faster Compilation With Delphi

In this post, we will explore various ways of improving Delphi applications and reducing development time. Moreover, we learn how to debug faster and smarter by using less known but valuable debugger features like groups, thread support, data breakpoints, and more.

  • Faster Parallel Programs
  • Compiler settings for effectively debug
  • Breakpoints
  • Passcount
  • Debugging Multithreaded Programs
  • Exceptions
  • Hardware breakpoints
  • and much more

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

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.

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:

The Dark Secrets Of Faster Compilation With Delphi with 8 workers

If you would like to learn more about this, you can watch our CodeRage session here on FastMM and Delphi: []

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:

The Dark Secrets Of Faster Compilation With Delphi - compiler optimization settings
  • It is usually preferred to disable the optimization in the debug mode. It can help to better watch and evaluate values.
  • Turn on Debug Information property if you want to tap into RTL along with Use debug .dcus options.

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.

The Dark Secrets Of Faster Compilation With Delphi - debugger settings

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:

  • Breakpoint modification
  • Breakpoint group
  • Enabling and disabling certain breakpoint or a breakpoint group
  • Conditional breakpoint
  • The breakpoint on a specific thread
  • Providing different colors for breakpoints to highlight the process 
  • and more

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:

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.

Desktop First UX Summit