Avoiding implicit try finally section

This is an old revision of Avoiding implicit try finally section, edited by Kai Burghardt at . The current revision is 30014.

substitute legacy syntaxhighlight syntax

Overview

When optimizing code it helps to know that the compiler will wrap certain code constructs in an implicit try … finally-block. This is needed whenever you use variables such as ansiStrings, variants or dynamic arrays which require initialization and finalization (i.e. where the standard procedures initialize and finalize are needed for correct allocation and release of acquired memory).

For example, a procedure like

procedure doSomething;
var
    msg: ansiString;
begin
    // do something with msg
end;

is actually expanded by the compiler to look like this (difference highlighted):

procedure doSomething;
var
    msg: ansiString;
begin
    initialize(msg);
    try
        // do something with msg
    finally
        finalize(msg);
    end;
end;

The compiler thereby ensures that the reference count of msg will be properly decremented when procedure doSomething exits with exception[s]?. However, often this may have significant adverse effects on the generated code's speed.

This is issue was a subject on the fpc-devel list in the TList slowness classes thread.

Note, that temporary ansiString variables can be created implicitly. The only way to be completely certain about what actually is being done is to read the assembler output.

Possible solutions

Risks and when to apply

These exception frames are generated for a reason. If you leave them out any exception in that code will leave a memory leak

In 2007 {$implicitExceptions} was added to the strutils unit. Meanwhile, https://www.freepascal.org/docs-html/rtl/sysutils/.html has probably followed. For this, the following approach was followed:

If you detect problems with these changes please contact Marco.

Demo program

Below is a small demo program that

time of fooNormal: 141
time of fooFaster: 17
program implicitExceptionDemo(input, output, stderr);

// for exceptions
{$mode objfpc}
// data type 'string' refers to 'ansistring'
{$longstrings on}

uses
  {$IFDEF UNIX}
    // baseUnix, unix needed only to implement clock
    BaseUnix, Unix,
  {$ENDIF}
    sysUtils;

function clock(): int64;

var
  {$IFDEF UNIX}
    dummy: tms;
  {$ELSE}
    TS : TTimeStamp;
  {$ENDIF}
begin
  {$IFDEF UNIX}
    clock := fpTimes(dummy);
  {$ELSE}
   TS:=DateTimeToTimeStamp(Now);
   result := TS.Time;
  {$ENDIF}
end;



// Note: when fooNormal and fooFaster are called
//       i is always >= 0, so no exception is ever actually raised.
// So string constants SNormal and SResString are not really used.

procedure fooNormal(i: integer);
var
    s: string;
begin
    if i = -1 then
    begin
        s := 'Some operation with AnsiString';
        raise exception.create(s);
    end;
end;

procedure fooFaster(i: integer);
    procedure raiseError;
    var
        s: string;
    begin
        s := 'Some operation with AnsiString';
        raise exception.create(s);
    end;
begin
    if i = -1 then
    begin
        raiseError;
    end;
end;


// M A I N =================================================
const
    testCount = 10000000;
var
    i: integer;
    start: int64;
begin
    start := clock();
    for i := 0 to testCount do
    begin
        fooNormal(i);
    end;
    writeLn('time of fooNormal: ', clock() - start);
    
    start := clock();
    for i := 0 to testCount do
    begin
        fooFaster(i);
    end;
    writeLn('time of fooFaster: ', clock() - start);
end.

By putting raiseError into a nested scope of fooFaster, exception handling does not become part of the main thread of execution.