Recent

Author Topic: Reference counting plz?  (Read 56227 times)

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9909
  • Debugger - SynEdit - and more
    • wiki
Re: Reference counting plz?
« Reply #60 on: April 03, 2014, 04:25:31 pm »
Someone was talking about doing ref count by hand, and what problems he had.
So I gave him some info, that may (on may not) be useful.

Yes, if there was automatic ref counting the compiler would do hidden try except for that. It does currently do so for strings and dyn array.

But we do not have automatic ref count.

It was already pointed out what the steps are to volunteer for doing it:
http://forum.lazarus.freepascal.org/index.php/topic,22231.msg144318.html#msg144318
http://www.freepascal.org/faq.var#extensionselect

So, if you want it, go ahead. Follow the steps, do a proposal, get an agreement on the requirements,  and implement it.


marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11459
  • FPC developer.
Re: Reference counting plz?
« Reply #61 on: April 04, 2014, 01:35:05 am »
Someone was talking about doing ref count by hand, and what problems he had.
So I gave him some info, that may (on may not) be useful.

Yes, if there was automatic ref counting the compiler would do hidden try except for that. It does currently do so for strings and dyn array.

(and interfaces)
 
« Last Edit: April 05, 2014, 07:04:07 pm by marcov »

BeniBela

  • Hero Member
  • *****
  • Posts: 908
    • homepage
Re: Reference counting plz?
« Reply #62 on: April 05, 2014, 06:40:43 pm »
Someone was talking about doing ref count by hand, and what problems he had.
So I gave him some info, that may (on may not) be useful.
 


Well that was completely useless

I doubt that "someone" wanted advice, he was just complaining that his proposed "proposition" was being ignored

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9909
  • Debugger - SynEdit - and more
    • wiki
Re: Reference counting plz?
« Reply #63 on: April 05, 2014, 07:41:06 pm »
@BeniBela:
useless for you, well indeed (since you say so).

"completely useless": I wonder how you come to this judgement, and how you know that this applies to *all* other past and future readers of my post?

I wonder even, if you had confirmation or any form of secured knowledge af the usefulness to the one person who wrote about ref by hand first?
But even, if that person would find it useless, this is a public forum, information is always for all readers, so once the topic was up, there are many readers and still will be more in the future.

You have some magic power, so you know it will be useless to them all?

If I had not known, it would have been useful to me.

Yes that is right: Even if there was fully automated refcounting, I would prefer refcounting by hand. That leaves the control of how it works with me. And that is what I prefer.
Who are you to say there is no one else who might see things that way?

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9909
  • Debugger - SynEdit - and more
    • wiki
Re: Reference counting plz?
« Reply #64 on: April 05, 2014, 07:55:17 pm »
he was just complaining that his proposed "proposition" was being ignored

Maybe, but that would be strange.

Since the answer was given before, with reference on how to make a proposal, and the need to find a volunteer.
« Last Edit: April 05, 2014, 08:00:16 pm by Martin_fr »

hinst

  • Sr. Member
  • ****
  • Posts: 303
Re: Reference counting plz?
« Reply #65 on: April 05, 2014, 08:01:24 pm »
Whoa whoa. Let's solve problems, not create them. Peace plz.
I would not argue with site admin unless I want to get banned and go ask my mom programming-related questions  :D

I do not want to implement features for FPC myself, and even if I wanted to, I am not sure if I could. Perhaps the best thing to do is to stick with existing compiler features and achieve goals using those existing features, not dream about new features.
Too late to escape fate

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9909
  • Debugger - SynEdit - and more
    • wiki
Re: Reference counting plz?
« Reply #66 on: April 05, 2014, 09:06:42 pm »
Whoa whoa. Let's solve problems, not create them. Peace plz.
I would not argue with site admin unless I want to get banned and go ask my mom programming-related questions  :D
If I argue, I argue as user. All else would be abusive.

Quote
I do not want to implement features for FPC myself, and even if I wanted to, I am not sure if I could. Perhaps the best thing to do is to stick with existing compiler features and achieve goals using those existing features, not dream about new features.
Well you can always just add it as feature request, but then it still needs a volunteer.

Lazarus has a wiki page for that (not sure if fpc has). Mantis may or may not be ok. IMHO Mantis is good, if there is a chance it will be done in not to far future.



marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11459
  • FPC developer.
Re: Reference counting plz?
« Reply #67 on: April 05, 2014, 11:22:07 pm »

Quote
I do not want to implement features for FPC myself, and even if I wanted to, I am not sure if I could. Perhaps the best thing to do is to stick with existing compiler features and achieve goals using those existing features, not dream about new features.
Well you can always just add it as feature request, but then it still needs a volunteer.

No, IMHO even a feature request needs to request something concrete and  implementable, not just "doing something with automatic memory management". That is too vague, and would probably be closed with "use interfaces"

mse

  • Sr. Member
  • ****
  • Posts: 286
Re: Reference counting plz?
« Reply #68 on: April 06, 2014, 08:56:30 am »
Yes that is right: Even if there was fully automated refcounting, I would prefer refcounting by hand. That leaves the control of how it works with me. And that is what I prefer.
Me too. I very well remember the nightmare with the reference counted COM interfaces for the Delphi compatible MSEgui. The FPC CORBA-style interfaces were a big simplification.

hinst

  • Sr. Member
  • ****
  • Posts: 303
Re: Reference counting plz?
« Reply #69 on: April 17, 2014, 09:34:28 am »
I recently came up with a test. I wanted to find out what is performance penalty from COM interface overhead.
Here is what I found: there is no overhead.
I compared standard TInterfacedObject implementing IUnknown with my custom implementation of reference-counted object (which does not implement COM interface)

Code: [Select]
program project1;

{$Interfaces COM}

uses
  EpikTimer,
  PSHO_SharedObjectU;

type
  TCOM = class(TInterfacedObject)

  end;

procedure TestCOM(aCom: IUnknown);
var
  b: IUnknown;
begin
  b := aCom;
  b := nil;
end;

procedure TestSha(const aSha: TSharedObject);
var
  b: TSharedObject;
begin
  b := nil;
  aSha.TakeTo(@b);
  b.Drop;
end;

var
  com: IUnknown;
  sha: TSharedObject;
  i: Integer;
  timer: TEpikTimer;

begin
  timer := TEpikTimer.Create(nil);

  com := TCOM.Create;
  timer.Clear;
  timer.Start;
  for i := 0 to 1000000 do
    TestCOM(com);
  timer.Stop;
  WriteLN(timer.ElapsedStr);

  sha := TSharedObject.Create;
  sha.Take;
  timer.Clear;
  timer.Start;
  for i := 0 to 1000000 do
    TestSha(sha);
  timer.Stop;
  WriteLN(timer.ElapsedStr);
  sha.Drop;

  timer.Free;
end.

I won't post my implementation of TSharedObject because it's pretty irrelevant, and there's nothing interesting about it. What matters is:
COM interface AddRef & Release 1 000 000 times takes about 50 milliseconds
My custom object AddRef & Release 1 000 000 times takes about 50 milliseconds as well
In terms of memory I found that each my object allocates about 88 bytes while TInterfacedObject allocates 80 bytes [even slightly better].

What I am still unsure about though is whether a object-based implementation would work faster, or at least it should allocate less memory. 80 bytes for an empty object is a bit too much

----
Worth mentioning in relation to COM interfaces: consider
procedure TestCOM(aCom: IUnknown); //and
procedure TestCOM(const aCom: IUnknown);
It works faster with const enabled. Otherwise it works significantly slower: 70 milliseconds instead of 50
« Last Edit: April 17, 2014, 09:36:09 am by hinst »
Too late to escape fate

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Reference counting plz?
« Reply #70 on: April 17, 2014, 01:03:09 pm »
Worth mentioning in relation to COM interfaces: consider
procedure TestCOM(aCom: IUnknown); //and
procedure TestCOM(const aCom: IUnknown);
It works faster with const enabled. Otherwise it works significantly slower: 70 milliseconds instead of 50
This is odd. Interface is a pointer. What's happening in the assembler level? (it feels like with "const" AddRef is just not being called, assuming there would be no changes to the interface).

Is it possible to increase the test time up to a second? Just to be more accurate.
How about 10 M iterations, rather than 1M ?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11459
  • FPC developer.
Re: Reference counting plz?
« Reply #71 on: April 17, 2014, 01:15:11 pm »
I recently came up with a test. I wanted to find out what is performance penalty from COM interface overhead.

On what platform did you test?

 

hinst

  • Sr. Member
  • ****
  • Posts: 303
Re: Reference counting plz?
« Reply #72 on: April 17, 2014, 02:01:21 pm »
This is odd. Interface is a pointer. What's happening in the assembler level? (it feels like with "const" AddRef is just not being called, assuming there would be no changes to the interface).
This is in fact not odd at all. Same happens with all reference counted types. For example, strings. If you have
Code: [Select]
procedure Go(s: string);Then the compiler will increase reference count for s when entering procedure; but if you have
Code: [Select]
procedure Go(const s: string);Then the compiler will not touch reference count for s.
Compiler skips reference counting for const arguments. Take a look at this article plz: http://www.freepascal.org/docs-html/ref/refsu63.html .

On what platform did you test?
I tested on Windows.
If I increase iteration count to 10 M then I get
  0,449796 for COM
  0,359612 for my object
  // seconds
I have a very basic testing app; I could do a more in-depth test; but it will take some time to develop testing app. Perhaps I will do it; not sure when
Too late to escape fate

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11459
  • FPC developer.
Re: Reference counting plz?
« Reply #73 on: April 17, 2014, 02:32:30 pm »
Move your test to a procedure and then do

 
Code: [Select]
coinitialize(nil);
  benchmarkproc;
  couninitialize(nil);

hinst

  • Sr. Member
  • ****
  • Posts: 303
Re: Reference counting plz?
« Reply #74 on: April 17, 2014, 02:39:38 pm »
Move your test to a procedure and then do

 
Code: [Select]
coinitialize(nil);
  benchmarkproc;
  couninitialize(nil);

Thanks for suggestion. Tried it. Memory usage increased by 1 kilobyte. I doubt though that it got attached to com interface instance, perhaps it was allocated globally. In terms of speed, however, no difference at all. Note that I do not measure time spent on class instance construction, only assignment
Too late to escape fate

 

TinyPortal © 2005-2018