<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Fri, Jan 29, 2016 at 1:24 PM, Marco van de Voort <span dir="ltr"><<a href="mailto:marcov@stack.nl" target="_blank">marcov@stack.nl</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span>In our previous episode, Sven Barth said:<br>
</span><span>> > Interfaces are relatively slow. Not only because of the refcounting, but<br>
> > returning interfaces in a tight loop (e.g. to get the elements of an<br>
> > enumeration) would be a new allocation each time.<br>
><br>
> Note: the enumerator of a for-in is only fetched once before the loop, so<br>
> it does not really matter whether it's an interface or record as long as<br>
> the elements themselves aren't on demand allocated interfaces.<br>
<br>
</span>The trouble is that you then have to avoid for-in for many potentially small<br>
iterations as in the hash case.</blockquote></div><div><br></div><div>Hm... that's my case. I have an object list that the key is an integer and the value is an object, ie a hash map.</div><div><br></div><div>However, some years ago I got a problem using interfaces in an old draft that I made, so to fix the problem I chose abstract class instead of interface. I don't know if it is a compiler behaviour or something that I forgot, the test is:</div><div><br></div><div>=== begin code ===</div><div><br></div><div><div> ITest = interface</div><div> ['{1DB8BD50-FCA2-40B3-B25B-A858DD206D26}']</div><div> procedure SetTest(ATest: ITest);</div><div> end;</div><div><br></div><div> TTest1 = class(TInterfacedObject, ITest)</div><div> private</div><div> FTest: ITest;</div><div> public</div><div> procedure SetTest(ATest: ITest);</div><div> property Test: ITest read FTest write FTest;</div><div> end;</div><div><br></div><div> TTest2 = class(TInterfacedObject, ITest)</div><div> private</div><div> FTest: ITest;</div><div> public</div><div> procedure SetTest(ATest: ITest);</div><div> property Test: ITest read FTest write FTest;</div><div> end;</div></div><div><br></div><div>...</div><div><br></div><div><div>procedure TTest1.SetTest(ATest: ITest);</div><div>begin</div><div> FTest := ATest;</div><div>end;</div><div><br></div><div>procedure TTest2.SetTest(ATest: ITest);</div><div>begin</div><div> FTest := ATest;</div><div>end;</div><div><br></div><div>procedure TForm1.Button1Click(Sender: TObject);</div><div>var</div><div> VTest1, VTest2: ITest;</div><div>begin</div><div> VTest1 := TTest1.Create;</div><div> VTest2 := TTest1.Create;</div><div> VTest1.SetTest(VTest2);</div><div> VTest2.SetTest(VTest1);</div><div>end;</div></div><div><br></div><div>=== end code ===<br></div><div><br></div><div>It throws some memory leaks, so to fix the problem I used abstract class:</div><div><br></div><div>=== begin code ===</div><div><br></div><div><div> TTest = class abstract</div><div> public</div><div> procedure SetTest(ATest: TTest); virtual; abstract;</div><div> end;</div><div><br></div><div> TTest1 = class(TTest)</div><div> private</div><div> FTest: TTest;</div><div> public</div><div> procedure SetTest(ATest: TTest); override;</div><div> property Test: TTest read FTest write FTest;</div><div> end;</div><div><br></div><div> TTest2 = class(TTest)</div><div> private</div><div> FTest: TTest;</div><div> public</div><div> procedure SetTest(ATest: TTest); override;</div><div> property Test: TTest read FTest write FTest;</div><div> end;</div><div><br></div><div>...</div><div><br></div><div>procedure TTest1.SetTest(ATest: TTest);</div><div>begin</div><div> FTest := ATest;</div><div>end;</div><div><br></div><div>procedure TTest2.SetTest(ATest: TTest);</div><div>begin</div><div> FTest := ATest;</div><div>end;</div><div><br></div><div>procedure TForm1.Button1Click(Sender: TObject);</div><div>var</div><div> VTest1, VTest2: TTest;</div><div>begin</div><div> VTest1 := TTest1.Create;</div><div> VTest2 := TTest1.Create;</div><div> VTest1.SetTest(VTest2);</div><div> VTest2.SetTest(VTest1);</div><div> VTest1.Free;</div><div> VTest2.Free;</div><div>end;</div></div><div><br></div><div>=== end code ===</div><div><br></div><div>However, I need to use the ARC feature in some occasions, and it seems that the better FPC entity that already implements that is the TInterfacedObject. :-/</div><div><br></div>--<br><div><div dir="ltr"><div>Silvio Clécio</div></div></div>
</div></div>