Recent

Author Topic: Using generic method as a const or type for another generic method  (Read 1678 times)

Okoba

  • Hero Member
  • *****
  • Posts: 528
I want to send a parameter of a generic type to another generic type, the catch is that if I send the method as a parameter it seems FPC can not inline it and it takes much more time to execute.
So I thought if there is a way to pass the method as a const or a type to another method but Test3 or Test4 fails.
Is there any way?
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}
  4.  
  5. type
  6.   generic TTest<T> = procedure(A: T);
  7.  
  8.   generic procedure Test1<T>(A: T);
  9.   begin
  10.   end;
  11.  
  12.   generic procedure Test2<T>(A: T; F: specialize TTest<T>);
  13.   var
  14.     i: Integer;
  15.   begin
  16.     for i := 1 to 10000000 do
  17.       F(I);
  18.   end;
  19.  
  20.   generic procedure Test3<T, specialize TTest<T>>(A: T);
  21.   var
  22.     i: Integer;
  23.   begin
  24.     for i := 1 to 10000000 do
  25.       F(I);
  26.   end;
  27.  
  28.   generic procedure Test4<T, const F:specialize TTest<T>>(A: T);
  29.   var
  30.     i: Integer;
  31.   begin
  32.     for i := 1 to 10000000 do
  33.       F(I);
  34.   end;
  35.  
  36. begin
  37. end.          

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: Using generic method as a const or type for another generic method
« Reply #1 on: August 04, 2021, 09:09:51 am »
I want to send a parameter of a generic type to another generic type, the catch is that if I send the method as a parameter it seems FPC can not inline it and it takes much more time to execute.

Method/function pointers can not be inlined, no matter if generic or not.

Note: for certain situations the compiler could in theory be extended to optimize this, but this is currently not the case and also won't be the case in general.
« Last Edit: August 04, 2021, 09:11:39 am by PascalDragon »

Okoba

  • Hero Member
  • *****
  • Posts: 528
Re: Using generic method as a const or type for another generic method
« Reply #2 on: August 04, 2021, 09:33:11 am »
I guessed it can be for a constant in generics. It can be useful for proving a custom compare function for search/sort. For now the only way come to mind is to pass the function parameter and in my tests it makes it much slower.

 

TinyPortal © 2005-2018