[fpc-pascal] Re: operator overloading and counting references / creating / destoying anonymous instances

Flávio Etrusco flavio.etrusco at gmail.com
Thu Jul 28 21:45:54 CEST 2011

On Thu, Jul 28, 2011 at 1:59 PM, Bernd <prof7bit at googlemail.com> wrote:
> 2011/7/28 Bernd <prof7bit at googlemail.com>:
>> I have tried making
>> use of Interface and TInterfacedObject and this seems to do what I
>> want: for example when witing A := A + B the + operator would return a
>> new instance and the reference counting would then automatically call
>> the destructor of A when it assigns the newly created number.
> I have profiled it
> http://imagebin.org/165317
> procedure Loop(a,b: IFoo);
> var
>  I : Integer;
> begin
>  for i := 0 to 10000 do begin
>    //BN_mul(b.Handle, a.Handle, a.Handle, b.Context);
>    b := a * a;
>  end;
> end;
> This creates and destroys an object of TFoo everytime and this in turn
> will also create and free resources inside OpenSSL, its only spending
> 37% of the time doing actually useful work (BN_mul).
> I think I'm not going to continue this route. I can't see any possible
> way to make useful use of overloading these operators, other than
> making a few lines in other places of the code look a little bit nicer
> at the cost of degrading performance by a factor of 3 (for add instead
> of mul its even factor 6).
> Occasionally I hear other people mentioning operator overloading as a
> must-have feature of any decent language but I wonder what real-world
> problems they are actually solving with it. Are other compilers better
> at dealing with these problems, is there room for improvement?
> Bernd

Implement += and *= operator to avoid allocating new objects?
You could also/instead override  TObject.NewInstance and FreeInstance
to implement a pool of objects.


More information about the fpc-pascal mailing list