Difference between revisions of "Avoiding implicit try finally section"

From Free Pascal wiki
Jump to navigationJump to search
(Minor changes for clarity)
m
 
(29 intermediate revisions by 16 users not shown)
Line 1: Line 1:
Sometimes it's important to know that compiler can wrap some code in implicit try ... finally section. In some cases this can significantly affect the speed of given code.
+
{{Avoiding implicit try finally section}}
  
Here's a link to archived discussion on fpc-devel list regarding this issue, with subject "TList slowness in classes" : http://www.mail-archive.com/fpc-devel@lists.freepascal.org/msg01367.html
+
== Overview ==
 +
When optimizing code it helps to know that the [[Compiler|compiler]] will wrap certain code constructs in an implicit [[Try|<syntaxhighlight lang="pascal" inline>try </syntaxhighlight>]] … [[Finally|<syntaxhighlight lang="pascal" inline>finally</syntaxhighlight>]] statement.
 +
This is needed whenever you use [[Variable|variables]] such as [[Ansistring|AnsiString]], [[Variant|Variant]] or [[Dynamic array|dynamic arrays]] which require [[Initialization|initialization]] and [[Finalization|finalization]] (i.e. where the standard [[Procedure|procedures]] <syntaxhighlight lang="pascal" inline>initialize</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>finalize</syntaxhighlight> are needed for correct allocation and release of acquired memory).
  
And below is a small demo program that
+
For example, a procedure like
* Shows a trick how to avoid implicit try ... finally block (without changing the meaning or safety of the code) in some cases.
+
<syntaxhighlight lang="pascal">
* When run, this program clearly shows that avoiding an implicit try ... finally block can make code a lot faster. When I run this program on my system, I get
+
procedure doSomething;
Time of Test_ResourceString:       107
+
var
Time of Test_ResourceString_Faster: 16
+
  msg: ansiString;
 +
begin
 +
  // do something with msg
 +
end;
 +
</syntaxhighlight>
 +
is actually expanded by the compiler to look like this (difference highlighted):
 +
<syntaxhighlight lang="pascal" highlight="5-6,8-10">
 +
procedure doSomething;
 +
var
 +
  msg: ansiString;
 +
begin
 +
  initialize(msg);
 +
  try
 +
    // do something with msg
 +
  finally
 +
    finalize(msg);
 +
  end;
 +
end;
 +
</syntaxhighlight>
 +
 
 +
The compiler thereby ensures that the reference count of <syntaxhighlight lang="pascal" inline>msg</syntaxhighlight> will be properly decremented when <syntaxhighlight lang="pascal" inline>procedure doSomething</syntaxhighlight> exits with [[Exceptions|exception]].
 +
However, often this may have significant adverse effects on the generated code's speed.
 +
 
 +
This is issue was a subject on the <tt>fpc-devel</tt> list in the [http://www.mail-archive.com/fpc-devel@lists.freepascal.org/msg01367.html <syntaxhighlight lang="pascal" inline>TList</syntaxhighlight> slowness classes] thread.
 +
 
 +
Note, that temporary <syntaxhighlight lang="pascal" inline>ansiString</syntaxhighlight> variables can be created ''implicitly''.
 +
The only way to be completely certain about what actually is being done is to read the [[Assembler|assembler]] output.
 +
 
 +
== Possible solutions ==
 +
* use [[$implicitExceptions|<syntaxhighlight lang="pascal" inline>{$implicitexceptions off}</syntaxhighlight>]]: Ensure this applies to release versions only. Debugging can become cumbersome with that switch especially locating memory leaks and corruption.
 +
* split off rarely used code that causes an implicit <syntaxhighlight lang="pascal" inline>try…finally</syntaxhighlight> into separate procedures. (You can use nested procedures)
 +
* use [[Const#const parameter|<syntaxhighlight lang="pascal" inline>const</syntaxhighlight> parameters]] rather than value parameters. This avoids the need to change <syntaxhighlight lang="pascal" inline>refcount</syntaxhighlight> but temporary variables could still be an issue.
 +
* use [[Global variables|global variables]]: You have to be careful with reentrancy issues here though and temporary variables could still be an issue.
 +
* use non-reference-counted types like [[ShortString|<syntaxhighlight lang="pascal" inline>shortstring</syntaxhighlight>]].
 +
 
 +
== Risks and when to apply ==
 +
 
 +
{{Warning|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 [[sImplicitExceptions|<syntaxhighlight lang="pascal" inline>{$implicitExceptions}</syntaxhighlight>]] was added to the {{Doc|package=RTL|unit=strutils|text=<syntaxhighlight lang="pascal" inline>strutils</syntaxhighlight>}} [[Unit|unit]].
 +
For this, the following approach was followed:
 +
* A [[Routine|routine]] that calls a routine that [[Raise|raises]] exceptions is unsafe – e.g. {{Doc|package=RTL|unit=sysutils|identifier=strtoint|text=<syntaxhighlight lang="pascal" inline>strToInt</syntaxhighlight>}}, but not {{Doc|package=RTL|unit=sysutils|identifier=strtointdef|text=<syntaxhighlight lang="pascal" inline>strToIntDef</syntaxhighlight>}}.
 +
* A routine that raises exceptions itself is unsafe.
 +
* Very large routines are not worth the trouble, because of the risk and low gains – e.g. {{Doc|package=RTL|unit=sysutils|identifier=datetimeroutines|text=date formatting}} routines.
 +
* Floating point usage can raise exceptions that are converted into catchable exceptions by [[sysutils|<syntaxhighlight lang="pascal" inline>sysUtils</syntaxhighlight>]]. I'm not sure if this really is sufficient reason, but I skipped floating point using routines initially for this reason.
 +
 
 +
If you detect problems with these changes please contact [[User:Marcov|Marco]].
 +
 
 +
== Demo program ==
  
<pre>
+
Below is a small demo [[Program|program]] that
 +
 
 +
* When run, clearly shows that avoiding an implicit <code>try … finally</code>-block can make code a lot faster. When I run this program on my system, I get
 +
time of fooNormal: 141
 +
time of fooFaster: 17
 +
* Shows a trick how to avoid implicit <syntaxhighlight lang="pascal" inline>try … finally</syntaxhighlight>-block (without changing the meaning or safety of the code) in some cases (when you don't need to actually use that [[Ansistring|<syntaxhighlight lang="pascal" inline>AnsiString</syntaxhighlight>]]/[[Variant|<syntaxhighlight lang="pascal" inline>Variant</syntaxhighlight>]]/[[Data type|something]] every time procedure is called but e.g. only if some parameter has some particular value).
 +
 
 +
<syntaxhighlight lang="pascal">
 +
program implicitExceptionDemo;
 +
 
 +
// for exceptions
 
{$mode objfpc}
 
{$mode objfpc}
 +
// data type 'string' refers to 'ansistring'
 +
{$longstrings on}
  
 
uses
 
uses
   {BaseUnix, Unix needed only to implement Clock} BaseUnix, Unix,
+
   {$IFDEF UNIX}
   SysUtils;
+
  BaseUnix, Unix, // needed only to implement clock()
 +
  {$ENDIF}
 +
   sysUtils;
  
function Clock: Int64;
+
function clock(): int64;
var Dummy: tms;
+
{$IFDEF UNIX}
 +
var
 +
  dummy: tms;
 
begin
 
begin
Clock := FpTimes(Dummy);
+
  clock := fpTimes(dummy);
 
end;
 
end;
 +
{$ELSE}
 +
var
 +
  TS: TTimeStamp;
 +
begin
 +
  TS := DateTimeToTimeStamp(Now);
 +
  result := TS.Time;
 +
end;
 +
{$ENDIF}
  
resourcestring
+
// When fooNormal() and fooFaster() are called
  SResString = 'blah blah blah blah blah blah blah blah blah blah';
+
// i is always >= 0, so no exception is ever actually raised,
 +
// and string constants are not really used.
  
{ Note that Test_ResourceString and Test_ResourceString_Fast
+
procedure fooNormal(i: integer);
  do exactly the same thing. }
+
var
 
+
  s: string;
procedure Test_ResourceString(i: Integer);
 
 
begin
 
begin
if i = -1 then raise Exception.Create(SResString);
+
  if i = -1 then
 +
  begin
 +
    s := 'Some operation with AnsiString';
 +
    raise Exception.Create(s);
 +
  end;
 
end;
 
end;
  
procedure Test_ResourceString_Faster(i: Integer);
+
procedure fooFaster(i: integer);
 
+
  //
 
   procedure RaiseError;
 
   procedure RaiseError;
 +
  var
 +
    s: string;
 
   begin
 
   begin
  raise Exception.Create(SResString)
+
    s := 'Some operation with AnsiString';
 +
    raise Exception.Create(s);
 
   end;
 
   end;
 
+
  //
 
begin
 
begin
if i = -1 then RaiseError;
+
  if i = -1 then
 +
  begin
 +
    RaiseError;
 +
  end;
 
end;
 
end;
  
{ Note that when I call Test_ResourceString and Test_ResourceString_Fast
+
// M A I N =================================================
  i is always >= 0 so Exception is never actually raised. }
 
 
 
 
const
 
const
   TestCount = 10000000;
+
   testCount = 10000000;
 
var
 
var
   i: Integer;
+
   i: integer;
   Start: Int64;
+
   start: int64;
 
begin
 
begin
Start := Clock;
+
  // normal version
for i := 0 to TestCount do Test_ResourceString(i);
+
  start := clock();
Writeln('Time of Test_ResourceString:       ', Clock - Start);
+
  for i := 0 to testCount do
+
  begin
Start := Clock;
+
    fooNormal(i);
for i := 0 to TestCount do Test_ResourceString_Faster(i);
+
  end;
Writeln('Time of Test_ResourceString_Faster: ', Clock - Start);
+
  writeLn('time of fooNormal: ', clock() - start);
 +
 
 +
  // faster version
 +
  start := clock();
 +
  for i := 0 to testCount do
 +
  begin
 +
    fooFaster(i);
 +
  end;
 +
  writeLn('time of fooFaster: ', clock() - start);
 
end.
 
end.
</pre>
+
</syntaxhighlight>
 +
 
 +
By putting <syntaxhighlight lang="pascal" inline>raiseError</syntaxhighlight> into a nested [[Scope|scope]] of <syntaxhighlight lang="pascal" inline>fooFaster</syntaxhighlight>, exception handling does not become part of the main thread of execution.
 +
 
 +
[[Category:Software security]]

Latest revision as of 18:23, 22 January 2024

English (en) suomi (fi) Bahasa Indonesia (id) русский (ru)

Overview

When optimizing code it helps to know that the compiler will wrap certain code constructs in an implicit tryfinally statement. This is needed whenever you use variables such as AnsiString, Variant 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. 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

  • use {$implicitexceptions off}: Ensure this applies to release versions only. Debugging can become cumbersome with that switch especially locating memory leaks and corruption.
  • split off rarely used code that causes an implicit tryfinally into separate procedures. (You can use nested procedures)
  • use const parameters rather than value parameters. This avoids the need to change refcount but temporary variables could still be an issue.
  • use global variables: You have to be careful with reentrancy issues here though and temporary variables could still be an issue.
  • use non-reference-counted types like shortstring.

Risks and when to apply

Warning-icon.png

Warning: 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. For this, the following approach was followed:

  • A routine that calls a routine that raises exceptions is unsafe – e.g. strToInt, but not strToIntDef.
  • A routine that raises exceptions itself is unsafe.
  • Very large routines are not worth the trouble, because of the risk and low gains – e.g. date formatting routines.
  • Floating point usage can raise exceptions that are converted into catchable exceptions by sysUtils. I'm not sure if this really is sufficient reason, but I skipped floating point using routines initially for this reason.

If you detect problems with these changes please contact Marco.

Demo program

Below is a small demo program that

  • When run, clearly shows that avoiding an implicit try … finally-block can make code a lot faster. When I run this program on my system, I get
time of fooNormal: 141
time of fooFaster: 17
  • Shows a trick how to avoid implicit try finally-block (without changing the meaning or safety of the code) in some cases (when you don't need to actually use that AnsiString/Variant/something every time procedure is called but e.g. only if some parameter has some particular value).
program implicitExceptionDemo;

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

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

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

// When fooNormal() and fooFaster() are called
// i is always >= 0, so no exception is ever actually raised,
// and string constants 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
  // normal version
  start := clock();
  for i := 0 to testCount do
  begin
    fooNormal(i);
  end;
  writeLn('time of fooNormal: ', clock() - start);

  // faster version
  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.