Difference between revisions of "Avoiding implicit try finally section"
m (minor rephrasing for clarity) |
(review) |
||
Line 2: | Line 2: | ||
== Overview == | == Overview == | ||
− | When optimizing code it helps to know that the compiler will wrap certain code constructs in an implicit try | + | When optimizing code it helps to know that the compiler will wrap certain code constructs in an implicit <code>try … finally</code>-block. |
+ | This is needed whenever you use variables such as [[Ansistring|<code>ansiString</code>s]], [[Variant|<code>variant</code>s]] or [[Dynamic array|dynamic arrays]] which require [[Initialization|initialization]] and [[Finalization|finalization]] (i.e. where the standard procedures <code>initialize</code> and <code>finalize</code> are needed for correct allocation and release of acquired memory). | ||
− | + | For example, a procedure like | |
<syntaxhighlight> | <syntaxhighlight> | ||
− | procedure | + | procedure doSomething; |
− | var | + | var |
− | + | msg: ansiString; | |
begin | begin | ||
− | + | // do something with msg | |
end; | end; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
is actually expanded by the compiler to look like this: | is actually expanded by the compiler to look like this: | ||
− | |||
<syntaxhighlight> | <syntaxhighlight> | ||
− | procedure | + | procedure doSomething; |
− | var | + | var |
− | + | msg: ansiString; | |
begin | begin | ||
− | + | initialize(msg); | |
− | + | try | |
− | + | // do something with msg | |
− | + | finally | |
+ | finalize(msg); | ||
+ | end; | ||
end; | end; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | The compiler thereby ensures that the reference count of | + | The compiler thereby ensures that the reference count of <code>msg</code> will be properly decremented when <code>procedure doSomething</code> 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 <code>fpc-devel</code> list in the [http://www.mail-archive.com/fpc-devel@lists.freepascal.org/msg01367.html <code>TList</code> slowness classes] thread. | |
− | Note that temporary | + | Note, that temporary <code>ansiString</code> 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} | + | * use [[sImplicitExeptions|<code>{$implicitexceptions off}</code>]]: 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 <code>try…finally</code> into separate procedures. (You can use procedures in procedures) | ||
+ | * use [[Const#const parameter|const parameters]] rather than value parameters. This avoids the need to change <code>refcount</code> 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|<code>shortstring</code>s]]. | ||
− | + | == 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}} | {{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 $ | + | In 2007 [[sImplicitExceptions|<code>{$implicitExceptions}</code>]] was added to the {{Doc|package=RTL|unit=strutils|text=<code>strutils</code> unit}}. |
− | * A routine that calls a routine that raises exceptions is unsafe | + | Meanwhile, {{Doc|package=RTL|unit=sysutils|tetx=<code>sysUtils</code>}} has probably followed. |
+ | For this, the following approach was followed: | ||
+ | * A routine that calls a routine that raises exceptions is unsafe – e.g. {{Doc|package=RTL|unit=sysutils|identifier=strtoint|text=<code>strToInt</code>}}, but not {{Doc|package=RTL|unit=sysutils|identifier=strtointdef|text=<code>strToIntDef</code>}}. | ||
* A routine that raises exceptions itself is unsafe. | * A routine that raises exceptions itself is unsafe. | ||
− | * Very large routines are not worth the trouble, because of the risk and low gains | + | * 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 | + | * Floating point usage can raise exceptions that are converted into catchable exceptions by [[sysutils|<code>sysUtils</code>]]. 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 == | |
− | |||
Below is a small demo program that | Below is a small demo 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 | |
− | * Shows a trick how to avoid implicit try | + | time of fooFaster: 17 |
+ | * Shows a trick how to avoid implicit <code>try … finally</code>-block (without changing the meaning or safety of the code) in some cases (when you don't need to actually use that [[Ansistring|<code>AnsiString</code>]]/[[Variant|<code>Variant</code>]]/[[Data type|something]] every time procedure is called but e.g. only if some parameter has some particular value). | ||
<syntaxhighlight> | <syntaxhighlight> | ||
− | {$mode objfpc}{$ | + | program implicitExceptionDemo(input, output, stderr); |
+ | |||
+ | // for exceptions | ||
+ | {$mode objfpc} | ||
+ | // data type 'string' refers to 'ansistring' | ||
+ | {$longstrings on} | ||
uses | uses | ||
− | + | // baseUnix, unix needed only to implement clock | |
− | + | BaseUnix, Unix, | |
+ | sysUtils; | ||
− | function | + | function clock(): int64; |
− | var | + | var |
+ | dummy: tms; | ||
begin | begin | ||
− | + | clock := fpTimes(dummy); | |
end; | end; | ||
− | procedure | + | // Note: when fooNormal and fooFaster are called |
− | var | + | // 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 | begin | ||
− | + | if i = -1 then | |
− | + | begin | |
− | + | s := 'Some operation with AnsiString'; | |
− | + | raise exception.create(s); | |
− | + | end; | |
end; | end; | ||
− | procedure | + | procedure fooFaster(i: integer); |
− | + | procedure raiseError; | |
− | + | var | |
− | + | s: string; | |
− | + | begin | |
− | + | s := 'Some operation with AnsiString'; | |
− | + | raise exception.create(s); | |
− | + | end; | |
− | |||
begin | begin | ||
− | + | if i = -1 then | |
+ | begin | ||
+ | raiseError; | ||
+ | end; | ||
end; | end; | ||
− | |||
− | |||
− | |||
+ | // M A I N ================================================= | ||
const | const | ||
− | + | testCount = 10000000; | |
var | var | ||
− | + | i: integer; | |
− | + | start: int64; | |
begin | 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. | end. | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | By putting <code>raiseError</code> into a nested [[Scope|scope]] of <code>fooFaster</code>, exception handling does not become part of the main thread of execution. | ||
[[Category:Tutorials]] | [[Category:Tutorials]] | ||
[[Category:FPC]] | [[Category:FPC]] |
Revision as of 15:29, 3 February 2018
│
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 try … finally
-block.
This is needed whenever you use variables such as ansiString
s, variant
s 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:
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
- 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
try…finally
into separate procedures. (You can use procedures in 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
s.
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 {$implicitExceptions}
was added to the strutils
unit.
Meanwhile, sysutils has probably followed.
For this, the following approach was followed:
- A routine that calls a routine that raises exceptions is unsafe – e.g.
strToInt
, but notstrToIntDef
. - 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 thatAnsiString
/Variant
/something every time procedure is called but e.g. only if some parameter has some particular value).
program implicitExceptionDemo(input, output, stderr);
// for exceptions
{$mode objfpc}
// data type 'string' refers to 'ansistring'
{$longstrings on}
uses
// baseUnix, unix needed only to implement clock
BaseUnix, Unix,
sysUtils;
function clock(): int64;
var
dummy: tms;
begin
clock := fpTimes(dummy);
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.