Recent

Author Topic: New language features?  (Read 21481 times)

PascalDragon

  • Hero Member
  • *****
  • Posts: 5755
  • Compiler Developer
Re: New language features?
« Reply #165 on: October 03, 2024, 01:18:47 pm »
As Ryan J said, if an anonymous function isn't assigned to a function reference then there won't be any heap allocation happening (only in FPC however, Delphi always goes through a function reference). In this case the compiler will essentially treat it as a nested function.

So performance are the same with nested function/procedure, or anonymous inline function/procedure. Isn't?

You have to differentiate here. The performance penality of assigning a function to a function reference is the allocation (and later on the deallocation). However if you call it you "only" have the penalty of a virtual method call aka an indirect call. This is a bit more expensive than a direct call, but it's not that bad.

If you call an anonymous function directly then it will be similar to a nested function which will both use direct calls then. If you assign the anonymous function to a nested function variable or a normal function/method variable you'll also have the penalty of an indirect call.

Ryan J

  • Full Member
  • ***
  • Posts: 138
Re: New language features?
« Reply #166 on: October 03, 2024, 01:21:07 pm »
If you call an anonymous function directly then it will be similar to a nested function which will both use direct calls then. If you assign the anonymous function to a nested function variable or a normal function/method variable you'll also have the penalty of an indirect call.

But are nested or anonymous functions able to be inlined? I thought they were not.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5755
  • Compiler Developer
Re: New language features?
« Reply #167 on: October 03, 2024, 01:25:42 pm »
If you call an anonymous function directly then it will be similar to a nested function which will both use direct calls then. If you assign the anonymous function to a nested function variable or a normal function/method variable you'll also have the penalty of an indirect call.

But are nested or anonymous functions able to be inlined? I thought they were not.

If the compiler can, it will inline:

Code: Pascal  [Select][+][-]
  1. program tnestedinline;
  2. {$mode objfpc}
  3. {$modeswitch anonymousfunctions}
  4. {$modeswitch functionreferences}
  5.  
  6. procedure Test;
  7.  
  8.   function Func(a, b: LongInt): LongInt; inline;
  9.   begin
  10.     Result := a + b;
  11.   end;
  12.  
  13. begin
  14.   Writeln(Func(2, 4));
  15.  
  16.   Writeln(function(a,b: LongInt): LongInt inline begin Result := a + b; end(4, 6));
  17. end;
  18.  
  19. begin
  20.   Test;
  21. end.

The relevant assembly code:

Code: [Select]
# [19] Writeln(Func(2, 4));
call fpc_get_output
movq %rax,%rbx
movq %rbx,%rdx
movq $6,%r8 ; <-- the inlined function
movl $0,%ecx
call fpc_write_text_sint
call fpc_iocheck
movq %rbx,%rcx
call fpc_writeln_end
call fpc_iocheck
# [21] Writeln(function(a,b: LongInt): LongInt inline begin Result := a + b; end(4, 6));
call fpc_get_output
movq %rax,%rbx
movq %rbx,%rdx
movq $10,%r8 ; <-- the inlined function
movl $0,%ecx
call fpc_write_text_sint
call fpc_iocheck
movq %rbx,%rcx
call fpc_writeln_end
call fpc_iocheck

Warfley

  • Hero Member
  • *****
  • Posts: 1761
Re: New language features?
« Reply #168 on: October 04, 2024, 12:17:05 am »
Code: Pascal  [Select][+][-]
  1.   Writeln(function(a,b: LongInt): LongInt inline begin Result := a + b; end(4, 6));
  2.  

You can (have to?) even add modifiers to the function signatures of anonymous functions? I think I'm going to scratch my eyes out... Who the hell has looked at that syntax and thought that this is a good idea?

PascalDragon

  • Hero Member
  • *****
  • Posts: 5755
  • Compiler Developer
Re: New language features?
« Reply #169 on: October 07, 2024, 09:37:13 pm »
Code: Pascal  [Select][+][-]
  1.   Writeln(function(a,b: LongInt): LongInt inline begin Result := a + b; end(4, 6));
  2.  

You can (have to?) even add modifiers to the function signatures of anonymous functions?

How else would you set the calling convention?

I think I'm going to scratch my eyes out... Who the hell has looked at that syntax and thought that this is a good idea?

Yeah... well... *looks towards Embarcadero*

Warfley

  • Hero Member
  • *****
  • Posts: 1761
Re: New language features?
« Reply #170 on: October 07, 2024, 09:45:33 pm »
How else would you set the calling convention?
I didn't consider them relevant for anonymous functions, to me calling conventions are mostly for external references, and as passing nested function pointers or (managed) function references outside of the code boundaries (e.g. to an API) seems not really desireable, I did not assume they were needed.

But I guess if you want to use inline assembly or something it comes in handy. Or did I miss something else?

But I mean there are also other directive like inline, noreturn and stuff like that that could be useful

PascalDragon

  • Hero Member
  • *****
  • Posts: 5755
  • Compiler Developer
Re: New language features?
« Reply #171 on: October 07, 2024, 10:06:28 pm »
How else would you set the calling convention?
I didn't consider them relevant for anonymous functions, to me calling conventions are mostly for external references, and as passing nested function pointers or (managed) function references outside of the code boundaries (e.g. to an API) seems not really desireable, I did not assume they were needed.

The interesting thing of function references is that in essence they are interfaces with a single Invoke method. In the Windows Foundation API used for Windows Apps callback functions are implemented using interfaces containing a single Invoke method (with stdcall calling convention). This essentially allows to pass anonymous functions to methods that take such a callback interface. Or in FPC even normal methods. ;)

But I mean there are also other directive like inline, noreturn and stuff like that that could be useful

inline and noreturn are only really useful if the anonymous function is called directly.

 

TinyPortal © 2005-2018