Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2023-07

$IMPLICITEXCEPTIONS vs early function return

7 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Hairy Pixels

Curious, exemptions require $IMPLICITEXCEPTIONS (https://www.freepascal.org/docs-html/prog/progsu34.html) or you leak memory with managed types and apparently these try..finally blocks slow down function calls quite a bit.

Question is, how is this different from early function returns? Does the compiler still need to wrap the whole function body in try..finally to capture any places the function could return?

Regards,
Ryan Joseph

Sven Barth
to Hairy Pixels
Hairy Pixels wrote
Question is, how is this different from early function returns? Does the compiler still need to wrap the whole function body in try..finally to capture any places the function could return?

There is no difference between early return or not, because if the early return isn't taken any other path needs to be protected as well. Thus the whole function is wrapped which deals with all of that - and also simplifies the implementation.

Regards,
Sven

Hairy Pixels
to Sven Barth
On Jul 14, 2023, at 4:19 PM, Sven Barth wrote: There is no difference between early return or not, because if the early return isn't taken any other path needs to be protected as well. Thus the whole function is wrapped which deals with all of that - and also simplifies the implementation.

I see so it's really not about exceptions but rather the managed types in general.

The manual says it makes all functions slower, 5-10%?? Does that apply to all functions or just ones with managed types in them? It's also curious why the try..finally block would have the kind of impact, all it does is call finalize functions on managed types right?

Regards,
Ryan Joseph

Jonas Maebe
to Hairy Pixels
Hairy Pixels wrote
The manual says it makes all functions slower, 5-10%?? Does that apply to all functions or just ones with managed types in them? It's also curious why the try..finally block would have the kind of impact, all it does is call finalize functions on managed types right?

It also sets up an exception frame. FPC uses setjmp/longjmp for that, which means that while restoring everything when an exception happens is fast, saving the exception frame itself is relatively slow.

Jonas

Hairy Pixels
to Jonas Maebe
On Jul 15, 2023, at 7:18 AM, Jonas Maebe via fpc-pascal wrote: It also sets up an exception frame. FPC uses setjmp/longjmp for that, which means that while restoring everything when an exception happens is fast, saving the exception frame itself is relatively slow.

Ok so just to confirm does disabling $IMPLICITEXCEPTIONS leak memory with ref counted types if a function exists early? For example:

===========================

{$implicitexceptions off}
procedure MyProcedure;
var
  s: AnsiString;
begin
  s := '1234';
  exit;
  // leaking??
end;

Regards,
Ryan Joseph

Jonas Maebe
to Hairy Pixels
Hairy Pixels wrote 7 lines
> On Jul 15, 2023, at 7:18 AM, Jonas Maebe via fpc-pascal wrote: > > It also sets up an exception frame. FPC uses setjmp/longjmp for that, which means that while restoring everything when an exception happens is fast, saving the exception frame itself is relatively slow. Ok so just to confirm does disabling $IMPLICITEXCEPTIONS leak memory with ref counted types if a function exists early?

No, local variables are still finalised on exiting the function. Only when an exception happens, the the exit code that contains this finalisation won't execute

Jonas

Hairy Pixels
to Jonas Maebe
On Jul 15, 2023, at 10:17 AM, Jonas Maebe via fpc-pascal wrote: No, local variables are still finalised on exiting the function. Only when an exception happens, the the exit code that contains this finalisation won't execute

So the trade off is exceptions put a cost on all functions but when they fire they can jump out of multiple stack frames and are faster then going back up the stack with normal returns (which clean up memory for you normal). I think that makes sense. Thanks.

Regards,
Ryan Joseph