[fpc-devel] Defer keyword

Sven Barth pascaldragon at googlemail.com
Fri May 7 07:26:42 CEST 2021


Am 07.05.2021 um 00:17 schrieb Ryan Joseph via fpc-devel:
>
>> On May 6, 2021, at 4:05 PM, Sven Barth via fpc-devel <fpc-devel at lists.freepascal.org> wrote:
>>
>> Other than that, you're right and what Ryan is trying to do is definitely the intended purpose of try ... finally.
>>
> Is there any runtime code involved with try..finally or does it just reorganize the code to run at the end of the block? My understanding of the defer keyword is that is was just a fancy way to move some code into a block which always gets run with a function exits.
It depends on the platform. In general FPC uses setjmp/longjmp based 
exception handling, thus there is a slight penalty in setting up the 
exception frame (no matter if "finally" or "except").

In case of Win32 and Win64 the OS' Structured Exception Handling 
functionality is used. On i386-win32 there is still a runtime overhead 
due to how exceptions are managed, but on x86_64-win64 and aarch64-win64 
(and in theory also arm-wince and arm-win32, but we have no ARM support 
for SEH yet (and arm-win32 isn't implemented yet :P )) this is done 
through meta data located in the PE file (the .pdata and .xdata 
sections) which allows for an implicit setup of the frames and thus 
there'll only be a penalty if an exception occurrs (cause the OS and the 
exception handling code will have to parse that data).

Trunk also supports POSIX exceptions on selected *nix based systems, 
though I haven't looked in depth yet in how far they incur a runtime 
penalty (also they need to be enabled by enabling them in the compiler 
and then recompiling everything, cause they're still experimental).

Regards,
Sven


More information about the fpc-devel mailing list